我需要按住此窗口,直到按下完成按钮:/

时间:2013-02-13 23:03:41

标签: java swing user-interface actionlistener

这里的第一个代码来自主代码的预先记录,我无法弄清楚。

boolean CompanyLoaded, StartingNum = true;

FirstCheck newwin = new FirstCheck();

public CollectNumbers() { 

        if (StartingNum = true) firstCheck = newwin.FirstCheck(); <----Runs FirstCheck okay

        if (CompanyLoaded = true) LoadCompany();

从现在开始,我有我的FirstCheck课程,我将在下面发布。我已经尝试过抽象覆盖actionPerformed,我已经尝试了线程睡眠,等待,并且无法弄清楚如何让我的FirstCheck()方法等到actionPerformed()返回我的字符串(Int)。任何帮助将不胜感激!

import java.awt.event.*;

import java.awt.*;

import javax.swing.*;

import java.io.*;

import java.util.*;

abstract class FirstCheck extends JFrame implements ActionListener {

    static int num;
    static boolean bnum = true;
    JTextField NumberEntry;

    Toolkit   tools   = Toolkit.getDefaultToolkit();
    Dimension windowLocVar = tools.getScreenSize(); 


    public int FirstCheck() {

        JFrame frame = new JFrame();

        NumberEntry = new JTextField();

        JButton Done = new JButton("Done");
        Done.addActionListener(this);

        JLabel label = new JLabel("Starting Check Number?");
        label.setVerticalTextPosition(JLabel.BOTTOM);  
        label.setHorizontalTextPosition(JLabel.CENTER);

        JPanel panel = new JPanel();
        panel.setLayout(new GridLayout(3,1));
        panel.add(NumberEntry);
        panel.add(label);
        panel.add(Done); 

        frame.add(panel);                       
        frame.pack();                                      
        frame.setVisible(true);                            
        frame.setSize(250,150);                            
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
        frame.setLocation(windowLocVar.width/2-300,windowLocVar.height/2-100);


        //try {
        //  Thread.sleep(5000);
        //  } 
        //catch(InterruptedException e) {
        //  // Restore the interrupted status
        //  Thread.currentThread().interrupt();
        //  }



    }



    public int actionPerformed(ActionEvent e) {
               bnum = false;
               num = Integer.parseInt(NumberEntry.getText());
               return num;

    }


    }

1 个答案:

答案 0 :(得分:2)

而不是JFrame frame = new JFrame()使用JDialog frame = new JDialog((Frame)null, true)

这将创建一个模态对话框,在frame.setVisible(true)点阻止代码执行,直到对话框关闭。

请查看How to use dialogs了解详情。

actionPerformed方法中,您需要存储一个“返回”值,一旦对话框关闭,您的应用程序就可以查询该值...

public void actionPerformed(ActionEvent e) {
    bnum = false;
    num = Integer.parseInt(NumberEntry.getText());
    Object source = e.getSource();
    if (source instanceof Component) {
        // Close the dialog....
        SwingUtilities.getWindowAncestor((Component)source).dispose();
    }
}

<强>更新

同样,您可以使用JOptionPane代替......

看看

作为几个例子