【问题标题】:Java swing button layout sizes won´t matchJava Swing 按钮布局大小不匹配
【发布时间】:2016-03-07 10:04:38
【问题描述】:

我正在尝试制作一个带有按钮的面板和一个用于选择不同选项的列表。但无论我做什么,我都无法使它们的大小相同。

从我的班级“动画师”中,我想在东侧的 JFrame 中添加一个按钮面板。居中将是一个移动盒子的动画,但尚未创建。我想我先制作按钮面板。

我尝试过setPreferedSize(new dimension(x,y)),但如果我在button 上这样做,JComboBox 也会发生变化。和button 仍然保持不变。我很困惑。

另外Jcombobox在选择另一个选项后没有任何动作?不应该吗?

这是 Animator 代码:

frame = new JFrame("Box Mover Calculator!");
    frame.setLayout(new BorderLayout());
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setSize(500, 500);
    frame.setLocationRelativeTo(null);

    buttonArea = new ButtonPanel(this); 
    boxArea = new JPanel(new GridLayout(1,1));
    frame.add(boxArea, BorderLayout.CENTER);
    frame.add(buttonArea, BorderLayout.EAST);
    frame.setVisible(true);

这是按钮面板:

public ButtonPanel(Animator animator) {
    super();
    //this.animator = animator;
    //setLayout(new BoxLayout(this, BoxLayout.PAGE_AXIS));
    buttonRow = new JPanel();
    buttonRow.setLayout(new BoxLayout(buttonRow, BoxLayout.PAGE_AXIS));
    buttonRow.setBackground(Color.CYAN);
    this.setBackground(Color.CYAN);

    button1 = new JButton("Start Animation");
    button1.addActionListener(new QuitHandler());
    button1.setBackground(Color.CYAN);
    button1.setForeground(Color.blue);
    buttonRow.add(button1);

    button2 = new JButton("Move Box");
    button2.addActionListener(new QuitHandler());
    button2.setBackground(Color.CYAN);
    button2.setForeground(Color.blue);
    button2.setOpaque(true);
    buttonRow.add(button2);

    String[] bookTitles = new String[] {"Effective Java", "Head First Java",
            "Thinking in Java", "Java for Dummies"};

    JComboBox<String> bookList = new JComboBox<>(bookTitles);

    //add to the parent container (e.g. a JFrame):
    buttonRow.add(bookList);

    //get the selected item:
    String selectedBook = (String) bookList.getSelectedItem();
    System.out.println("You seleted the book: " + selectedBook);

    //Adds all rows
    add(buttonRow);     
    setVisible(true);

}

这是快照,

【问题讨论】:

  • 您是否考虑过将GridBagLayout 用于buttonRow

标签: java swing button jpanel jbutton


【解决方案1】:

也许可以尝试使用GridBagLayout,例如...

public class TestPane extends JPanel {

    public TestPane() {
        setLayout(new BorderLayout());
        JPanel buttonRow = new JPanel(new GridBagLayout());
        buttonRow.setBackground(Color.CYAN);
        this.setBackground(Color.CYAN);

        GridBagConstraints gbc = new GridBagConstraints();
        gbc.gridwidth = GridBagConstraints.REMAINDER;
        gbc.fill = GridBagConstraints.HORIZONTAL;

        JButton button1 = new JButton("Start Animation");
        button1.setBackground(Color.CYAN);
        button1.setForeground(Color.blue);
        buttonRow.add(button1, gbc);

        JButton button2 = new JButton("Move Box");
        button2.setBackground(Color.CYAN);
        button2.setForeground(Color.blue);
        button2.setOpaque(true);
        buttonRow.add(button2, gbc);

        String[] bookTitles = new String[]{"Effective Java", "Head First Java",
            "Thinking in Java", "Java for Dummies"};

        JComboBox<String> bookList = new JComboBox<>(bookTitles);

        gbc.weighty = 1;
        gbc.anchor = GridBagConstraints.NORTH;
        //add to the parent container (e.g. a JFrame):
        buttonRow.add(bookList, gbc);

        //Adds all rows
        add(buttonRow);
    }

}

详情请见How to Use GridBagLayout

【讨论】:

  • @AndersBreid 如果此解决方案解决了您的问题,您可以点击勾选接受他的解决方案。您将获得 2 点声望作为回报。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-05-27
  • 1970-01-01
  • 2016-05-05
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多