使用while循环创建javagui的问题
我正在创建一个java GUI,它是一个算命师。每当您单击“获取我的财富”按钮时,GUI将吐出十二个财富中的一个,字符串将不会背靠背重复,可以在其他字符串之前重复。我已经做了大部分。但是现在我在创建while循环来显示字符串而不重复时遇到了一些问题。我看了我的书,但没什么帮助。如果你们能给我指出正确的方向,我将不胜感激。谢谢 我输入了所有的代码,以便您可以看到使用的变量。但我的问题是从RndButtonListener开始的使用while循环创建javagui的问题,java,Java,我正在创建一个java GUI,它是一个算命师。每当您单击“获取我的财富”按钮时,GUI将吐出十二个财富中的一个,字符串将不会背靠背重复,可以在其他字符串之前重复。我已经做了大部分。但是现在我在创建while循环来显示字符串而不重复时遇到了一些问题。我看了我的书,但没什么帮助。如果你们能给我指出正确的方向,我将不胜感激。谢谢 我输入了所有的代码,以便您可以看到使用的变量。但我的问题是从RndButtonListener开始的 package FortuneTellerRunner; import
package FortuneTellerRunner;
import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Random;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
/**
*
* @author a3cal_000
*/
class FortuneTellerFrame extends JFrame
{
final private JPanel mainPnl, titlePnl, displayPnl, buttonPnl, imagePnl;
final private JButton quitBtn, rndBtn;
final private JLabel titleLbl, iconLbl;
final private JTextArea displayTa;
final private JScrollPane scroller;
public String[] fortune = new String [12];
int newIndex, oldIndex;
private static final int HEIGHT = 250;
private static final int WIDTH = 450;
public FortuneTellerFrame()
{
setSize(WIDTH, HEIGHT);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
mainPnl = new JPanel();
mainPnl.setLayout(new BorderLayout());
displayPnl = new JPanel();
buttonPnl = new JPanel();
titlePnl = new JPanel();
ImageIcon icon = new ImageIcon("FortuneTellerIcon.JPEG");
iconLbl = new JLabel(icon);
titleLbl = new JLabel("Fortune Teller!");
displayTa = new JTextArea();
imagePnl = new JPanel();
scroller = new JScrollPane();
// Create the layout of the title panel
titlePnl.setLayout(new GridLayout(2,1));
add(mainPnl);
// Set the label to the panel.
titlePnl.add(titleLbl);
titlePnl.add(iconLbl);
// add the panel to the main panel.
mainPnl.add(titlePnl, BorderLayout.NORTH);
mainPnl.add(scroller, BorderLayout.CENTER);
mainPnl.add(displayTa, BorderLayout.CENTER);
// Create the "Get my fortune button.
rndBtn = new JButton("Get My Fortune!");
quitBtn = new JButton("Quit");
// Add the buttons to the buttonPnl in grid layout.
buttonPnl.add(rndBtn);
buttonPnl.add(quitBtn);
// Create the grid layout for the button panel.
buttonPnl.setLayout( new GridLayout(1, 2));
// Add the button panel to the grid layout, South.
mainPnl.add(buttonPnl, BorderLayout.SOUTH);
ActionListener listener = new RndButtonListener();
rndBtn.addActionListener(listener);
quitBtn.addActionListener(listener);
}
class RndButtonListener implements ActionListener
{
public void actionPerformed(ActionEvent evt)
{
fortune[0] = "He who throws dirt is losing ground.";
fortune[1] = "You will find the love of your life in food.";
fortune[2] = "Do or do not, there is no try.";
fortune[3] = "Tomorrow is a better day to try anything of importance.";
fortune[4] = "Life's not about how hard you can hit, but how hard you can get hit and keep moving forward.";
fortune[5] = "You can't be late until you show up.";
fortune[6] = "If you think things can't get worse it's probably only because you lack sufficent imagination.";
fortune[7] = "If youre at the top it means you have further to fall.";
fortune[8] = "Even in last place, youre still in the race.";
fortune[9] = "The road to riches is paved on the failures of others.";
fortune[10] = "If you feel like your going no where, get off the treadmill.";
fortune[11] = "Thinking about going to the gym is just as good as going.";
Random rnd = new Random(fortune.length);
do
{
newIndex = rnd.nextInt(fortune.length);
}
while(newIndex == oldIndex);
do
{
System.out.println(fortune[newIndex]);
displayTa.append(fortune[newIndex] + "||");
displayTa.updateUI();
mainPnl.updateUI();
oldIndex = newIndex;
}
while(newIndex != oldIndex);
class QuitButtonListener implements ActionListener
{
public void actionPerformed(ActionEvent evt)
{
System.exit(0);
}
}
}
}
}基本问题是每次都用相同的种子重新创建
随机
,这通常是一次又一次地创建相同的随机序列
相反,尝试使用
do {
newIndex = (int) Math.round(Math.random() * (fortune.length - 1));
} while (newIndex == oldIndex);
你也不需要第二个循环,它只是混乱混乱的情况
你也会发现
displayTa.append(fortune[newIndex] + "\n");
产生更好的输出(IMHO)
您可能还希望查看一下您的程序是否运行良好,但这是一个问题,fortune.length是一个随机种子,当我稍后调用random.nextInt()时,它只返回6和8 这样做
Random rnd = new Random();
并考虑MadProgrammer给出的格式化解决方案。
随机化()给出相同的数字模式。尝试随机(System.currentTimeMillis())。它使用当前时间作为种子,所以你可以得到真正的随机数。
我今天做了类似的事情,让我们看看我是否记得。。。我制作了一个int类型的数组列表,其中列出了我有多少物品(财富) 在那之后,只需循环访问财富for(int i = 0; i < fortune.length; i++) {
fortuneSeq.add(i);
}
for(int i = 0; i < fortune.length; i++) {
System.out.println(fortune[fortuneSeq.get(i)]);
//...
}
for(int i=0;i
编辑:愚蠢的自动更正,你不喜欢程序员
编辑:修复了一些furtune而不是fortunes和Fixed println语句。我认为您不需要第二个循环,您已经从第一个循环中获得了所需的所有信息。非常感谢您的回答!现在它可以正常工作了,我有不同性质的问题,但为了不让这个线程阻塞,我将在另一个线程中询问!但再次感谢!
for(int i = 0; i < fortune.length; i++) {
fortuneSeq.add(i);
}
Collections.shuffle(fortuneSeq);
for(int i = 0; i < fortune.length; i++) {
System.out.println(fortune[fortuneSeq.get(i)]);
//...
}