我想使按鈕和列表中的面板來選擇不同的選項。但不管我做什麼,我不能讓他們相同的大小。爪哇擺動按鈕佈局大小不匹配
從我的班級「動畫師」中,我想添加一個東側的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