2012-12-17 95 views
2

由於是初學者,我不想介入佈局管理器,所以我只是在我的主JFrame中添加一個JPanel,併爲面板中的每個組件提供了特定的位置。但不知何故,則輸出方式太錯..將組件添加到JFrame中的JPanel中

frame = new JFrame(email + " (Offline)"); 
    frame.setSize(400, 400); 
    frame.setLocation(0, 0); 
    frame.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE); 
    frame.setLayout(new FlowLayout()); 
    frame.addWindowListener(new WindowAdapter() 
    { 
     public void windowClosing(WindowEvent e) 
     { 
      // out.println("BYE"); 
      // out.flush(); 
      frame.dispose(); 
      thread.stop(); 
     } 
    }); 
    panel = new JPanel(); 
    frame.add(panel); 
    chat = new JTextArea(); 
    chat.setSize(400, 200); 
    chat.setLocation(0, 0); 
    chat.setEditable(false); 
    panel.add(chat); 
    panel.validate(); 
    JLabel you = new JLabel("You:"); 
    you.setSize(you.getPreferredSize()); 
    you.setLocation(0, 210); 
    panel.add(you); 
    panel.validate(); 
    input = new JTextArea(); 
    input.setSize(200, 200); 
    input.setLocation(0, 220 + chat.getSize().height); 
    panel.add(input); 
    panel.validate(); 
    send = new JButton("Send"); 
    send.setSize(send.getPreferredSize()); 
    send.setLocation(210, 220 + chat.getSize().height); 
    panel.add(send); 
    panel.validate(); 
    frame.setVisible(true); 

該幀的結果是文本區域是看不見的,一個你:標籤中間,旁邊就是它右側的按鈕..我是什麼在這裏失蹤?

+5

佈局管理器的全部意義就是讓這個*更輕鬆*。 –

+4

我必須說* amen * @BrianRoach剛發佈的內容。通過避免使用佈局管理器,你正在爲自己做一件大事。看看這個教程(谷歌會幫你找到它們),然後讓他們去......但是現在我要說避免GridBagLayout,因爲它和GroupLayout是最複雜的。 –

+0

噢,我一直在檢查佈局,但它似乎並不像我想要的那樣非常靈活。但幸運的是,對於與我的問題完全相同的人,請將setLayout設置爲null。就像我在我的代碼panel.setLayout(null)。這將允許你自由地給你父母的每個組件。 –

回答

9

同樣,不要使用null佈局,因爲它使更新和維護GUI比應該更加困難,並且如果您打算讓它們在多個平臺上運行,可能會導致難看的GUI。相反

  • 使用幾個JPanels,每一個夾持元件的核心小組,並分別使用其最好的佈局管理器
  • 鳥巢這些JPanels其他JPanels使用最好的佈局管理器來顯示它們
  • ,這將允許您的GUI無需額外的代碼即可調整大小。
  • 將JTextAreas放入JScrollPanes中,以便即使超出文本區域也可以看到所有文本。
  • 永遠不要設置JTextArea的大小,因爲它不允許它滾動。而是設置其列和行。

作爲一個非常簡單的例子,運行這個,看看我的意思是:

import java.awt.*; 
import javax.swing.*; 

public class FooSwing2 { 
    public static void main(String[] args) { 
     JTextArea chatArea = new JTextArea(8, 40); 
     chatArea.setEditable(false); 
     chatArea.setFocusable(false); 
     JScrollPane chatScroll = new JScrollPane(chatArea); 
     JPanel chatPanel = new JPanel(new BorderLayout()); 
     chatPanel.add(new JLabel("Chat:", SwingConstants.LEFT), BorderLayout.PAGE_START); 
     chatPanel.add(chatScroll); 

     JTextField inputField = new JTextField(40); 
     JButton sendBtn = new JButton("Send"); 
     JPanel inputPanel = new JPanel(); 
     inputPanel.setLayout(new BoxLayout(inputPanel, BoxLayout.LINE_AXIS)); 
     inputPanel.add(inputField); 
     inputPanel.add(sendBtn); 

     JPanel youLabelPanel = new JPanel(new FlowLayout(FlowLayout.LEFT, 0, 0)); 
     youLabelPanel.add(new JLabel("You:")); 

     JPanel mainPanel = new JPanel(); 
     mainPanel.setLayout(new BoxLayout(mainPanel, BoxLayout.PAGE_AXIS)); 
     mainPanel.add(chatPanel); 
     mainPanel.add(Box.createVerticalStrut(10)); 
     mainPanel.add(youLabelPanel); 
     mainPanel.add(inputPanel); 

     JFrame frame = new JFrame("Foo"); 
     frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     frame.add(mainPanel); 
     frame.pack(); 
     frame.setLocationRelativeTo(null); 
     frame.setVisible(true); 
    } 
} 

這將導致看上去像這樣一個簡單的(非功能)GUI:
enter image description here

現在說你想改變這個並添加另一個按鈕,一個「退出」JButton在發送JButton的右側。如果您使用空佈局,則必須調整GUI大小,您必須將發送按鈕移到左側,並確保您的數學運算沒有錯誤等。如果您使用佈局管理器,則需要只是代碼兩條新線(改變顯示,當然不是功能):

JTextField inputField = new JTextField(40); 
    JButton sendBtn = new JButton("Send"); 
    JButton exitBtn = new JButton("Exit"); // ***** added 
    JPanel inputPanel = new JPanel(); 
    inputPanel.setLayout(new BoxLayout(inputPanel, BoxLayout.LINE_AXIS)); 
    inputPanel.add(inputField); 
    inputPanel.add(sendBtn); 
    inputPanel.add(exitBtn); // ***** added 

就是這樣,這將顯示:
enter image description here

+5

+1從右邊*「不要使用空白布局」*。 –