-1

Am writing a code on swings and am using null layout so that i can put j components anywhere i like by using set Bounds But when i maximize the J Frame my components will be static.

How can i make components to move automatically. Please help me its important.

And i have to use null layout only cant use any other layout because i have to put so many components to there specific place.

    public class ScreenResize extends JFrame{
    private static JButton b;
    ScreenResize(){

    JFrame.setDefaultLookAndFeelDecorated(true);

    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    int frameWidth = 200;
    int frameHeight = 100;
    Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
    setBounds((int) screenSize.getWidth() - frameWidth, 0, frameWidth, frameHeight);
    setVisible(true);
    b=new JButton();
    b.setBounds(50,50,40,20);
    add(b);

    }
    public static void main(String[] args) {
    new ScreenResize();
    }
    }   

Thanks.

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
yasmeen
  • 55
  • 1
  • 6
  • 2
    Don't use `null` layout if you want to handover it to layout manager itself to position the components. – Braj Apr 21 '14 at 18:27
  • I suggest you to use [GridBagLayout](http://docs.oracle.com/javase/tutorial/uiswing/layout/gridbag.html). – Braj Apr 21 '14 at 18:36
  • *"And i have to use null layout only cant use any other layout because i have to put so many components to there specific place."* Rubbish. Provide ASCII art (or an image with a simple drawing) of the GUI as it should appear in smallest size and (if resizable) with extra width/height. – Andrew Thompson Apr 22 '14 at 01:19

1 Answers1

2

You won't be able to do that unless you want to reprogram what would essentially be the other layouts like BorderLayout, GridLayout, and so on. So the solution for this is to not use a null layout and use a combination of other layouts nested together.

Here's a link to get started on what the other specific layouts can do for you.

NESPowerGlove
  • 5,496
  • 17
  • 28