2

I'm trying to create a graphic interface using JPanel and JButton. So far it's good except when I'm creating the JButton instances, they seem to align within the same line. I want every button to be in one line, not all in the same line.

How do I achieve the desired effect?

public class Interface  extends JFrame{

    public  Interface ()
    {
    //frame 
        super("Panel");
        pack();
        setSize(500,400);
        setVisible(true);

    //declaration container
    Container c;
    c=getContentPane();
    c.setLayout(new BorderLayout());
    //declaration des panel avec leurs caracteristiques

    JPanel menu =new JPanel();
    JPanel MessageList =new JPanel();
    JPanel about=new JPanel();

    menu.setLayout(new FlowLayout());
    MessageList.setLayout(new FlowLayout());
    about.setLayout(new FlowLayout());

    menu.setBackground(Color.blue);
    MessageList.setBackground(Color.cyan);
    about.setBackground(Color.cyan);

    c.add(menu,BorderLayout.WEST);
    c.add(MessageList,BorderLayout.EAST);
    c.add(about,BorderLayout.SOUTH);
    //--------Button---------------------
    JButton button1=new JButton("button1");
    JButton button2=new JButton("Button2");

    menu.add(button1);
    menu.add(button2);
    //-----------------------------
        }

    public static void main(String args[])
    {
        Interface Message=new Interface();
        Message.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }
}
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
Lou
  • 51
  • 1
  • 2
  • 3
  • 2
    You might have to use a layout other than `FlowLayout()`. Check out this tutorial on effectively using layouts http://java.sun.com/developer/onlineTraining/GUI/AWTLayoutMgr/shortcourse.html – Bala R Aug 24 '11 at 00:24
  • 2
    Or the Swing tutorial on [Layout Managers](http://download.oracle.com/javase/tutorial/uiswing/layout/visual.html). Also you should invoke pack() and setVisible(true) AFTER you have added all the components to the frame. – camickr Aug 24 '11 at 00:29
  • Ok , your comment were a big help thanks – Lou Aug 24 '11 at 01:07

2 Answers2

2

If you want to stack components, use BoxLayout. Also, make sure to follow the suggestions made by @camickr.

See also:

Community
  • 1
  • 1
mre
  • 43,520
  • 33
  • 120
  • 170
2

Use a nested layout. The yellow and blue panels each have their own layout, and are then placed in constraints of the larger layout.

Message GUI

import java.awt.*;
import javax.swing.*;

public class Interface  extends JFrame{

    public  Interface ()
    {
        //frame
        super("Panel");

        //declaration container
        Container c;
        c=getContentPane();
        c.setLayout(new BorderLayout());
        c.setBackground(Color.white);
        //declaration des panel avec leurs caracteristiques

        JPanel menu =new JPanel(new GridLayout(0,1,3,3));
        JPanel messageList =new JPanel(new FlowLayout());
        JPanel about=new JPanel(new FlowLayout());

        menu.setBackground(Color.blue);
        messageList.setBackground(Color.cyan);
        messageList.add(new JLabel("'messageList' padder"));
        about.setBackground(Color.green);
        about.add(new JLabel("'about' padder"));

        JPanel menuConstrain = new JPanel(new BorderLayout());
        menuConstrain.setBackground(Color.yellow);

        menuConstrain.add(menu,BorderLayout.NORTH);
        c.add(menuConstrain,BorderLayout.WEST);
        c.add(messageList,BorderLayout.EAST);
        c.add(about,BorderLayout.SOUTH);
        //--------Button---------------------
        JButton button1=new JButton("button1");
        JButton button2=new JButton("Button2");

        menu.add(button1);
        menu.add(button2);
        //-----------------------------

        pack();
        setSize(300,150);
        setVisible(true);
    }


    public static void main(String args[])
    {
        Interface Message=new Interface();
        Message.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }
}

For a more comprehensive example of using nested layouts, see NestedLayoutExample.java.


BTW - That is some confusingly written code!

  1. It uses the common 'camel case' nomenclature sometimes, but not others.
  2. There is a concrete class with name Interface.
  3. There is a panel named menu.
  4. The frame appears on-screen titled Panel.
  5. Try putting comments in Ethiopian next time. I read that about as well as I read French.
  6. That all combined with the inconsistent indentation, results in code that is hard to read & understand.

Is this a poor man's form of obfuscation?


Note that Swing GUIs should be created on the EDT.

Community
  • 1
  • 1
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433