繁体   English   中英

打开后应用程序立即关闭

[英]Application closes immediately after opening

所以我在eclipse上使用WindowBuilder来设计我的GUI。 这样做之后,我尝试对其进行测试,以查看GUI是否正确弹出,但是由于某种原因,应用程序在运行后几乎立即关闭。

这是我的代码:

public class ServerFrame extends JFrame {

private JTextField ServerAddressField;
private JTextField PortNumberField;
private server ChatServer;
private InetAddress ServerAddress ;
private JTextArea ChatBox;
private JTextArea ClientTextArea;
private JTextArea UserText;
private JButton Start, Send;

/**
 * Create the application.
 */

/**
 * Initialize the contents of the frame.
 */
public ServerFrame() {

    setTitle("Server");
    setSize(700,700);
    Container cp = getContentPane();    
    cp.setLayout(new FlowLayout());
    cp.setVisible(true);

    ChatBox = new JTextArea();
    ChatBox.setBounds(20, 30, 497, 259);
    cp.add(ChatBox);

    ClientTextArea = new JTextArea();
    ClientTextArea.setBounds(549, 30, 128, 259);
    cp.add(ClientTextArea);

    UserText = new JTextArea();
    UserText.setBounds(20, 317, 497, 57);
    cp.add(UserText);

    ServerAddressField = new JTextField();
    ServerAddressField.setBounds(107, 414, 130, 26);
    cp.add(ServerAddressField);
    ServerAddressField.setColumns(10);

    JLabel lblNewLabel = new JLabel("Server Address:");
    lblNewLabel.setBounds(6, 417, 110, 21);
    cp.add(lblNewLabel);

    PortNumberField = new JTextField();
    PortNumberField.setBounds(342, 414, 130, 26);
    cp.add(PortNumberField);
    PortNumberField.setColumns(10);

    JLabel lblPortNumber = new JLabel("Port Number:");
    lblPortNumber.setBounds(249, 417, 116, 21);
    cp.add(lblPortNumber);

    Start = new JButton("Connect");
    Start.setBounds(482, 414, 117, 29);
    cp.add(Start);

    Send = new JButton("Send");
    Send.setBounds(529, 332, 117, 29);
    cp.add(Send);

    JLabel lblChatHistory = new JLabel("Chat History");
    lblChatHistory.setBounds(231, 6, 92, 16);
    cp.add(lblChatHistory);

    JLabel lblClientList = new JLabel("Client List");
    lblClientList.setBounds(579, 8, 75, 12);
    cp.add(lblClientList);


    Start.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {

            ChatServer=new server();
            ChatServer.start();

        }
    });
    Send.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
          //  ChatServer.SendMassage(ServerAddress.getHostName()+" < Server > "+UserText.getText());
            UserText.setText("");

        }
    });

    UserText.addKeyListener(new KeyListener(){

        @Override
        public void keyTyped(KeyEvent e) {
            // TODO Auto-generated method stub

        }

        @Override
        public void keyPressed(KeyEvent e) {
            if(e.getKeyCode() == KeyEvent.VK_ENTER){
                e.consume();
                Send.doClick();         
        }
        }

        @Override
        public void keyReleased(KeyEvent e) {
            // TODO Auto-generated method stub

        }

    });


}

public static void main(String[] args) {
    // TODO code application logic here
    new ServerFrame();
}

任何帮助,将不胜感激。 非常感谢你!

您永远不会在顶级窗口ServerFrame.this JFrame上调用setVisible(true) ,因此永远不会真正启动事件线程。

将所有组件添加到ServerFrame之后,只需在其上调用setVisible(true) .....或在main方法中,执行:

new ServerFrame().setVisible(true);

另一个问题:尽管null布局和setBounds()似乎是Swing新手创建复杂GUI的最简单和最佳方法,但您创建的Swing GUI越多,使用它们时就会遇到更大的困难。 当GUI调整大小时,它们不会重新调整组件的大小;它们是需要增强或维护的皇家女巫;放置在滚动窗格中时,它们会完全失败;在所有平台或与原始分辨率不同的屏幕分辨率下查看时,它们看起来都是令人毛骨悚然的。

另外,请确保在Swing事件线程上启动GUI,否则可能会遇到偶发且难以调试的线程错误。 例如,

public static void main(String[] args) {
    SwingUtilities.invokeLater(() -> {
        new ServerFrame().setVisible(true);
    });
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM