Je veux faire une application de swing qui a deux (ou plusieurs) boutons, qui basculent entre les actions en cours. Pour rester simple, ces actions peuvent être simplement une impression répétée. Voici le cadre de la JFrame:Java basculer les processus de commutateur avec GUI
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);
}
}
J'ai essayé ce qui suit, mais je sais maintenant c'était une approche tout à fait tort.
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();
}
}
}
J'ai depuis lu de nombreux articles et le doc "concurrency in swing" de Java Tutorials. Je comprends maintenant ce qu'est Idid mais je n'ai toujours aucune idée de comment commencer. Mon (sous-) objectif est que le programme imprime continuellement "choice1" (ou toute autre action), et qu'il passe à l'impression "choice2" après avoir appuyé sur l'autre bouton. Quelqu'un peut-il me pousser un peu dans la bonne direction?
Quit des questions reposter. Gardez toutes les informations dans un fil afin que tout le monde sache ce qui a déjà été suggéré. – camickr
J'ai réévalué à la fois les réponses que j'ai eu ici et sur reddit. Je ne peux pas continuer sur ma vieille question parce que les gens se mettent en colère et me disent de "faire une nouvelle question". Ils me disent alors de ne pas poster une nouvelle question. Arrivé avant, jsut sautant un pas. – RnRoger
'Ils me disent alors de ne pas poster une nouvelle question' - ce n'est pas une nouvelle question. C'est pourquoi toutes les informations devraient être dans la même question. La réponse ici (pour utiliser un sujet distinct) est la même que la réponse à votre dernière question, car la question est la même. – camickr