如何修复GridBagLayout按钮大小

时间:2017-03-09 15:00:11

标签: java swing

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()searchBar按钮并使用search时,如何将所有按钮保持gridBagLayout相同的尺寸?

1 个答案:

答案 0 :(得分:2)

再次阅读你的问题几次之后,我终于明白了你的真正问题是什么。我起初并没有完全理解它;下面的原始答案(在线下)实际上不会解决您的问题。这是你的问题:

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()获取当前尺码,然后您可以使用widthheight制作新尺码从旧的,并添加或乘以它。

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

setPreferredSize