2017-03-09 58 views
0
import javax.swing.JFrame; 

public class Main { 
    private JFrame frame; 

    /** 
    * @param args 
    */ 
    public Main() { 
     frame = new JFrame(); 
     frame.setTitle("450"); 
     frame.setSize(1000, 600); 
     MemberPanel memberPanel = new MemberPanel(); 
     frame.add(memberPanel); 
     frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     frame.setVisible(true); 
    } 

    public static void main(String[] args) { 
     new Main(); 
    } 

} 
import java.awt.GridBagConstraints; 
import java.awt.GridBagLayout; 
import java.awt.Insets; 
import java.awt.TextField; 
import javax.swing.JButton; 
import javax.swing.JPanel; 

public class MemberPanel extends JPanel{ 
    private GridBagConstraints c; 

    public MemberPanel() { 
     setLayout(new GridBagLayout()); 
     c = new GridBagConstraints(); 
     memberPage(); 
     //memberPage2(); 

    } 
    public void memberPage() { 
     JButton addMovie = new JButton("Add Movie"); 
     JButton random = new JButton("Random"); 
     JButton storedData = new JButton("Stored Data"); 
     JButton settings = new JButton("Settings"); 
     JButton quit = new JButton("Quit"); 
     JButton search = new JButton("Search"); 
     TextField searchBar = new TextField(40); 
     removeAll(); 
     repaint(); 
     revalidate(); 
     c.fill = GridBagConstraints.HORIZONTAL; 
     c.ipady = 20; 
     c.ipadx = 40; 
     c.insets = new Insets(0, 20, 20, 0); // side padding 
     c.gridx = 0; 
     c.gridy = 0; 
     add(addMovie, c); 
     c.fill = GridBagConstraints.HORIZONTAL; 
     c.ipady = 20; 
     c.ipadx = 40; 
     c.insets = new Insets(0, 20, 20, 0); // side padding 
     c.gridx = 1; 
     c.gridy = 0; 
     add(random, c); 
     c.fill = GridBagConstraints.HORIZONTAL; 
     c.ipady = 20; 
     c.ipadx = 40; 
     c.insets = new Insets(0, 20, 20, 0); // side padding 
     c.gridx = 2; 
     c.gridy = 0; 
     add(storedData, c); 
     c.fill = GridBagConstraints.HORIZONTAL; 
     c.ipady = 20; 
     c.ipadx = 40; 
     c.insets = new Insets(0, 20, 20, 0); // side padding 
     c.gridx = 3; 
     c.gridy = 0; 
     add(settings, c); 
     c.fill = GridBagConstraints.HORIZONTAL; 
     c.ipady = 20; 
     c.ipadx = 40; 
     c.insets = new Insets(0, 20, 20, 0); // side padding 
     c.gridx = 4; 
     c.gridy = 0; 
     add(quit, c); 
     c.fill = GridBagConstraints.HORIZONTAL; 
     c.ipady = 20; 
     c.ipadx = 40; 
     c.insets = new Insets(20, 20, 20, 20); // side padding 
     c.gridx = 2; 
     c.gridy = 2; 
     add(searchBar, c); 
     c.fill = GridBagConstraints.HORIZONTAL; 
     c.ipady = 20; 
     c.ipadx = 40; 
     c.insets = new Insets(20, 20, 20, 20); // side padding 
     c.gridx = 3; 
     c.gridy = 2; 
     add(search, c); 
    } 
    public void memberPage2(){ 
     JButton addMovie = new JButton("Add Movie"); 
     JButton random = new JButton("Random"); 
     JButton storedData = new JButton("Stored Data"); 
     JButton settings = new JButton("Settings"); 
     JButton quit = new JButton("Quit"); 
     JButton search = new JButton("Search"); 
     TextField searchBar = new TextField(40); 
     removeAll(); 
     repaint(); 
     revalidate(); 
     c.fill = GridBagConstraints.HORIZONTAL; 
     c.ipady = 20; 
     c.ipadx = 40; 
     c.insets = new Insets(0, 20, 20, 0); // side padding 
     c.gridx = 0; 
     c.gridy = 0; 
     add(addMovie, c); 
     c.fill = GridBagConstraints.HORIZONTAL; 
     c.ipady = 20; 
     c.ipadx = 40; 
     c.insets = new Insets(0, 20, 20, 0); // side padding 
     c.gridx = 1; 
     c.gridy = 0; 
     add(random, c); 
     c.fill = GridBagConstraints.HORIZONTAL; 
     c.ipady = 20; 
     c.ipadx = 40; 
     c.insets = new Insets(0, 20, 20, 0); // side padding 
     c.gridx = 2; 
     c.gridy = 0; 
     add(storedData, c); 
     c.fill = GridBagConstraints.HORIZONTAL; 
     c.ipady = 20; 
     c.ipadx = 40; 
     c.insets = new Insets(0, 20, 20, 0); // side padding 
     c.gridx = 3; 
     c.gridy = 0; 
     add(settings, c); 
     c.fill = GridBagConstraints.HORIZONTAL; 
     c.ipady = 20; 
     c.ipadx = 40; 
     c.insets = new Insets(0, 20, 20, 0); // side padding 
     c.gridx = 4; 
     c.gridy = 0; 
     add(quit, c); 
    } 
} 

