程序不覆盖变量

Program not overwriting variable

我有一个程序,它使用 3 个单选按钮在计数器的 3 个增量值之间切换,这里是 time。 我想在按下单选按钮时更改 status,它确实这样做了,但只是一小部分。启动程序时将继续打印

0
Normal
2
Normal
4
Normal
6

等当我按下按钮 slow 时,它打印 CHANGE Slow 一次,但随着 2 不断递增,并且每次仍然打印 Normal 。 在我再次选择另一个单选按钮之前,我如何才能永久切换到 status 的不同值和不同的增量?

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();
            }

        }
    }
}
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");

然后

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

或在java 8:

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

}

您实际上创建了第二个 DayNightCycle。

您正在创建 DayNightCycle-Objects,第一个显示 GUI,第二个打印在控制台上。

换行

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

(new Thread(frame)).start();