使用 GUI 进行 Java swing 切换进程

标签 java swing jframe

我想制作一个具有两个(或多个)按钮的 Swing 应用程序,这些按钮可以在正在进行的操作之间切换。为了简单起见,这些 Action 可以只是重复打印。 这是 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 教程中的“swing 中的并发”文档。我现在明白我做错了什么,但我仍然不知道如何开始。 我的(子)目标是让程序连续打印“choice1”(或任何其他操作),并在按下其他按钮后切换到打印“choice2”。 有人可以给我一点插入力,让我朝着正确的方向前进吗?

最佳答案

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

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

关于使用 GUI 进行 Java swing 切换进程,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46219275/

相关文章:

java - Elasticsearch - 分配分片

java - 如何从 Context 获取 FREContext?

java - 如果程序仅通过 doClick() 进行交互,则 JFrame 永远不会呈现;

java - JFrame 滚动条和 Canvas

Java JFrame 弹出窗口排序(应始终位于顶部但位于旧 JFrames 之后)

java - 单击 JCheckBox 时添加值

java - 使用类方法进行除法始终为零

java - 编写连接到网站并执行某些操作的宏之类的代码的最佳语言是什么?

java - 如何将 ImageIcon 添加到 JLabel

java - JFrame 上绘制的点的总距离