我正在创建一个算命先的java GUI。每次点击“获取我的财富”按钮时,GUI将吐出十二个财富中的一个,字符串将永远不会重复,可以在其他字符串消失之后重复。我已经做了大部分。但是现在我在创建while循环以显示字符串而不重复时遇到了一些麻烦。我看过那本没有帮助的书。如果你们能指出我正确的方向,那将非常感激。谢谢! 我输入了所有代码,因此您可以看到使用的变量。但我的问题始于课程RndButtonListener。
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);
}
}
}
}
}
答案 0 :(得分:0)
基本问题是你每次都使用相同的种子重新创建Random
,这通常会一遍又一遍地创建相同的随机序列。
而是尝试使用......
do {
newIndex = (int) Math.round(Math.random() * (fortune.length - 1));
} while (newIndex == oldIndex);
你也不需要第二个循环,这只会让人感到困惑。
你也可能会发现......
displayTa.append(fortune[newIndex] + "\n");
产生更好的输出(恕我直言)
您可能还希望查看How to use Scroll Panes
答案 1 :(得分:0)
你的程序运行正常,但这是一个问题,fortune.length是一个随机种子,当我后来调用Random.nextInt()时,它只返回6和8。
Random rnd = new Random(fortune.length);
这样做
Random rnd = new Random();
并考虑MadProgrammer提供的格式化解决方案。
答案 2 :(得分:0)
Random()给出相同的数字模式。尝试Random(System.currentTimeMillis())。它使用当前时间作为种子,因此您可以获得真正的随机数。
答案 3 :(得分:0)
我今天做了类似的事情,所以让我们看看我是否记得......我创建了一个int类型的ArrayList,我有多少项目(命运)
ArrayList<Integer> fortuneSeq = new ArrayList<Integer>();
然后从0开始添加一些数字来代码命运。
for(int i = 0; i < fortune.length; i++) {
fortuneSeq.add(i);
}
然后我使用Collections类中的shuffle()方法随机化列表。
Collections.shuffle(fortuneSeq);
在此之后,只需循环访问财富。
for(int i = 0; i < fortune.length; i++) {
System.out.println(fortune[fortuneSeq.get(i)]);
//...
}
编辑:愚蠢的自动更正,你不喜欢程序员。
编辑:修正了一些furtunes,而不是命运和固定的println语句。