gpt4 book ai didi

Java swing 按钮布局尺寸不匹配

转载 作者:行者123 更新时间:2023-12-02 03:51:28 26 4
gpt4 key购买 nike

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

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

我已经尝试过setPreferedSize(newDimension(x,y)),但如果我在按钮上执行此操作,JComboBox也会改变。和 button 仍然保持不变。我很困惑。

选择另一个选项后Jcombobox也没有任何操作?不应该吗?

这是动画器代码:

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);

}

这是快照,

enter image description here

最佳答案

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

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了解更多详情

关于Java swing 按钮布局尺寸不匹配,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35840826/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com