我有一个问题,想知道将数组中的随机String元素附加到JTextArea的最佳方法是什么。我的问题是,我的数组输出中的两个字符串不能相继连接。 (不能与上次提出的相同)。我试过了真正的循环,for循环。我不太确定如何编写这个约束。任何指向正确的方向将不胜感激。在这个问题已经有一段时间了,我已经从上到下设置了stackoverlow。我的代码现在运行正常,但我遇到的问题是在btnSubmit actionlistener中的createBottomPanel()方法中。感谢
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;
}
}
答案 0 :(得分: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() );