I want a JFrame application with 2 buttons (eventually more) that I can use to switch between multiple repeating actions, ofr simplicity I'm just using a console print for now, though it will probably be calling a method instead later. Here is the framework for the JFrame:
public class DayNight extends JFrame implements ActionListener{
//JFrame entities
private JPanel animationPanel;
private JButton button;
private JButton button2;
public static void main(String[] args) {
DayNight frame = new DayNight();
frame.setSize(2000, 1300);
frame.setLocation(1000,350);
frame.createGUI();
frame.setVisible(true);
frame.setTitle("Day/Night Cycle, Rogier");
}
private void createGUI() {
setDefaultCloseOperation(EXIT_ON_CLOSE);
Container window = getContentPane();
window.setLayout(new FlowLayout() );
animationPanel = new JPanel();
animationPanel.setPreferredSize(new Dimension(2000, 900));
animationPanel.setBackground(Color.black);
window.add(animationPanel);
button = new JButton("choice1");
button.setFont(new Font("Arial", Font.PLAIN, 50));
window.add(button);
button.setActionCommand("choice1");
button.addActionListener(this);
button2 = new JButton("choice2");
button2.setFont(new Font("Arial", Font.PLAIN, 50));
window.add(button2);
button2.setActionCommand("choice2");
button2.addActionListener(this);
}
}
I've tried the following:
public void actionPerformed(ActionEvent event) {
String command = event.getActionCommand();
while ("Stop"!=(command)){
command = event.getActionCommand();
try{
Thread.sleep(500);
if ("choice1".equals(command)){
System.out.println("choice1");
}
else if("choice2".equals(command)){
System.out.println("choice2");
}
else{
System.out.println("no choice");
}
}
catch(InterruptedException ex){
Thread.currentThread().interrupt();
}
}
}
But after I click a button it keeps stuck on that print and I can't even interact with the buttons anymore. Am I missing something or do I need a completely different structure? I've examined a lot of different programs but they are too complicated for me to understand, reading the concurrency in swing also didn't clear it up for me.
Edit: There is no "stop" command yet because I don't need it for now.