MemberPanel類的構造函數有兩個功能memberPage()memberPage2()memberPage()是我想要實現的功能,但按鈕的大小都是錯誤的。我希望除searchBarsearch之外的所有按鈕的大小相同。看看我的意思,取消memberPage2()並註釋掉memberPage()。如何將memberPage2()中的所有按鈕保持相同大小,同時添加我的searchBarsearch按鈕並使用gridBagLayout如何修復的GridBagLayout按鈕大小

回答

2

多次閱讀您的問題後,我終於明白了您的真正問題所在。起初我不太明白;下面的原始答案(下方)不會真正解決您的問題。以下是您的問題:

GridBagLayout將所有內容放入行和列(gridx,gridy)的網格中。您的「存儲數據」按鈕與文本字段的寬度相同,因爲它們是同一列的一部分。有幾種方法可以解決這個問題,你究竟做什麼取決於你想要它的樣子。

最簡單的(我的意見)答案是讓文本字段佔據多列。 GridBagConstraint對此有一個gridwidth字段。

c.gridwidth = 3; 
panel.add(txtField, c); 
c.gridwidth = 1; // don't forget to put it back after 

如果你這樣做,請記住,「搜索」按鈕,然後還需要在不同的列,作爲下一列文本字段的權利(和下一個後用gridwidth 3)全部被文本字段佔據。因此,要麼將文本字段放在gridx = 0處,要麼將搜索按鈕的gridx加上2以上。


您一般通過設置按鈕的setPreferredSize(Dimension)

JPanel panel = new JPanel(new GridBagLayout()); 
GridBagConstraints c = new GridBagConstraints(); 

c.gridx = x1; 
c.gridy = y1; 
JButton btn1 = new JButton("Smaller"); 
btn1.setPreferredSize(new Dimension(30, 20)); 
panel.add(btn1, c); 

c.gridx = x2; 
c.gridy = y2; 
JButton btn2 = new JButton("Larger"); 
btn2.setPreferredSize(new Dimension(50, 40)); 
panel.add(btn1, c); 

是你所追求的控制按鈕的大小?

如果你想在新的大小是相對於舊的大小,你可以用Dimension getPreferredSize()得到電流的大小,那麼你就可以使新的使用來自舊的widthheight和相加或相乘它。

Dimension d = btn1.getPreferredSize(); 
btn1.setPreferredSize(d.width + 30, d.height); 

setPreferredSize

+0

所以ipady和ipadx不控制規模?我如何實現ipadx和ipady? – XXIV

+0

我並不是說ipadx/ipady與大小無關。我所說的是,爲了「設置組件的大小」(首先出現的是特定的內容),組件的setPreferredSize()通常是答案,並且也可以在其他佈局管理器中使用。但是,請參閱最新的答案。我的原始答案不會幫助你。 – Aaron

+0

謝謝。由於我對佈局不熟悉,所以花了一些時間來擺弄,但我把它用於網格寬度 – XXIV