带有GUI的Java swing开关进程

时间:2017-09-14 12:32:45

标签: java swing jframe

我想制作一个包含两个(或多个)按钮的swing应用程序,这些按钮可在正在进行的操作之间切换。为了简单起见,这些操作可能只是重复打印。 这是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);
   }
}

我尝试了以下内容,但我现在知道这是一种完全错误的做法。

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

    }
}

我已经阅读了许多帖子和Java Tutorials中的“swing in concurrency”文档。我现在明白了Idid的错误,但我仍然不知道如何开始。 我的(子)目标是让程序继续打印“choice1”(或任何其他动作),并在按下另一个按钮后切换到打印“choice2”。 有人能给我一点正确的方向吗?

1 个答案:

答案 0 :(得分:0)

您很可能需要单独启动Thread。然后,您可以为每个创建一个按钮/ actionlistener。让线程在同一个小部件上输出内容要困难得多,所以假设每个Thread都在控制台上输出:

class SampleThread extends Thread {
    private String myOutput;
    public boolean paused = true;
    public SampleThread(String output) { myOutput = output; }
    public void run() {
        while (true) {
            if (!paused) {
                System.out.println(myOutput);
            }
            Thread.sleep(100);
        }
    }
    public void setPaused(boolean p) { paused = p; }
}

请注意,这只是您可以在没有GUI的情况下启动的内容:

public static void main(String[] args) {
    for (int i = 0; i < 10; i++) {
        SampleThread t = new SampleThread("hello from thread " + i);
        t.setPaused(false);
        t.start();
    }
}

现在,对于Swing控件,您可以为每个按钮创建一个带有动作侦听器的按钮:

Colletion<SampleThread> threads = new ArrayList<>(); // all threads
public void init(int count) {
    for (int i = 0; i < count; i++) {
        JButton button = new JButton("activate thread " + i);
        String output = "thread " + i;
        SampleThread thread = new SampleThread(output);
        thread.start();
        threads.add(thread);
        button.addActionListener() {
            public void actionPerformed(ActionEvent e) {
                // pause all threads except the one we just created
                threads.forEach(t -> t.setPaused(t != thread));
            }
        }
        add(button);
    }
}