我想使按钮和列表中的面板来选择不同的选项。但不管我做什么,我不能让他们相同的大小。爪哇摆动按钮布局大小不匹配
从我的班级“动画师”中,我想添加一个东侧的JFrame按钮面板。而居中将是一个移动框的动画,但尚未创建。我想我先做按钮面板。
我试过setPreferedSize(new dimension(x,y))
但是如果我在button
上做了这个,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);
}
这里快照,
你有使用'GridBagLayout'为'buttonRow'考虑见How to Use GridBagLayout? – MadProgrammer