0

Hi i am trying to make java desktop application where i am having 4 jbutton i want to set them bottom left corner one by one i am using null layout for setting them but i dont want to use null layout so

how can i achieve this ?

Thanks in advance

   ok = new JButton(s);
      ok.setBounds(800, 725, 100, 40);
      ok.setBackground(Color.red);
      ok.setOpaque(true);
       ok.setForeground(Color.BLACK);
       c.add(ok);

     print = new JButton("Print");
      print.setBounds(925, 725, 100, 40);
       print.setBackground(Color.red);
      print.setOpaque(true);
       print.setForeground(Color.BLACK);
       c.add(print);

     next = new JButton("next");
     next.setBounds(400, 725, 100, 40);
      next.setBackground(Color.red);
      next.setOpaque(true);
       next.setForeground(Color.BLACK);
       c.add(next);

     home = new JButton("home");
     home.setBounds(500, 725, 100, 40);
       home.setForeground(Color.BLACK);
        home.setBackground(Color.red);
  home.setOpaque(true);
       c.add(home);
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
user3456343
  • 252
  • 3
  • 7
  • 21
  • 2
    Choose one of the [available layouts](http://docs.oracle.com/javase/tutorial/uiswing/layout/index.html), depeding on what else you have on this JPanel. – Qiu Mar 27 '14 at 06:53
  • Java GUIs might have to work on a number of platforms, on different screen resolutions & using different PLAFs. As such they are not conducive to exact placement of components. To organize the components for a robust GUI, instead use layout managers, or [combinations of them](http://stackoverflow.com/a/5630271/418556), along with layout padding & borders for [white space](http://stackoverflow.com/q/17874717/418556). – Andrew Thompson Mar 27 '14 at 07:08

2 Answers2

4

Sort answer, use a layout manager.

Long answer, pixel perfect layouts are an illusion in modern user interface design. You can't possible predict the combination of hardware and software that is used to render content to the screen and what effect that will have on your components.

Each system could be using different fonts, rendering pipelines, DPI, hardware...and much more, all of which will change the size of the text and other elements on your UI.

Swing was designed to work around the use of layout managers, trying to do with out greatly increases your work load, as you now need to do the work that the layer manager was as well as be able to detect changes that occur around you and the parent components that you've been added to...

Looking at your code, I could suggest FlowLayout or GridBagLayout as a possible starting point

MadProgrammer
  • 343,457
  • 22
  • 230
  • 366
  • if i use netbeans then can i give name at run time to my jbutton – user3456343 Mar 27 '14 at 06:56
  • @user3456343 What's that got to do with the question? – MadProgrammer Mar 27 '14 at 06:57
  • i am using net beans ide for gui creation i create jbuuton by netbeans but wnt to set name on jbutton by code – user3456343 Mar 27 '14 at 07:01
  • When the form design is open, there should be an explorer window which shows all the components that are currently on the selected form. Select the node that represents the component you want to name, press `F2` and type in a new name... – MadProgrammer Mar 27 '14 at 07:04
-1

As @MadProgrammer said you really should use layout managers.

But if you insist on working like this use:

c.setLayout(null);
Benjamin Albert
  • 738
  • 1
  • 7
  • 19
  • *"But if you insist on working like this.."* ..be prepared to enter the 7th level of Hell. For ..anything else ***use layouts.*** – Andrew Thompson Mar 28 '14 at 04:53