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()
是我想要實現的功能,但按鈕的大小都是錯誤的。我希望除searchBar
和search
之外的所有按鈕的大小相同。看看我的意思,取消memberPage2()
並註釋掉memberPage()
。如何將memberPage2()
中的所有按鈕保持相同大小,同時添加我的searchBar
和search
按鈕並使用gridBagLayout
?如何修復的GridBagLayout按鈕大小
所以ipady和ipadx不控制規模?我如何實現ipadx和ipady? – XXIV
我並不是說ipadx/ipady與大小無關。我所說的是,爲了「設置組件的大小」(首先出現的是特定的內容),組件的setPreferredSize()通常是答案,並且也可以在其他佈局管理器中使用。但是,請參閱最新的答案。我的原始答案不會幫助你。 – Aaron
謝謝。由於我對佈局不熟悉,所以花了一些時間來擺弄,但我把它用於網格寬度 – XXIV