user3491205
user3491205

Reputation: 69

how do i change the size of Jbuttons without changing the size of the Jpanel its sitting on?

So I have 2 panels on screen. One has been set to the WEST and the other is set to CENTER to take up the rest of the screen. The Problems I am having:

  1. When I change the size of the buttons it either changes the size of the panel too or does nothing at all. The line commented out is where I am trying to do this. How would I make the buttons smaller without changing the size of the panel?
  2. Are the 2 buttons always gonna do the same thing as far as resizing? Or is there a way of making different buttons different sizes with different spaces in between WITHOUT having to assign them to different panels?

 public MainPanel() {
        super();

        this.setLayout(new BorderLayout());
        buttonPanel = new JPanel();
        buttonPanel.setLayout(new GridLayout(5, 1));
        buttonPanel.setBorder(BorderFactory.createLineBorder(Color.BLACK));
        this.add(buttonPanel, BorderLayout.WEST);

                centerPanel = new JPanel();
        centerPanel.setBackground(Color.black);
        centerPanel.setOpaque(true);
        centerPanel.setBorder(BorderFactory.createLineBorder(Color.BLACK));
        this.add(centerPanel, BorderLayout.CENTER);

        JButton options = new JButton("Options");
        //options.setPreferredSize(new Dimension(100, 100));
        buttonPanel.add(options);
        options.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                RichardDemo.this.switchPanel(MainPanel.this);
            }
        });     

        JButton start = new JButton("Start Game");
        buttonPanel.add(start);
        start.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {

            }
        });
    }
}

Upvotes: 0

Views: 43

Answers (1)

Braj
Braj

Reputation: 46841

When I change the size of the buttons it either changes the size of the panel too or does nothing at all?

You don't need to change the size of components. Just leave it to Layout Manager to size your components.

Are the 2 buttons always gonna do the same thing as far as resizing?

Its not required. It depends on the Layout that you are using.

Is there a way of making different buttons different sizes with different spaces in between WITHOUT having to assign them to different panels?

Yes, you can achieve it using GridBagLayout.

enter image description here

Upvotes: 1

Related Questions