2012-03-12 138 views
3

嗨我想添加2 JPanel的JFrame采取JFrame.I的全部宽度和高度设法添加它们与GridBagLayout()但我似乎无法设置。使用的setSize()的JPanels的大小,我也tryied所用ipady和ipadx,而一开始就有些工作我交锋一些按钮的整个布局成为mess.Here后是我的代码:Java 2 JPanel的在一个JFrame布局

  JFrame tradeframe = new JFrame("Trade"); 
      JPanel P1panel = new JPanel();   
      P1panel.setBackground(Color.red); 
      JPanel P2panel = new JPanel(); 
      P2panel.setBackground(Color.BLACK); 


      tradeframe.setVisible(true); 
      tradeframe.setSize(600, 400); 
      tradeframe.setResizable(false); 
      tradeframe.setLocationRelativeTo(null); 
      tradeframe.setLayout(new GridBagLayout()); 

      P1panel.add(new JButton ("P1 Agree")); 

      P2panel.add(new JButton ("P2 Agree")); 


      GridBagConstraints a = new GridBagConstraints(); 
      a.gridx = 0; 
      a.gridy = 0; 
      a.weightx = 360; 
      a.weighty = 300; 
      //a.fill = GridBagConstraints.HORIZONTAL; 
      tradeframe.add(P1panel , a); 

      GridBagConstraints b = new GridBagConstraints(); 
      b.gridx = 1; 
      b.gridy = 0; 
      b.weightx = 360; 
      b.weighty = 300; 
      // b.fill = GridBagConstraints.HORIZONTAL; 
      tradeframe.add(P2panel , b); 

我怎样才能让每个JPanel的宽度是300px,高度是400px?

+1

当您使用LayoutManager时,您不使用setXXXSize(),这是LayoutManager的工作来担心这一点。再次阅读GridBagLayout教程,看看他们告诉你什么值为weightx和weighty :-) – 2012-03-12 13:33:46

回答

4

GridBaglayout你必须设置

  • 填写

  • 的weightx和沉重

  • 的gridx/gridy(取决于定向)

则可能例如

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

public class BorderPanels extends JFrame { 

    private static final long serialVersionUID = 1L; 

    public BorderPanels() { 
     setLayout(new GridBagLayout());// set LayoutManager 
     GridBagConstraints gbc = new GridBagConstraints(); 
     JPanel panel1 = new JPanel(); 
     Border eBorder = BorderFactory.createEtchedBorder(); 

     panel1.setBorder(BorderFactory.createTitledBorder(eBorder, "20pct")); 
     gbc.gridx = gbc.gridy = 0; 
     gbc.gridwidth = gbc.gridheight = 1; 
     gbc.fill = GridBagConstraints.BOTH; 
     gbc.anchor = GridBagConstraints.NORTHWEST; 
     gbc.weightx = gbc.weighty = 20; 
     add(panel1, gbc); // add compoenet to the COntentPane 

     JPanel panel2 = new JPanel(); 
     panel2.setBorder(BorderFactory.createTitledBorder(eBorder, "60pct")); 
     gbc.gridy = 1; 
     gbc.weightx = gbc.weighty = 60; 
     //gbc.insets = new Insets(2, 2, 2, 2); 
     add(panel2, gbc); // add component to the COntentPane 

     JPanel panel3 = new JPanel(); 
     panel3.setBorder(BorderFactory.createTitledBorder(eBorder, "20pct")); 
     gbc.gridy = 2; 
     gbc.weightx = gbc.weighty = 20; 
     gbc.insets = new Insets(2, 2, 2, 2); 
     add(panel3, gbc); 

     setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // important 
     pack(); 
     setVisible(true); // important 
    } 

    public static void main(String[] args) { 
     javax.swing.SwingUtilities.invokeLater(new Runnable() { // important 

      public void run() { 
       BorderPanels borderPanels = new BorderPanels(); 
      } 
     }); 
    } 
} 

在大多数情况下,会更好用另一种LayoutManager

+0

+1,用于实际解释要做什么,而不是使用'setXXXSize(...)'。但是,请阅读GridBagLayout对于重量和重量的说明,“一般来说,重量指定为0.0和1.0作为极值:根据需要使用中间的数字。” – 2012-03-12 13:36:49

+0

