2012-03-12 499 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