【问题标题】:Checking options in empty Infinite loop vs dosomething infite loop检查空无限循环中的选项与做一些无限循环
【发布时间】:2020-12-18 09:26:30
【问题描述】:
public static void main(String... s) {
        StartUp obj = new StartUp();
          
        while(true) {
            //System.out.println("Option - " + option);
            if(option == 1) {
                option = 0;
                obj.setVisible(false);
                obj.dispose();
                new Test();
                break;
            }else if(option == 2) {
                option = 0;
                obj.setVisible(false);
                obj.dispose();
                new PWorld.Splash().load();
                break;
            }
        }
    }
    

我需要将 System.out.println("Option - " + option); 放入 while 循环中才能使其工作,否则程序在运行 StartUp obj = new StartUp(); 后会冻结;

option 是 StartUp 类中的静态 int,由 Actionlistener 更改 option 中的值由 ActionListener 更改,但 while 循环似乎不起作用。

但是如果我将System.out.println("Option - " + option); 放在while 循环中,它就可以工作。 为什么!

我正在使用这个 while 循环,因为 new PWorld.Splash().load(); 具有 Thread.sleep(),如果从具有 Thread 的 ActionListener(在 UI 线程中)调用,则不会绘制 as in this answere 新的 JFrame。

谢谢

【问题讨论】:

  • 不管用 println 延迟循环是否有助于它工作,我能给你的主要建议是:完全摆脱循环。您正在使用事件驱动的 GUI 库,因此无需循环,相反,您应该编写代码以基于程序状态(由对象的关键字段保存的值)的行为来响应事件。
  • @DontKnowMuchBut 越来越好我不能直接在 ActionListener 中调用方法,因为例如 new PWorld.Splash().load(); 使用的线程会干扰 ActionListener 的 UI 线程
  • 不,事实并非如此。可以在 ActionListeners 中创建新线程,事实上我一直都在这样做。这个问题看起来是xy problem 类型的问题,您可能问错了问题。正确的方法是如何在不干扰 GUI 功能的情况下将此代码正确集成到 GUI 中,要回答这个问题,您需要提供更多详细信息,并可能发布有效的 minimal reproducible example
  • 你可能希望edit 改进问题,告诉这些细节,因为我认为你的整个方法需要改变,包括摆脱while循环。
  • 我的答案中发布的示例代码。如果您有任何问题,请检查,运行,评论

标签: java swing awt actionlistener infinite-loop


【解决方案1】:

您的问题是:

  • 您正在调用一个“紧密”循环,它会占用 CPU 并阻止其他代码运行。 System.out.println(...) 语句添加了减慢此循环的代码,将 CPU 从紧密循环的钳口中释放出来,允许其他线程运行,这就是您的问题的根源。
  • 话虽如此,您的编码方法并不好,因为您使用 while (true) 循环代替响应事件,这就是 Swing GUI 的编码方式。
  • 您说原因是while循环中的一段代码调用了Thread.sleep,并且如果在Swing事件线程(例如在ActionListener中)调用此代码,将阻塞事件线程,冻结您的 GUI - 都是真的。
  • 但是您的解决方案是错误的。正确的解决方案不是在 main 方法的 while (true) 循环中调用它,而是从后台线程调用 Thread.sleep,例如在 SwingWorkerdoInBackground() 方法中(链接是教程),或者更好的是,使用Swing Timer(同样,链接是教程)代替Thread.sleep。这将允许您的代码在不阻塞 Swing 事件线程的情况下暂停一些代码。
  • 如果您需要显示对话框(子)窗口,另一种选择是使用模态 JDialog 显示窗口,同时阻止与主 GUI 窗口的交互,直到对话框窗口不再可见。

再次,如需更详细和全面的解决方案,请考虑创建并发布您的Minimal, Reproducible Example 程序并附上您的问题。

例如,这是我的 Minimal, Reproducible Example:

import java.awt.Dialog.ModalityType;
import java.awt.Dimension;
import java.awt.Color;
import java.awt.GridBagLayout;
import java.awt.GridLayout;
import java.awt.Window;
import javax.swing.*;

public class MinReproExample {
    public static void main(String[] args) {
        SwingUtilities.invokeLater(() -> {
            Startup startup = new Startup();
            startup.showStartUp();

            Option option = startup.getOption();
            if (option == Option.TEST) {
                JOptionPane.showMessageDialog(null, "Test selected", "Selection", JOptionPane.DEFAULT_OPTION);
            } else if (option == Option.PWORLD) {
                PWorld pworld = new PWorld();
                pworld.showSplash();
            }   
        });
    }
}
class Startup {
    private JDialog startupDialog;
    private Option option = null;
    
