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()
。如何在添加 searchBar
和 search
按钮并使用 gridBagLayout< 时使
?memberPage2()
中的所有按钮保持相同大小
最佳答案
多读了几遍你的问题,我终于明白你真正的问题是什么了。一开始我不太明白;下面的原始答案(线下)实际上并不能解决您的问题。这是您的问题:
GridBagLayout
将所有内容放入行和列的网格中(gridx、gridy)。您的“存储数据”按钮与文本字段的宽度相同,因为它们是同一列的一部分。有几种方法可以解决这个问题,具体做什么取决于您希望它是什么样子。
最简单的(我认为)答案是让文本字段占据多列。 GridBagConstraint
有一个 gridwidth
字段用于此目的。
c.gridwidth = 3;
panel.add(txtField, c);
c.gridwidth = 1; // don't forget to put it back after
如果您这样做,请记住“搜索”按钮也需要位于不同的列中,因为文本字段右侧的下一列(以及之后的下一列,网格宽度为 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()
获取当前尺寸,然后您可以使新尺寸使用width
和 height
与旧的相加或相乘。
Dimension d = btn1.getPreferredSize();
btn1.setPreferredSize(d.width + 30, d.height);
关于java - 如何修复 GridBagLayout 按钮大小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42698601/