【发布时间】: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