问题描述
这是我的GUI
- 在开始窗口期间,将打开并输入主机,端口和持续时间。
- 单击“连接”按钮将关闭窗口。
public class Frame1 {
public String userText;
public String pwdText;
public JFrame frame;
public JTextField textField;
public JTextField textField_1;
public JTextField textField_2;
public JLabel lblNewLabel;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokelater(new Runnable() {
public void run() {
try {
Frame1 window = new Frame1();
window.frame.setVisible(true);
} catch (Exception e) {
e.printstacktrace();
}
}
});
}
/**
* Create the application.
*/
public Frame1() {
initialize();
}
/**
* Initialize the contents of the frame.
*/
public void initialize() {
frame = new JFrame();
frame.getContentPane().setBackground( new Color(240,240,240));
frame.setBounds(100,100,450,300);
frame.setDefaultCloSEOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().setLayout(null);
JButton btnNewButton = new JButton("Connect");
btnNewButton.setForeground(Color.BLACK);
btnNewButton.setBackground(Color.GRAY);
btnNewButton.setFont(new Font("Tahoma",Font.BOLD,16));
btnNewButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
//messape
userText = textField.getText();
pwdText = textField_1.getText();
System.out.println(userText);
if(e.getSource()==btnNewButton) {
frame.dispose();
// This exit Your GUI
}
}
});
这是我的主程序,在其中我调用 Connection2 中的 Frame1 。
public class Connection2{
// public static final String HOST = "localhost";
// public static final String PORT = "5555";
public static String HOST;
public static String PORT;
public static long START;
public static long END;
public static void main(String[] args) throws InterruptedException,ClassNotFoundException,sqlException,IOException {
Frame1 frame = new Frame1();
Scanner sc = new Scanner(system.in);
System.out.println("JMX CONNECTION:");
System.out.println("HOST AND PORT");
frame.main(args);
HOST =frame.userText;
PORT =frame.pwdText;
Thread.sleep(10000);
System.out.println(HOST);
System.out.println(PORT);
Thread.sleep(20000);
System.out.println(HOST);
System.out.println("TEST DURATION IN SECONDS");
System.out.println("DURATON(sec): ");
int Duration = sc.nextInt();
JMX CONNECTION
HOST AND PORT
localhost
null
null
在给线程暂停时间之后,此输出即时消息。
解决方法
在此代码中,它不会等到用户按下按钮后才返回空值。
因此,解决此问题的一种可能方法是在连接类中添加一个在按下按钮时调用的方法