程序不会覆盖变量

时间:2017-09-18 13:04:48

标签: java swing actionlistener

我有一个程序,它使用3个radiobuttons在一个计数器的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();
            }

        }
    }
}

2 个答案:

答案 0 :(得分:2)

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

更改行

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

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

答案 1 :(得分: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");

然后

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

或者在java 8中:

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

}

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