2013-07-26 97 views
3

應該使用什麼樣的佈局來創建頁面像這樣: 它應該是可調整大小的 它有兩個主面板Right and Left框架的最佳佈局

Example of GUI

+2

使用'BorderLayout'。將左側面板放在'CENTER'位置和右側'EAST'中。 – Reimeus

+0

@Reimeus我會在'EAST'上使用'LINE_END'來解釋不同的區域設置。 –

+0

以及什麼樣的佈局右鍵讓按鈕垂直對齊? –

回答

3

額外的空間將被賦予「主文本」文本區域,並將額外的高度賦予按鈕面板,同時居中它們。

End-Of-Line Button Layout

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

public class EndOfLineButtonLayout { 

    public static void main(String[] args) { 
     Runnable r = new Runnable() { 

      @Override 
      public void run() { 
       // the GUI as seen by the user (without frame) 
       JPanel gui = new JPanel(new BorderLayout()); 
       gui.setBorder(new EmptyBorder(2, 3, 2, 3)); 

       JPanel textPanel = new JPanel(new BorderLayout(5,5)); 
       textPanel.add(new JScrollPane(new JTextArea("Top Text",3,20)), 
         BorderLayout.PAGE_START); 
       textPanel.add(new JScrollPane(new JTextArea("Main Text",10,10))); 
       gui.add(textPanel, BorderLayout.CENTER); 

       JPanel buttonCenter = new JPanel(new GridBagLayout()); 
       buttonCenter.setBorder(new EmptyBorder(5,5,5,5)); 
       JPanel buttonPanel = new JPanel(new GridLayout(0,1,5,5)); 
       for (int ii=1; ii<6; ii++) { 
        buttonPanel.add(new JButton("Button " + ii)); 
       } 
       // a component added to a GBL with no constraint will be centered 
       buttonCenter.add(buttonPanel); 

       gui.add(buttonCenter, BorderLayout.LINE_END); 

       JFrame f = new JFrame("Demo"); 
       f.add(gui); 
       // Ensures JVM closes after frame(s) closed and 
       // all non-daemon threads are finished 
       f.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); 
       // See http://stackoverflow.com/a/7143398/418556 for demo. 
       f.setLocationByPlatform(true); 

       // ensures the frame is the minimum size it needs to be 
       // in order display the components within it 
       f.pack(); 
       // should be done last, to avoid flickering, moving, 
       // resizing artifacts. 
       f.setVisible(true); 
      } 
     }; 
     // Swing GUIs should be created and updated on the EDT 
     // http://docs.oracle.com/javase/tutorial/uiswing/concurrency/initial.html 
     SwingUtilities.invokeLater(r); 
    } 
} 
0

您可以使用網格包佈局,嘗試使用NetBeans,我已經試了一下,發現真的有用。 一旦你用netbeans創建它,你可以使用相同的東西,並構建任何類型的佈局。

與其他解決方案好運。

p.s.邊界佈局非常適合您的需求,但我提到這一點以防萬一您想要做更多。

0

我會使用BorderLayout。

創建三個JPanels並將它們添加到一個JFrame如下:

public class YourClass extends JFrame{ 
//code here 
this.setLayout(new BorderLayout()); 
this.add(TopPanel, BorderLayout.NORTH); 
this.add(RightPanel, BorderLayout.EAST); 
this.add(MainPanel, BorderLayout.CENTER); 
this.pack(); 
this.setVisible(true); 
+0

我不認爲這會給他的東西從應用程序的頂部到應用程序底部的位置尋找效果。 –

+0

有一個構造函數可以將水平和垂直間隙設置爲負值以使組件重疊。我建議玩這些價值觀。 BorderLayout(int hgap,int vgap) – Isaac

+0

如果你的NorthPanel是一個JTextArea,並且你的EastPanel是一組按鈕,EastPanel不會重疊JTextArea中的文本嗎? –

0

的兩個主面板將被放置使用BorderLayout的主要的JPanel內。左側面板將使用BorderLayout.CENTER放置,右側面板將使用BorderLayout.LINE_END放置。

左側面板將使用BoxLayout,Y軸來分隔左側面板中的兩個JPanel。

右側按鈕面板將使用GridBagLayout。這使按鈕的大小相同,並允許您使用Insets向按鈕添加一些間距。

按鈕將從右側按鈕面板的頂部到底部間隔開。如果您希望所有按鈕都朝向右側按鈕面板的頂部,則可以使用FlowLayout將右側按鈕面板放入另一個JPanel中。