2017-09-30 61 views
0

我有一個問題搞清楚什麼是從我的數組中隨機的字符串元素追加到JTextArea的最佳方法可能是。我的問題是,我不能從我的數組輸出相互連續相同的兩個字符串。 (不能和上次展示的一樣)。我嘗試了真正的循環,for循環。我只是不太確定如何編寫這個約束。任何指針正確的方向將不勝感激。在這個問題上已經有一段時間了,我已經從上到下掃除了stackoverlow。我的代碼現在運行良好,但我遇到的問題是在btnSubmit actionlistener中的createBottomPanel()方法中。由於從數組中的隨機字符串,不能有兩個相同的字符串連續選擇Java

package Tell; 

import java.awt.*; 
import java.awt.event.ActionEvent; 
import java.awt.event.ActionListener; 
import java.awt.image.BufferedImage; 
import java.io.File; 
import java.io.IOException; 
import javax.imageio.ImageIO; 
import javax.swing.*; 
import java.util.Random; 

public class FortuneTellerFrame extends JFrame { 

JPanel pnlTop, pnlMiddle, pnlBottom; 
JButton btnSubmit, btnQuit; 
JLabel lblFortuneTeller, lblPassword; 
JTextArea txaResults; 
JScrollPane jsp; 
String[] fortunes = {"A loved one will die","You will recieve a gift", 
    "A mysterious person will come into your life","You will encounter misfortune soon", 
    "Life will become easier for you","Sadness will overcome you", 
    "Good tidings are in your future","Some sum of money will find its way to you", 
    "Good news is around the corner","Persevere and you shall be rewarded", 
    "You will run out of time at a bad moment","You will fall and break a leg"}; 

public FortuneTellerFrame() { 
    add(this.createTopPanel(), BorderLayout.NORTH); 
    add(this.createMiddlePanel(), BorderLayout.CENTER); 
    add(this.createBottomPanel(), BorderLayout.SOUTH); 

    // Always set the size of data and the default close operation. 
    // Visibility needs to be set to true to be seen as well 
    this.setSize(400, 300); 
    this.setVisible(true); 
    this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 


    } 
    private JPanel createTopPanel() 
    { 
     pnlTop = new JPanel(); 
     pnlTop.setLayout(new GridLayout(2,2)); 
     ImageIcon icon = new ImageIcon("ball.jpg"); 
     Image image1 = icon.getImage(); // transform it 
     Image newimg = image1.getScaledInstance(75, 75, java.awt.Image.SCALE_SMOOTH); 
     icon = new ImageIcon(newimg); // transform back 
     JLabel label = new JLabel("Fortune Teller",icon,JLabel.CENTER); 
     label.setForeground(Color.red); 
     label.setFont(new Font ("Lucida Sans Unicode", Font.BOLD, 20)); 
     label.setHorizontalTextPosition(JLabel.CENTER); 
     label.setVerticalTextPosition(JLabel.BOTTOM); 

     pnlTop.add(label); 

     return pnlTop; 
    } 

    private JPanel createMiddlePanel() 
    { 
     pnlMiddle = new JPanel(); 
     txaResults = new JTextArea(10, 30); 
     txaResults.setFont(new Font ("Serif", Font.ITALIC, 10)); 
     jsp = new JScrollPane(txaResults,JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, 
       JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS); 
     pnlMiddle.add(jsp); 

     return pnlMiddle; 
    } 
    private JPanel createBottomPanel() 
    { 

     pnlBottom = new JPanel(); 
     btnSubmit = new JButton("Read My Fortune!"); 
     btnQuit = new JButton("Quit"); 
     btnSubmit.setFont(new Font ("Arial", Font.BOLD, 9)); 
     btnQuit.setFont(new Font ("Arial", Font.BOLD, 9));  

     btnQuit.addActionListener(e ->{ 
      System.exit(0); 
     }); 

     btnSubmit.addActionListener(e ->{ 
      //String username = this.txtFortuneTeller.getText(); 

      Random rand = new Random(); 
      String x = fortunes[rand.nextInt(fortunes.length)]; 

      this.txaResults.append(x + "\n"); 
     }); 

     pnlBottom.add(btnSubmit); 
     pnlBottom.add(btnQuit); 

     return pnlBottom; 
    } 

}

+2

爲什麼不隨機財富(一次)一然後迭代它? –

+0

'Collections.shuffle(Arrays.toList(fortunes))'會做到這一點。 –

+0

@AndyTurner我跟着你的代碼,並不斷找到toList方法的符號錯誤。我已經導入了列表和數組庫,但由於某種原因,我仍然收到錯誤。這與這種類型有什麼關係?我的數組元素是字符串 –

回答

0

如果我理解正確,可能會出現每個答案多次,但不能直接繼承,最簡單的辦法是,如果出現兩次,是記住上次的財富,並跳過:

class FortuneAppenderAction implements ActionListener { 

    private String lastFortune = null; 

    @Override 
    public void actionPerformed(ActionEvent e) { 
     Random rand = new Random(); 
     String x; 
     do { 
      x = fortunes[rand.nextInt(fortunes.length)]; 
     } 
     while (x.equals(lastFortune)); 
     lastFortune = x; 
     txaResults.append(x + "\n"); 
    } 
} 

而且在createBottomPanel()

btnSubmit.addActionListener(new FortuneAppenderAction()); 
相關問題