2017-09-30 52 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()); 
相关问题