2016-11-29 231 views
1

所以我在eclipse上使用WindowBuilder來設計我的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(); 
} 

任何幫助,將不勝感激。非常感謝你!

回答

2

您絕對不會在頂層窗口ServerFrame.this JFrame上調用setVisible(true),因此事件線程永遠不會真正啓動。

將所有組件的ServerFrame後,只需調用它setVisible(true)。 .....還是在main方法,這樣做:

new ServerFrame().setVisible(true); 

另一個問題:在空佈局和setBounds()似乎爲Swing像最簡單的和最好的方式新手創建複雜的圖形用戶界面的,更多的搖擺創建GUI'S使用它們時遇到的更嚴重的困難。它們不會在GUI大小調整時調整組件的大小,它們是增強或維護的皇室女巫,當它們放在滾動窗格中時它們會完全失敗,在所有平臺或屏幕分辨率與原始視圖不同時,它們看起來會非常糟糕。

此外,一定要啓動Swing事件線程上的GUI,否則你可能會運行到偶爾的和硬調試線程錯誤。例如,

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

編輯:對不起,當我刷新頁面時,我只看到您的回覆中的第一段。我現在用你的建議修復我的代碼,看看它是否能解決問題,謝謝。 – Calvin

+0

請看我編輯的評論,這是我的錯誤,對不起! – Calvin

+1

@Calvin:好的,請回到我這裏。此外,您正在使用FlowLayout,這是一種不尊重組件「界限」的佈局。再次,我敦促您瞭解並使用佈局管理器。它會讓你的代碼更容易調試。 –

相關問題