首页 > 解决方案 > JFrame等待用户输入

问题描述

我有一个程序,我需要通过 Jframe 窗口从用户那里获取输入。它就像一个 while 循环,我在其中调用 Jframe 窗口并从用户那里获取输入。但是 JFrame 窗口不会等待输入并运行循环,直到它应该运行的最后一次并且只有在那里等待输入。

我想让 Jframe 窗口在 while 循环的每次迭代中等待输入。有没有办法做到这一点?

...

/**
 * Create the frame.
 */
public Janela3(KieSession kSession) {
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setBounds(100, 100, 462, 447);
    contentPane = new JPanel();
    contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
    setContentPane(contentPane);
    kSession.setGlobal("J3", this);
    contentPane.setLayout(null);
    JPanel panelMain = new JPanel();
    panelMain.setBackground(new Color(248, 148, 6));
    panelMain.setBounds(0, 0, 448, 44);
    contentPane.add(panelMain);
    tituloJanela = new JLabel();
    tituloJanela.setFont(new Font("Tahoma", Font.BOLD, 24));
    tituloJanela.setForeground(new Color(255, 255, 255));
    panelMain.add(tituloJanela);
    kSession.setGlobal("TJ3", tituloJanela);
    JPanel childPugh = new JPanel();
    childPugh.setBounds(0, 44, 448, 364);
    ...




}

标签: javaswinginputjframe

解决方案


如果我是正确的,您有一个 while 循环,您可以在其中获取用户输入,但如果还没有输入,循环可能不会继续?目前您提供的信息太有限,无法给出具体答案,但我会试一试。

您可以通过使用另一个等待输入的 while 循环来解决这个问题。检查下面的“伪半真实代码”。

input = getInput();
while (input == null)
{
   input = getInput();
}

推荐阅读