0

I've a big problem with Swing in Java, I used BoxLayout for this but still it looks bad. Any suggestions about my usage of layouts, or how to change it to look like in assumptions? (here are assumptions)

Container main = new Container();
    Container left = new Container();// here goin buttons
    Container right = new Container(); // here goin tabs +  more buttons, textfields and other stuff

    BoxLayout lewyL = new BoxLayout(left, BoxLayout.Y_AXIS);
    left.setLayout(lewyL);
    left.add(rastrowa); //radiobutton
    left.add(wektorowa);//radiobutton
    left.add(apDwuliniowa);//checkbox
    left.add(wczytaj);//button
    left.add(zapisz);//obutton
    left.add(wyczysc);//button
    BoxLayout prawyL = new BoxLayout(right, BoxLayout.Y_AXIS);
    right.setLayout(prawyL);
    right.add(zakladki);// tabs (mostly i use BoxLayout but for last one i need something more "complicated")

EDIT: I almost solve this problem, I need to move all elements to left (how it look like)but I have no idea how ;/ Here is constructor of this class.

            JLabel label = new JLabel("O wektor");
    JLabel labelA = new JLabel("a:");
    JLabel labelB = new JLabel("b:");
    JButton wykonaj = new JButton("Wykonaj");
    JTextField a = new JTextField(5);
    JTextField b = new JTextField(5);
    add(label);
    add(labelA);
    add(a);
    add(labelB);
    add(b);
    add(wykonaj);   
USER_
  • 63
  • 4

1 Answers1

1

There's nothing wrong with the way it looks (in my opinion), but if you want it to look a little better, why don't you convert the left panel (which is 6x1) into a 3x2 panel, with the checkboxes/radiobuttons on the left, and buttons on the right? Sounds like a job for GridLayout - one of my favorite classes...

JPanel leftPanel = new JPanel(new GridLayout(3,2));
leftPanel.add(rastrowa);     //radiobutton
leftPanel.add(wczytaj);      //button
leftPanel.add(wektorowa);    //radiobutton
leftPanel.add(zapisz);       //obutton
leftPanel.add(apDwuliniowa); //checkbox
leftPanel.add(wyczysc);      //button

Note that the 3,2 defines the number of rows,columns. When adding panels, they are added to the grid from left-to-right, and top-to-bottom. GridLayout also auto-sizes the components, so all the buttons etc will be the same width and height, making it look more consistent.

The GridLayout documentation might be useful, and the Visual Guide to Layout Managers is a great place to see other layout managers that might work better for your different situations. I personally find BorderLayout and GridLayout to be the most useful, and cover about 95% of the situations I ever need for my GUIs.

wattostudios
  • 8,666
  • 13
  • 43
  • 57
  • I tried with GridLayout, i still need help how to do interface whats fit in program size(setBounds(950,650)) – USER_ Nov 19 '12 at 13:49