简体   繁体   中英

Java swing button layout sizes won´t match

I am trying to make like a panel with buttons and a list to select different options in. But no matter what I do I can not make them the same size.

From my class "animator" I want to add to a JFrame on East side a button panel. And in centered will be an animation that moves a box, but that is yet to be created. I figured I make the button panel first.

I´ve tried setPreferedSize(new dimension(x,y)) but if I do that on a button the JComboBox also changes. and button still remains the same. I am confused.

Also the Jcombobox has no action after selecting another option? should it not?

This is the Animator code:

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

This is the button panel:

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

}

here is snapshot,

在此处输入图片说明

Maybe try using a GridBagLayout , for example...

网格包布局

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

}

See How to Use GridBagLayout for more details

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM