2017-06-01 17 views
0

出於測試目的,我在ClientGUI類中創建了一個GUI。該程序似乎永遠不會退出並hangs.I在main調用我的創建函數,看看gui如何看起來,在此之後程序應該終止,但它掛起。請向我解釋爲什麼發生這種情況。程序在創建GUI時不會退出

import java.awt.*; 
import java.awt.event.ActionEvent; 
import java.awt.event.ActionListener; 
import javax.swing.*; 
import java.io.*; 
import java.net.*; 

public class ClientGUI extends JFrame{ 

// creates username textfield 
private JLabel username = new JLabel("Username "); 
private JTextField textUsername = new JTextField(10); 

//creates password textfield 
private JLabel password = new JLabel("Password "); 
private JPasswordField passText = new JPasswordField(10); 

// adding two buttons 
private JButton Login = new JButton("Login"); 
private JButton Create = new JButton("Create"); 

private JPanel error; 
private JFrame my_error; 
private JLabel errormsg = new JLabel("Error Try Again"); 
private JButton cancel_me = new JButton("OK"); 
private String option; 
private String usr_name; 
private char [] ident; 
private String pass; 
private String passme; 
public volatile boolean []go = new boolean[1]; 

// constructor 
ClientGUI(){ 

    super("GossApp"); //title of jframe 

     } 

public void CreatGui(){ 

    JPanel myPanel = new JPanel(new GridBagLayout()); // creates a panel of type gridbaglayout 
    GridBagConstraints GridC = new GridBagConstraints();// constraints for layout 
    GridC.insets = new Insets(0,10,0,10); // spacing 

    // position 0,0 is corner 
    GridC.gridx = 0; 
    GridC.gridy = 0; 
    myPanel.add(username, GridC); // adding to panel 

    // position 0,1 adding user text field below username 
    GridC.gridy = 1; 
    myPanel.add(textUsername, GridC); 

    // position 0, 2 adding password name below user 
    GridC.gridx = 0; 
    GridC.gridy = 2; 
    myPanel.add(password, GridC); 

    //position 0,3 adding password text field below password 
    GridC.gridy = 3; 
    myPanel.add(passText, GridC); 

    // adding login button to panel next to password text 
    GridC.gridx = 1; 
    GridC.gridy = 3; 
    myPanel.add(Login, GridC); 

    Login.addActionListener(new ActionListener() { 
     @Override 
     public void actionPerformed(ActionEvent e) { 

      /* 
      String my_name = textUsername.getText(); 
      char [] my_password = passText.getPassword(); 
      String my_stringword = new String(my_password); 
      */ 
      option ="log"; 
      whileLog(option); 
     } 
    }); 

    // adding create button gui next to user text field 
    GridC.gridx = 1; 
    GridC.gridy = 1; 
    myPanel.add(Create,GridC); 

    Create.addActionListener(new ActionListener() { 
     @Override 
     public void actionPerformed(ActionEvent e) { 

      option= "create"; 
      whileLog(option); 
     } 
    }); 

    add(myPanel); //adds gui to jframe 
    setSize(270,170); // creates dimensions of frame 
    setLocationRelativeTo(null); // center gui 
    setVisible(true); // I can see! 

    my_error = new JFrame("Error"); 
    error = new JPanel(new GridBagLayout()); 
    GridC.gridx= 1; 
    GridC.gridy = 1; 

    error.add(cancel_me,GridC); 
    cancel_me.addActionListener(new ActionListener() { 
     @Override 
     public void actionPerformed(ActionEvent e) { 
      my_error.setVisible(false); 
     } 
    }); 

    GridC.gridy = 2; 
    error.add(errormsg,GridC); 
    my_error.add(error); 
    my_error.pack(); 
    my_error.setLocationRelativeTo(null); 
    my_error.setVisible(false); 

} 



public class clientserver{ 

public static void main(String[]args) { 

    ClientGUI my_gui = new ClientGUI(); 
    my_gui.CreatGui(); 
} 
} 
+0

我懷疑'whileLog'被阻斷EDT - 參見[在Swing併發(https://docs.oracle.com/javase/tutorial/uiswing/concurrency/ )爲「爲什麼」和[工作線程和SwingWorker](https://docs.oracle.com/javase/tutorial/uiswing/concurrency/worker.html)爲可能的「如何」 – MadProgrammer

+1

可能是因爲'my_error'' JFrame將永遠不可見,而不是被摧毀,這將保持'JVM'運行。你也需要爲你的'JFrame'設置'JFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE)'來關閉它,否則關閉它會使得配置不會退出 – SomeJavaGuy

+0

我試着在日誌中註釋掉並得到了相同的結果 – JavaKiller

回答

1

你需要設置

setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 

爲了您ClientGui,否則在默認踢,這使得它沒有真正接近。由於JFrame未被正確銷燬,默認情況下JVM仍在運行。

methods documentation的另一個小段落,它顯示了默認值是什麼。

[...]

HIDE_ON_CLOSE(在WindowConstants中定義):自動隱藏框架調用任何註冊的WindowListener對象之後。

[...]

EXIT_ON_CLOSE(在JFrame中定義):退出使用系統退出方法中的應用。僅在應用程序中使用它。

[...]

的值設置爲默認HIDE_ON_CLOSE。對此屬性值的更改會導致觸發屬性更改事件,並且屬性名稱爲「defaultCloseOperation」。

[...]