I need Please help. I have a Code in Java where I have a little GUI. I cant click the Button twice. I can onlx activate the while loop but i cant deactivate it.
Here is my CODE:
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
public class Test2 {
public static void main(String[] args) {
JFrame Frame = new JFrame("");
Frame.setBounds(100, 100, 450, 300);
Frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Frame.setVisible(true);
JPanel Panel = new JPanel();
Frame.add(Panel);
JToggleButton toggle = new JToggleButton("");
Panel.add(toggle);
toggle.setPreferredSize(new Dimension(50,50));
toggle.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
int zufallszahl;
if(toggle.isSelected()) {
while(toggle.isSelected()) {
zufallszahl = (int)(Math.random() * 10 +1);
System.out.println(zufallszahl);
}
}
}
});
}
}