我在ArrayList中有12个字符串。我想在textarea中随机显示这些字符串。我已经到了弦随机出现的地方,但有时会重复。我不想让它们重复,直到它完成所有12个字符串。到目前为止,这是我的代码:
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.List;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.Random;
import javax.swing.Icon;
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;
import javax.swing.JTextField;
/**
* Layout of the application
*
* ###############################
* ## ##
* ## ##
* ## ##### ##
* ## ##### ##
* ## ##### ##
* ## ##
* ###############################
* ## ##
* ## ##
* ## Text Area ##
* ## ##
* ## ##
* ## ##
* ###############################
* ## ##
* ## #Fortune# #Quit# ##
* ## ##
* ###############################
*/
/**
*
* @author Kelle Schmitt
*/
public class FortuneTellerFrame extends JFrame
{
public FortuneTellerFrame()
{
//set width and height of the window
final int FRAME_HEIGHT = 400;
final int FRAME_WIDTH = 400;
//set width and height of the text area
final int AREA_ROWS = 10;
final int AREA_COLUMNS = 25;
//declare the panels that are going to be used.
final JPanel mainPnl, titlePnl, textPnl, controlPnl;
final JLabel fortuneImage;
//declare and find image for what your title/title image will be?
ImageIcon icon = new ImageIcon("src/1804-1-bad-fortune-teller.jpg");
fortuneImage = new JLabel("Fortune Teller!", icon, JLabel.CENTER);
fortuneImage.setVerticalTextPosition(JLabel.TOP);
fortuneImage.setHorizontalTextPosition(JLabel.CENTER);
//declare the buttons that are going to be used.
final JButton futureBtn, quitBtn;
//create the main panel, and add the other panels to it
mainPnl = new JPanel();
mainPnl.setLayout(new BorderLayout());
titlePnl = new JPanel();
textPnl = new JPanel();
controlPnl = new JPanel();
mainPnl.add(titlePnl, BorderLayout.NORTH);
mainPnl.add(textPnl, BorderLayout.CENTER);
mainPnl.add(controlPnl, BorderLayout.SOUTH);
//add the main panel to the parent frame
add(mainPnl);
setSize(FRAME_WIDTH, FRAME_HEIGHT);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//add other componets to the sub panel
titlePnl.add(fortuneImage);
final JTextArea fortuneArea;
fortuneArea = new JTextArea(AREA_ROWS, AREA_COLUMNS);
JScrollPane scrollPane = new JScrollPane(fortuneArea);
fortuneArea.setText("I will tell the future...");
textPnl.add(scrollPane);
futureBtn = new JButton("Read my Fortune!");
quitBtn = new JButton("Quit");
controlPnl.add(futureBtn);
controlPnl.add(quitBtn);
//create actionlisteners for the buttons
class QuitButtonListener implements ActionListener
{
public void actionPerformed(ActionEvent evt)
{
System.exit(0);
}
}
ActionListener quitListener = new QuitButtonListener();
quitBtn.addActionListener(quitListener);
class ReadMyFortuneListener implements ActionListener
{
public void actionPerformed(ActionEvent evt)
{
ArrayList<String> fortunes = new ArrayList<>();
fortunes.add("The Browns will win the Super Bowl. Just Kidding the will never happen.");
fortunes.add("You will get an A++ in Programming this semester!");
fortunes.add("You will die in a very humorous way.");
fortunes.add("You will fall in love with Kyle Hurt.");
fortunes.add("You will be attacked by Aliens in the near future.");
fortunes.add("You will be eaten by an alligator while walking to class today.");
fortunes.add("You will go pro in football.");
fortunes.add("Steve Jobs will come back from the dead to give you 1 billion dollars.");
fortunes.add("You are going to disappear and never be heard from again.");
fortunes.add("You are going to win a lifetime of moonshine.");
fortunes.add("A bag of sugar will fall on you in the store today.");
fortunes.add("You get to be the frountman of your favorite band.");
Random rand = new Random();
int pick = rand.nextInt(fortunes.size());
fortuneArea.setText(fortunes.get(pick));
}
}
ActionListener futureTeller = new ReadMyFortuneListener();
futureBtn.addActionListener(futureTeller);
}
}
答案 0 :(得分:3)
使用Collection shuffle静态方法来洗牌你的数字:
import java.util.List;
import java.util.ArrayList;
import java.util.Collections;
public class ShuffleNumbers {
static List<Integer> list = new ArrayList<Integer>();
static {
for (int i = 1; i <= 12; i++)
list.add(new Integer(i));
}
public static void main(String[] argv) throws Exception {
Collections.shuffle(list);
System.out.println(list);
}
}
答案 1 :(得分:1)
首先!
您必须将arraylist
字段放在FortuneTellerFrame类中:
这是代码:
ArrayList<String> fortunes = new ArrayList<>();
public FortuneTellerFrame()
{
fortunes.add("The Browns will win the Super Bowl. Just Kidding the will never happen.");
fortunes.add("You will get an A++ in Programming this semester!");
fortunes.add("You will die in a very humorous way.");
fortunes.add("You will fall in love with Kyle Hurt.");
fortunes.add("You will be attacked by Aliens in the near future.");
fortunes.add("You will be eaten by an alligator while walking to class today.");
fortunes.add("You will go pro in football.");
fortunes.add("Steve Jobs will come back from the dead to give you 1 billion dollars.");
fortunes.add("You are going to disappear and never be heard from again.");
fortunes.add("You are going to win a lifetime of moonshine.");
fortunes.add("A bag of sugar will fall on you in the store today.");
fortunes.add("You get to be the frountman of your favorite band.");
并且在actionPerform中,每次找到元素时都必须删除该元素。这是代码:
public void actionPerformed(ActionEvent evt)
{
Random rand = new Random();
int pick = rand.nextInt(fortunes.size());
fortuneArea.setText(fortunes.get(pick));
fortunes.remove(pick)
}