java - 应用程序打开后立即关闭

标签 java swing sockets user-interface chat

所以我在 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) 即可。 .....或者在主方法中,执行以下操作:

new ServerFrame().setVisible(true);

另一个问题:虽然空布局和 setBounds() 对于 Swing 新手来说似乎是创建复杂 GUI 的最简单、最好的方法,但您创建的 Swing GUI 越多,遇到的困难就越严重使用它们时。当 GUI 调整大小时,它们不会调整组件的大小,它们是增强或维护的皇家女巫,当放置在滚动 Pane 中时它们完全失败,当在与原始平台或屏幕分辨率不同的所有平台或屏幕分辨率上查看时,它们看起来非常糟糕.

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

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

关于java - 应用程序打开后立即关闭,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40856347/

相关文章:

带数据库的 Java 套接字

java - 有没有办法重命名用 lombok 生成的 getter 方法?

java - Selenium 。如何切换到URL中包含某个子字符串的窗口?

java - 如何使用Java Swing实现多 View ?

java - 无法缓慢移动面板

linux - 持有TCP连接的大规模websocket系统的设计

java - Java 类之间的共享数组列表

java - JPA 无法使用 START_OBJECT TOKEN 作为搜索键

java - jFrame 无法正确打开或显示内容

android - 如何在 Android 中创建 Socket 连接?