2013-07-26 99 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中。