3

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);
    }
}
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
Elyas Hadizadeh
  • 3,289
  • 8
  • 40
  • 54
  • Bad idea to set layout to null. [Create your ownLayoutManager](http://docs.oracle.com/javase/tutorial/uiswing/layout/custom.html) instead. – Stefan Jan 27 '15 at 13:44
  • yeah i agree with you but it is our project at university and they told us to do it like this :-( – Elyas Hadizadeh Jan 27 '15 at 13:46

2 Answers2

8

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);
    }
}
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
  • It's seems that you are true, i don't know why my professor said do it like this, he said you must setBounds component and if the the user changes the size of jFrame the decorations must change. in this case i must use null layout manager, am i right? – Elyas Hadizadeh Jan 27 '15 at 13:54
  • 3
    *"he said you must setBounds"* The man is a moron. See [Should I avoid the use of set(Preferred|Maximum|Minimum)Size methods in Java Swing?](http://stackoverflow.com/q/7229226/418556) (Yes.) – Andrew Thompson Jan 27 '15 at 13:56
  • `in this case i must use null layout manager, am i right? ` No you don't need to use a null layout. You can create a custom layout that does what you want. – camickr Jan 27 '15 at 16:18
  • as you said it is impossible to have responsive JFrame without layout manager – Elyas Hadizadeh Jan 28 '15 at 05:03
3

You could try to use:

jFrame.addComponentListener(new ComponentListener() {

    // this method invokes each time you resize the frame
    public void componentResized(ComponentEvent e) {            
        // your calculations on buttons          
    }
});
Rafcik
  • 362
  • 7
  • 18
  • thank you for your answer but i don't recognize what should i do inside of componentResized, could you please say an example? – Elyas Hadizadeh Jan 27 '15 at 13:49
  • Change the positions of buttons accordingly to frame `width`, `height` and value of all Buttons in this frame :) – Rafcik Jan 27 '15 at 14:01