程序不会在GUI创建时退出

时间:2017-06-01 07:24:37

标签: java swing

出于测试目的,我在ClientGUI类中创建了一个GUI。该程序似乎永远不会退出并挂起。我在main中调用我的create函数来查看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();
}
}

1 个答案:

答案 0 :(得分:1)

你需要设置

setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

对于你的ClientGui,否则默认会启动,这使得它不会非常接近。默认情况下,JVM保持运行,因为JFrame未正确销毁。

methods documentation中的另一个小段落,显示默认值。

  

[...]

     

HIDE_ON_CLOSE(在WindowConstants中定义):在调用任何已注册的WindowListener对象后自动隐藏框架。

     

[...]

     

EXIT_ON_CLOSE(在JFrame中定义):使用System exit方法退出应用程序。仅在应用程序中使用它。

     

[...]

     

默认情况下,该值设置为HIDE_ON_CLOSE。对此属性值的更改会导致触发属性更改事件,属性名称为“defaultCloseOperation”。

     

[...]