Search code examples
javaswingjframelayout-managernull-layout-manager

Responsive JFrame without Layout Manager


I am trying to do have two button in JFrame, and I call the setBounds method of them for setting the positions and size of them and also I passed null to setLayout1 because I want to usesetBounds` method of component.

Now I want to do something with my code that whenever I resize the frame buttons decoration will change in a suitable form like below pictures:

before resizing after resizing

I know it is possible to use create an object from JPanel class and add buttons to it and at the end add created panel object to frame, but I am not allowed to it right now because of some reason (specified by professor).

Is there any way or do you have any suggestion?

My code is like this:

public class Responsive
{
    public static void main(String[] args)
    {
        JFrame jFrame = new JFrame("Responsive JFrame");
        jFrame.setLayout(null);
        jFrame.setBounds(0,0,400,300);

        JButton jButton1 = new JButton("button 1");
        JButton jButton2 = new JButton("button 2");

        jButton1.setBounds(50,50,100,100);
        jButton2.setBounds(150,50,100,100);

        jFrame.add(jButton1);
        jFrame.add(jButton2);

        jFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        jFrame.setVisible(true);
    }
}

Solution

  • A FlowLayout with no horizontal spacing, some vertical spacing and large borders could achieve that easily. A null layout manager is never the answer to a 'responsive' robust GUI.

    enter image description here enter image description here

    import java.awt.*;
    import javax.swing.*;
    import javax.swing.border.EmptyBorder;
    
    public class ResponsiveGUI {
    
        private JComponent ui = null;
    
        ResponsiveGUI() {
            initUI();
        }
    
        public void initUI() {
            if (ui!=null) return;
    
            ui = new JPanel(new FlowLayout(FlowLayout.CENTER, 0, 8));
            ui.setBorder(new EmptyBorder(10,40,10,40));
    
            for (int i=1; i<3; i++) {
                ui.add(getBigButton(i));
            }
        }
    
        public JComponent getUI() {
            return ui;
        }
    
        private final JButton getBigButton(int number) {
            JButton b = new JButton("Button " + number);
            int pad = 20;
            b.setMargin(new Insets(pad, pad, pad, pad));
    
            return b;
        }
    
        public static void main(String[] args) {
            Runnable r = new Runnable() {
                @Override
                public void run() {
                    try {
                        UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                    } catch (Exception useDefault) {
                    }
                    ResponsiveGUI o = new ResponsiveGUI();
    
                    JFrame f = new JFrame("Responsive GUI");
                    f.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
                    f.setLocationByPlatform(true);
    
                    f.setContentPane(o.getUI());
                    f.pack();
    
                    f.setVisible(true);
                }
            };
            SwingUtilities.invokeLater(r);
        }
    }