2013-04-27 110 views
5

我使用Border Layout創建了簡單的應用程序,並在其中添加了兩個按鈕和JTable。我在button2和JTable之間使用JSplitPane。我想重新定義位於button1的塊的默認大小。我該如何解決這個任務?無法更改BorderLayout中塊的大小

這裏是我的代碼:

package test; 

import java.awt.BorderLayout; 
import java.awt.Dimension; 
import javax.swing.JButton; 
import javax.swing.JFrame; 
import javax.swing.JPanel; 
import javax.swing.JScrollPane; 
import javax.swing.JSplitPane; 
import javax.swing.JTable; 
import javax.swing.SwingUtilities; 

public class Sample { 

    public Sample() { 
     JFrame app = new JFrame("Sample"); 
     app.setSize(new Dimension(800,600)); 

     JPanel panel = new JPanel(); 
     app.add(panel); 

     BorderLayout borderlayout = new BorderLayout(); 
     panel.setLayout(borderlayout); 

     JButton but1 = new JButton("1"); 
     JButton but2 = new JButton("2"); 
     but2.setMinimumSize(new Dimension(250,0)); 
     String[] colNames = {"Name","Number","Scores"}; 
     Object[][] data = { 
       { "Mark",11,12}, 
       {"Tommy",23,34}, 
       {"John",34,45} 
     }; 
     JTable table = new JTable(data, colNames); 

     JScrollPane scrollpane = new JScrollPane(table); 
     JSplitPane jsplitpane = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT,but2,scrollpane); 

     panel.add(but1,BorderLayout.NORTH); 
     panel.add(jsplitpane,BorderLayout.CENTER); 

     app.setVisible(true); 
    } 

    public static void main(String[] args) { 
     SwingUtilities.invokeLater(new Runnable() { 
      public void run() { 
       new Sample(); 
      } 
     }); 
    } 
} 

enter image description here

+0

中號唉,請您詳細說明一下您的需求,以便有人在這些問題上回答您:-)一張您想要的圖像,一張粗略的草圖,就是創建想法所需的一切,一條必須遵循的線路瞭解問題的需要。 +1對於SSCCE – 2013-04-27 14:05:55

回答

4

首選大小,那麼我想這個Layout併爲你做這項工作,如粘貼下面的代碼示例說明:-)

import java.awt.BorderLayout; 
import java.awt.GridLayout; 
import java.awt.GridBagConstraints; 
import java.awt.GridBagLayout; 
import java.awt.Dimension; 
import javax.swing.BorderFactory; 
import javax.swing.JButton; 
import javax.swing.JFrame; 
import javax.swing.JPanel; 
import javax.swing.JScrollPane; 
import javax.swing.JSplitPane; 
import javax.swing.JTable; 
import javax.swing.SwingUtilities; 

public class Sample { 

    public Sample() { 
     JFrame app = new JFrame("Sample"); 
     app.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); 
     app.setSize(new Dimension(800,600)); 

     JPanel panel = new JPanel(); 
     panel.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5)); 
     panel.setLayout(new BorderLayout(5, 5)); 
     JPanel centerPanel = new JPanel(); 
     centerPanel.setLayout(new GridBagLayout());   

     JButton but1 = new JButton("1"); 
     JButton but2 = new JButton("2"); 
     but2.setMinimumSize(new Dimension(250,0)); 
     String[] colNames = {"Name","Number","Scores"}; 
     Object[][] data = { 
       { "Mark",11,12}, 
       {"Tommy",23,34}, 
       {"John",34,45} 
     }; 
     JTable table = new JTable(data, colNames); 

     JScrollPane scrollpane = new JScrollPane(table); 
     JSplitPane jsplitpane = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT,but2,scrollpane); 

     GridBagConstraints gbc = new GridBagConstraints(); 
     gbc.anchor = GridBagConstraints.FIRST_LINE_START; 
     gbc.fill = GridBagConstraints.BOTH; 
     gbc.gridx = 0; 
     gbc.gridy = 0; 
     gbc.weightx = 1.0; 
     gbc.weighty = 0.3; 

     centerPanel.add(but1, gbc);   

     gbc.fill = GridBagConstraints.BOTH; 
     gbc.gridy = 1; 
     gbc.weighty = 0.7; 

     centerPanel.add(jsplitpane, gbc); 

     panel.add(centerPanel, BorderLayout.CENTER); 

     app.add(panel); 
     app.setVisible(true); 
    } 

    public static void main(String[] args) { 
     SwingUtilities.invokeLater(new Runnable() { 
      public void run() { 
       new Sample(); 
      } 
     }); 
    } 
} 

這裏是相同的輸出:

GRIDBAGEXAMPLE

+0

Great.Big謝謝。 – veinhorn 2013-04-27 14:56:04

6

組件在BorderLayout.PAGE_START位置都尊重他們的首選大小的高度。因此,如果你願意用GridBagLayout爲上述目的,你可以重寫的JButtonbut1

JButton but1 = new JButton("1") { 
    public Dimension getPreferredSize() { 
     return new Dimension(100, 80); 
    }; 
}; 
+0

thx。抱歉,我在標題中出錯。但我可以改變BorderLayout.NORTH的默認大小嗎? – veinhorn 2013-04-27 14:00:16

+0

我試圖使用這個解決方案,但它不起作用。 – veinhorn 2013-04-27 14:24:33

+0

調整高度參數,以便在默認尺寸和修改尺寸上看到可觀的差異。看到較小的更新 – Reimeus 2013-04-27 14:27:02