2017-09-18 1 views
0

J'ai un programme qui utilise 3 boutons radio pour basculer entre 3 valeurs d'incrémentation pour un compteur, ici time. Je veux changer status quand un radiobutton est pressé, et il le fait, mais seulement pour une fraction. Lors du lancement du programme gardera l'impressionVariable de programme ne pas écraser

0 
Normal 
2 
Normal 
4 
Normal 
6 

etc. Lorsque j'appuie sur le bouton slow imprime CHANGE Slow une fois, mais conserve incrémentée 2 et imprime encore Normal chaque fois. Comment est-ce que je peux avoir cette permutation permutée à une valeur différente pour status, et un incrément différent, jusqu'à ce que je choisisse un autre radiobutton encore?

package daynightcycle; 
import java.awt.*; 
import java.awt.event.ActionEvent; 
import java.awt.event.ActionListener; 
import javax.swing.*; 
import static javax.swing.JFrame.EXIT_ON_CLOSE; 

/** 
* Day/night cycle with visuals. Adjustable speed and time inserts. 
* Optional date or daycounter later 
* @author rogie 
*/ 
public class DayNightCycle extends JFrame implements Runnable{ 

    //JFrame entities 
    private JPanel animationPanel; 
    public JRadioButton button; 
    public JRadioButton button2; 
    public JRadioButton button3; 
    public int time = 0; 
    public String status = "Normal"; 


    public static void main(String[] args) { 
     DayNightCycle frame = new DayNightCycle(); 
     frame.setSize(2000, 1300); 
     frame.setLocation(1000,350); 
     frame.createGUI(); 
     frame.setVisible(true); 
     frame.setTitle("Day/Night Cycle, Rogier"); 
     (new Thread(new DayNightCycle())).start(); 
    } 

    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); 
    JRadioButton option1 = new JRadioButton("Slow"); 
    JRadioButton option2 = new JRadioButton("Normal", true); 
    JRadioButton option3 = new JRadioButton("Fast"); 
    option1.addActionListener(new ActionListener() { 
    public void actionPerformed(ActionEvent event) { 
      System.out.println("CHANGE"); 
      status = "Slow"; 
      System.out.println(status); 
     } 
    }); 
    option2.addActionListener(new ActionListener() { 
     public void actionPerformed(ActionEvent event) { 
      status = "Normal"; 
     } 
    }); 
    option2.addActionListener(new ActionListener() { 
     public void actionPerformed(ActionEvent event) { 
      status = "Fast"; 
     } 
    }); 

    //option2.setFont(new java.awt.Font("Tahoma", Font.BOLD, 30)); 
    //option2.putClientProperty("JComponent.sizeVariant", "huge"); //doesn't work 

    ButtonGroup group = new ButtonGroup(); 
    group.add(option1); 
    group.add(option2); 
    group.add(option3); 
    add(option1); 
    add(option2); 
    add(option3); 

     pack(); 
    } 

    public void run() { 

     while(true){ 
      System.out.println(time); 
      System.out.println(status); 
      try  
      { 
       Thread.sleep(500); 
       if (status.equals("Slow")) { 
        time += 1; 
       } 
       else if (status.equals("Normal")){ 
        time += 2; 
       } 
       else { 
        time += 3; 
       } 
      } 
       catch(InterruptedException ex) 
      { 
       Thread.currentThread().interrupt(); 
      } 

     } 
    } 
} 
+1

Vous avez également une faute de frappe: vous ajoutez deux ActionListeners à option2 .... – JensS

+0

Ouais, mais remarqués merci! – RnRoger

Répondre

2

Vous créez à DayNightCycle -Objets, le premier affiche l'interface graphique et les secondes impressions sur la console.

Modifier la ligne

(new Thread(new DayNightCycle())).start(); 

à

(new Thread(frame)).start(); 
1
public static void main(String[] args) { 
    final DayNightCycle frame = new DayNightCycle(); 
    frame.setSize(2000, 1300); 
    frame.setLocation(1000,350); 
    frame.createGUI(); 
    frame.setTitle("Day/Night Cycle, Rogier"); 

Et puis

EventQueue.invokeLater(new Runnable() { 
     @Override 
     public void run() { 
      frame.setVisible(true); 
     } 
    }); 

Ou en java 8:

EventQueue.invokeLater(() -> frame.setVisible(true)); 

} 

Vous en effet créé une deuxième DayNightCycle.