    public Startup() {
        ButtonGroup buttonGroup = new ButtonGroup();
        JPanel optionsPanel = new JPanel(new GridLayout(1, 0, 10, 10));
        optionsPanel.setBorder(BorderFactory.createTitledBorder("Options"));
        for (final Option op : Option.values()) {
            JRadioButton rBtn = new JRadioButton(op.getText());
            rBtn.setActionCommand(op.getText());
            optionsPanel.add(rBtn);
            buttonGroup.add(rBtn);
            rBtn.addActionListener(e -> {
                option = op;
                Window window = SwingUtilities.getWindowAncestor(optionsPanel);
                window.dispose();
            });
        }
        
        startupDialog = new JDialog(null, "Select Option", ModalityType.APPLICATION_MODAL);
        startupDialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
        startupDialog.add(optionsPanel);
        startupDialog.pack();
        startupDialog.setLocationRelativeTo(null);
    }
    
    public void showStartUp() {
        if (startupDialog != null) {
            startupDialog.setVisible(true);
        }
    }
    
    public Option getOption() {
        return option;
    }
}
class PWorld {
    private static final Color ROBINS_EGG_BLUE = new Color(0, 204, 204);
    private JDialog pworldSplashDialog;
    private JFrame mainPWorldFrame;

    public PWorld() {
        JLabel splashLabel = new JLabel("Splash Window", SwingConstants.CENTER);
        JPanel splashPanel = new JPanel(new GridBagLayout());
        splashPanel.add(splashLabel);
        splashPanel.setBackground(Color.PINK);
        splashPanel.setPreferredSize(new Dimension(300, 250));

        pworldSplashDialog = new JDialog(null, "Splash", ModalityType.MODELESS);
        pworldSplashDialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
        pworldSplashDialog.add(splashPanel);
        pworldSplashDialog.pack();
        pworldSplashDialog.setLocationRelativeTo(null);

        JLabel mainLabel = new JLabel("Main GUI Window", SwingConstants.CENTER);
        JPanel mainPanel = new JPanel(new GridBagLayout());
        mainPanel.add(mainLabel);
        mainPanel.setBackground(ROBINS_EGG_BLUE);
        mainPanel.setPreferredSize(new Dimension(500, 350));

        mainPWorldFrame = new JFrame("Main PWorld GUI");
        mainPWorldFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        mainPWorldFrame.add(mainPanel);
        mainPWorldFrame.pack();
        mainPWorldFrame.setLocationRelativeTo(null);

    }

    public void showSplash() {
        int timerDelay = 2000; // two second delay
        Timer timer = new Timer(timerDelay, e -> {
            if (pworldSplashDialog != null && pworldSplashDialog.isVisible()) {
                pworldSplashDialog.dispose();
                showMainPWorldFrame();
            }
        });
        timer.setRepeats(false);
        timer.start();

        pworldSplashDialog.setVisible(true);
    }

    private void showMainPWorldFrame() {
        mainPWorldFrame.setVisible(true);
    }
}
// options to choose from
enum Option {
    TEST("Test"), PWORLD("PWorld");
    private String text;

    private Option(String text) {
        this.text = text;
    }

    public String getText() {
        return text;
    }
}

【讨论】:

  • 谢谢@DontKnowMuchBut 越来越好,You may want to see this
  • @SameerGupta:不,我不需要看那个。 MadProgrammer 忘记的 Swing 比我所知道的还要多,他应该把事情做好。
【解决方案2】:

如果选项在初始输入时不是 1 或 2,这个循环应该做什么?你只是无缘无故地消耗 CPU 周期,等待其他线程做某事。

添加打印语句会注入一点非 CPU 燃烧延迟,在这种情况下,可能要设置“选项”的线程开始运行。

(FWIW,如果您希望更改对其他线程可见,则可能需要将“选项”声明为 volatile)。

这不是一个好的设计。我无法告诉您足够的上下文来告诉您应该做什么,但需要某种体面的通知机制。但这应该回答您的“为什么?”的问题。

【讨论】:

  • 我不能直接在 ActionListener 中调用方法,因为例如new PWorld.Splash().load(); 使用Thread .sleep 会干扰 ActionListener 的 UI 线程并且不会绘制新框架。所以我想出了这个方法
  • ActionListener 是一个接口。我猜您应该实现该接口,然后响应选项there 的任何更改。也就是说,你的设计是颠倒的。你不叫它;它在呼唤你。
猜你喜欢
  • 2016-04-21
  • 1970-01-01
  • 2014-04-12
  • 1970-01-01
  • 2021-05-19
  • 2015-06-25
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多