1

here's my problems : I tried to create a simple interface from where I can click on several buttons that call differents functions, and show the result (which always is text) in a jlabel.I tried to have 2 separates parts (north/south or east/west) The first area would contain a gridlayout or flowlayout of all my buttons and the 2nd area would show the text result.

Here are my declarations :

private static JButton b0 = new JButton("Creer Zone");
private static JButton b1 = new JButton("1");
private static JButton b2 = new JButton("2");
...
private static JButton b11= new JButton("11");
private static JButton b12 = new JButton("12");
private static JButton b14 = new JButton("Help");
private static JFrame windows = new JFrame();
private static JPanel container = new JPanel();
private static JLabel res = new JLabel();

and here is how i added them to the JFrame (which is really awfull to see) :

container.add(b0);
container.add(b1);
container.add(b2);
...
container.add(b12);
container.add(b14);
container.add(res);

windows.setSize(450,500);
windows.setContentPane(container);
windows.setLocation(150 , 150);
windows.setVisible(true);

I've tried to declare my jpanel with a gridlayout, a borderlayout and change the location (N S E W) and a flowlayout, i always ended up with the jlabel disapearing (which is bad because this jlabel show the result of my functions)

Any one have a simple way to help me overcome this ? Thanks a lot for any time you take

WizLiz
  • 2,068
  • 5
  • 25
  • 39
  • You're not adding the panel (which looks like it contains everything else) to the JFrame. Might be an issue... Also, I can reproduce this issue, and the reason being is the fact that I'm not defining a layout for the frame. I usually use FlowLayout for things like this. If you need clarification, simply ask. :) – fireshadow52 Apr 03 '12 at 14:24
  • Is there a specific reason that you need to use `JButton` to perform these task, as you mentioned or anything other than that can also be used to save your day? :-) – nIcE cOw Apr 03 '12 at 15:15

2 Answers2

1

nicely explained on http://docs.oracle.com/javase/tutorial/uiswing/layout/gridbag.html

Peter
  • 5,728
  • 20
  • 23
  • thanks for the answer ^-^ but I have already spent like an hour on the oracla java doc that's why I asked here. GridBagLayout seemed to be the perfect solution at first but i never managed to make it work so i gave up and tried to have 2 different section, in the end nothing worked. Anyway i'll try this GridBag again, maybe i can come with something better this time =D – WizLiz Apr 03 '12 at 13:57
  • *"I have already spent like an hour on the oracla java doc"* Spend like a few days doing the [Laying Out Components Within a Container](http://docs.oracle.com/javase/tutorial/uiswing/layout/index.html) lesson of the Java Tutorial. Use them as [nested layouts](http://stackoverflow.com/a/5630271/418556) to achieve complex layouts, simply. – Andrew Thompson Apr 04 '12 at 04:24
1

Another possible solution for your future endeavors is to look into WindowsBuilder. It basically simplifies your layout design by allowing you to just simply drag and drop your elements (Jframe, JtextField, etc) and then in the background WindowsBuilder will write the code for you. Mind you the only thing that you will have to really add/change is the naming conventions of the buttons and of course the eventHandlers, but honestly those are something that any designer would want to control... Hopefully this helps you!

https://developers.google.com/java-dev-tools/wbpro/