申请在开业后立即关闭

时间:2016-11-29 01:28:43

标签: 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();
}

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

1 个答案:

答案 0 :(得分:2)

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

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

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);
    });
}