在空的无限循环中检查选项与在无限循环中进行检查

问题描述

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(),如果从具有{{1}的ActionListener(在UI线程中)调用as in this answere,则不会绘制新的JFrame }。

谢谢

解决方法

您的问题是:

  • 您正在调用“紧密”循环,该循环占用CPU并阻止其他代码运行。 System.out.println(...)语句添加的代码会减慢该循环的速度,从而使CPU从紧密循环的棘爪中释放出来,并允许其他线程运行,这就是问题的根源。
  • 话又说回来,您的编码方法也不好,因为您使用while (true)循环来代替对事件的响应,而这正是Swing GUI的编码方式。
  • 您指出,这样做的原因是while循环中的一小段代码调用了Thread.sleep,并且如果在Swing事件线程上(例如在ActionListener中)调用了此代码,则该事件将被阻止线程,冻结您的GUI-完全正确。
  • 但是您的解决方案是错误的。正确的解决方案不是在main方法的while (true)循环中调用此方法,而是从后台线程中调用Thread.sleep,例如在{ {3}}(指向教程的链接),或者更好的方法是使用SwingWorker(再次指向教程的链接)代替doInBackground()。这将使您的代码暂停某些代码而不会阻塞Swing事件线程。
  • 如果需要显示对话框(子)窗口,另一个选择是使用模式JDialog来显示窗口,同时阻止与主GUI窗口的交互,直到对话框窗口不再可见。

再次提供更详细,全面的解决方案,请考虑创建并发布您的Swing Timer程序并提出问题。

例如,这是我的最小可复制示例:

Thread.sleep
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,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);
    }
}
,

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

添加print语句会产生一些非CPU刻录延迟,在这种情况下,可能要设置'option'的线程才能运行。

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

这不是一个好的设计。关于上下文,我还不能告诉您应该要做的事情,但是需要某种体面的通知机制。但这应该回答您的“为什么?”问题。