I want a new string from my Array to show in console everytime i press the button in JFrame. However, when i press the button in JFrame it only show me 1 string that dosen't change. please help!
And sorry for my bad english, i'm from Sweden
Here is my code:
private JFrame frame;
private JPanel panel;
private JButton rand;
private String random;
public Start() {
frame = new JFrame("Java Jar exec");
frame.setVisible(true);
frame.setSize(450, 305);
frame.setLocation(610, 300);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setResizable(false);
panel = new JPanel();
panel.setLayout(null);
rand = new JButton("Rand");
rand.setBounds(10, 10, 10, 10);
rand.addActionListener(this);
rand.setActionCommand("rand");
rand.addMouseListener(this);
panel.add(rand);
frame.add(panel);
ArrayList<String>arr = new ArrayList <String>();
arr.add("1");
arr.add("2");
arr.add("3");
arr.add("4");
random = arr.get(new Random().nextInt(arr.size()));
}
@Override
public void actionPerformed(ActionEvent e) {
String name = e.getActionCommand();
if (name.equals("rand")) {
System.out.print(random + '\n');
}
}
There is more code, but this is the only important.
Aucun commentaire:
Enregistrer un commentaire