2016-03-07 42 views
0

我想使按鈕和列表中的面板來選擇不同的選項。但不管我做什麼,我不能讓他們相同的大小。爪哇擺動按鈕佈局大小不匹配

從我的班級「動畫師」中,我想添加一個東側的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); 

} 

這裏快照,

enter image description here

+0

你有使用'GridBagLayout'爲'buttonRow'考慮見How to Use GridBagLayout? – MadProgrammer

回答

2

也許嘗試使用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); 
    } 

} 

更多細節

+0

謝謝它真的解決了我的問題。 –

+0

@AndersBreid如果此解決方案解決了您的問題,您可以通過單擊打勾來接受他的解決方案。你獲得2點聲望點。 – user3437460