0

I have been trying to learn Buttons but it refuses to resize. Button1 (code below) just takes up the whole screen. I have seen other posts who's problem was that they didn't use

setMaximumSize();

but I'm using it and it still isn't working! I didn't make a JPanel yet. Here is my JFrame:

import java.awt.Dimension;

import javax.swing.JButton;
import javax.swing.JFrame;

public class Frame extends JFrame {
private JButton button1;
private JButton button2;

public Frame()
{
    button1 = new JButton("Hello button1");
    button2 = new JButton("Hello button2");
    button2.setMaximumSize(new Dimension(100,100));
    button1.setMaximumSize(new Dimension(100,100));
    add(button2);
    add(button1);

}

}

My main class is plain and simple:

import javax.swing.JFrame;
import javax.swing.JPanel;

public class Panel extends JPanel{
public static void main(String args [])
 {
    Frame frame = new Frame();
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setSize(500, 500);
    frame.setVisible(true);
  }
 }
Jack
  • 955
  • 1
  • 9
  • 30
  • It's because the frame('s `contentPane`) has a [`BorderLayout`](https://docs.oracle.com/javase/tutorial/uiswing/layout/border.html) by default. You add the buttons to `BorderLayout.CENTER`, so the layout manager ignores the `minimum`, `preferred` and `maximum` `size`. – Lukas Rotter Feb 12 '16 at 15:22
  • Can you please make that into an answer with example code? Thanks! – Jack Feb 12 '16 at 15:27
  • Please describe us the desired layout, then I could make an example code. – Lukas Rotter Feb 12 '16 at 15:29
  • I just want them to come up small and side by side. – Jack Feb 12 '16 at 15:29

2 Answers2

4

It's because the frame('s contentPane) has a BorderLayout by default. You add the buttons to BorderLayout.CENTER, so the layout manager ignores the minimum-, preferred- and maximumSize.

I just want them to come up small and side by side

For that you could use a simple FlowLayout. (And if you want them to be centered on the frame, a parent JPanel with a GridBagLayout)

If you want a custom width & height for the buttons, override their getPreferredSize method. Overriding this method is safer than calling setPreferredSize.


Example:

import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.GridBagLayout;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;

public class Example {

    public Example() {

        JButton button1 = new JButton("Hello button1");
        JButton button2 = new JButton("Hello button2") {
            @Override
            public Dimension getPreferredSize() {
                int width = super.getPreferredSize().width;
                return new Dimension(width, width);
            }
        };;

        JPanel buttonPanel = new JPanel();
        buttonPanel.add(button1);
        buttonPanel.add(button2);

        JPanel contentPanel = new JPanel(new GridBagLayout());
        contentPanel.add(buttonPanel);

        JFrame frame = new JFrame();
        frame.setContentPane(contentPanel);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setSize(400, 400);
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
    }

    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                new Example();
            }
        });
    }

}
Lukas Rotter
  • 4,158
  • 1
  • 15
  • 35
1

I added a flowlayout and changed setMaximumSize to setPreferredSize. That should fix your problem. Here try this:

import java.awt.Dimension;
import javax.swing.JButton;
import javax.swing.JFrame;

public class Frame extends JFrame {
private JButton button1;
private JButton button2;

public Frame()
{
    button1 = new JButton("Hello button1");
    button2 = new JButton("Hello button2");
    button2.setPreferredSize(new Dimension(100,100));
    button1.setPreferredSize(new Dimension(100,100));
    add(button2);
    add(button1);

    }

}

<----now the other class--->

import java.awt.FlowLayout;
import javax.swing.JFrame;
import javax.swing.JPanel;

public class Panel extends JPanel{
public static void main(String args [])
 {
    Frame frame = new Frame();
    frame.setLayout(new FlowLayout());
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setSize(500, 500);
    frame.setVisible(true);
  }
 }
W.Sar
  • 126
  • 1
  • 6
  • 3
    See also [*Should I avoid the use of set(Preferred|Maximum|Minimum)Size methods in Java Swing?*](http://stackoverflow.com/q/7229226/230513). – trashgod Feb 12 '16 at 15:54
  • Thank you! I don't have access to my computer right now but I'll try it later. – Jack Feb 12 '16 at 16:48