@Gagandeep巴厘岛只是简单的黑客,否则调整大小并不是连续的和成比例的,我的好奇心你有另一种方式怎么做(GridBagLayout) – mKorbel 2012-03-12 13:39:08

+0

我刚刚加了个小程序,检查出来了:-) – 2012-03-12 13:58:54

2

在面板对象上调用setPreferredSize(new Dimension(int width, int height));方法。

+1

-1,用于指定'setXXXSize(...)',当你使用Layout时,这个美妙的方法需要什么。布局用于克服这些事情:-) – 2012-03-12 13:34:43

+0

首先在这里阅读http://stackoverflow.com/q/1783793/544983它说setsize是没有布局管理器时使用。请删除downvote。我的回答是正确的。 – Juvanis 2012-03-12 13:38:46

+0

@deporter +1我同意。在使用preferredSize和权重方面似乎存在一些意见上的差异。它从来没有那么清晰,取决于布局经理。例如对于文本标签,为什么不让组件决定大小 - 它知道自己的字体等等,对于固有的基于像素的小部件(例如视频回放,图像查看器)也是如此。等等。 – Adam 2012-03-12 13:45:35

3
JFrame tradeframe = new JFrame("Trade"); 
    JPanel P1panel = new JPanel();   
    P1panel.setBackground(Color.red); 
    JPanel P2panel = new JPanel(); 
    P2panel.setBackground(Color.BLACK); 
    tradeframe.setSize(600, 400); 
    tradeframe.setResizable(false); 
    tradeframe.setLocationRelativeTo(null); 

    Box content = new Box(BoxLayout.X_AXIS); 

    P1panel.add(new JButton ("P1 Agree")); 

    P2panel.add(new JButton ("P2 Agree")); 

    content.add(P1panel); 
    content.add(P2panel); 

    tradeframe.setContentPane(content); 
    tradeframe.setVisible(true); 
+0

你似乎对BoxLayout :-)很感兴趣,但是你给出的想法无疑会起作用,+1 :-) – 2012-03-12 13:38:30

+0

+1正确使用正确的LayoutManager – mKorbel 2012-03-12 13:40:22

+0

每个人似乎都在使用GridBag,这非常糟糕痛苦使用。我只是启发他们:-)我也喜欢BorderLayout。您可以使用Box和Border布局和嵌套面板创建所有可能的布局。 – 2012-03-12 13:41:22

0

您正在使用setSize()而不是setPreferredSize()。差异是有点误导,我会认为这是一个在Java中的陷阱。关于两者之间有什么区别的更多信息here.

如果您是Java的新手,我链接的文章有一些其他陷阱/陷阱和一个有用的阅读。

2

下面是做到这一点的方式:

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

public class GridBagLayoutTest 
{ 
    public GridBagLayoutTest() 
    { 
     JFrame frame = new JFrame("GridBag Layout Test"); 
     frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     frame.setLocationByPlatform(true); 

     Container container = frame.getContentPane(); 
     container.setLayout(new GridBagLayout()); 

     JPanel leftPanel = new JPanel(); 
     leftPanel.setBackground(Color.WHITE); 
     GridBagConstraints gbc = new GridBagConstraints(); 
     gbc.gridx = 0; 
     gbc.gridy = 0; 
     gbc.weightx = 0.5; 
     gbc.weighty = 1.0; 
     gbc.anchor = GridBagConstraints.FIRST_LINE_START; 
     gbc.fill = GridBagConstraints.BOTH; 

     container.add(leftPanel, gbc); 

     JPanel rightPanel = new JPanel(); 
     rightPanel.setBackground(Color.BLUE); 
     gbc = new GridBagConstraints(); 
     gbc.gridx = 1; 
     gbc.gridy = 0; 
     gbc.weightx = 0.5; 
     gbc.weighty = 1.0; 
     gbc.anchor = GridBagConstraints.FIRST_LINE_END; 
     gbc.fill = GridBagConstraints.BOTH; 

     container.add(rightPanel, gbc); 

     frame.setSize(600, 400); 
     frame.setVisible(true); 
    } 

    public static void main(String... args) 
    { 
     Runnable runnable = new Runnable() 
     { 
      public void run() 
      { 
       new GridBagLayoutTest(); 
      } 
     }; 
     SwingUtilities.invokeLater(runnable); 
    } 
} 

OUTPUT:

GRIDBAG LAYOUT TEST