我目前正在開發聊天程序的登錄表單,並希望程序加載框架並等待用戶輸入。 不幸的是程序打開框架,但同時恢復主要方法。 我希望你有一些想法來幫助我。JFrame - 等待用戶按下按鈕
問候
public static void main(String[] args){
boolean running = true;
//Starting JFrame
chatFrame.loginFrame();
//Processing - Receiving Status from Login method
if(getStatus() == 1){
...
} else {
System.out.println("An Error occured..");
System.exit(0);
}
}
JFrame類:
public class chatFrame{
private static String sLogin;
private static String password;
public static void loginFrame(){
System.out.println("Launching Frame");
JFrame loginFrame = new JFrame();
loginFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JTextField user = new JTextField("Username");
JTextField pass = new JTextField("Password");
JButton login = new JButton("Login");
loginFrame.setLayout(new BorderLayout());
loginFrame.add(user, BorderLayout.NORTH);
loginFrame.add(pass, BorderLayout.CENTER);
loginFrame.add(login, BorderLayout.SOUTH);
loginFrame.pack();
loginFrame.setSize(250, 150);
loginFrame.setVisible(true);
login.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
System.out.println("Action performed");
String sLogin = user.getText();
String password = pass.getText();
//Calling Login method
ClEngine.login(sLogin, password);
System.out.println("dataIn:" + dataIn);
loginFrame.setVisible(false);
}
});
}
}
參見[多個JFrames,好/壞習慣的用?(http://stackoverflow.com/q/9554636/418556) –