Pergunta

In the code below, when a JPanel is not added to the JFrame extention, the programme works fine but then, I commented out the JPanel and used the GridLayout, everything is properly and nicely placed on the JFrame. Could it be some restrictions to how JPanel display objects? In the code I have commented out the following statements:

  this.setLayout(new GridLayout(3,2));
   this.add(nameLabel);
   this.add(name);
   this.add(urlLabel);
   this.add(url);
   this.add(typeLabel);
   this.add(type);

and the programme is displayed wrongly but if the statements above are uncommented and the one below commented:

   //add some panes..
    JPanel panel = new JPanel();
    panel.add(nameLabel);
    panel.add(name);
    panel.add(urlLabel);
    panel.add(url);
    panel.add(typeLabel);
    panel.add(type);
    this.add(panel);

, then the programme works fine.. Below is the full code excerpt.

[CODE]

public class FeedInfo extends JFrame {
  private JLabel nameLabel = new JLabel("Name:", SwingConstants.RIGHT);
  private JTextField name;
  private JLabel urlLabel = new JLabel("URL",SwingConstants.RIGHT);
  private JTextField url;
  private JLabel typeLabel = new JLabel("Type",SwingConstants.RIGHT);
  private JTextField type;


   public FeedInfo()
      {
         super("Feed Information");
         this.setSize(400,105);
         this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

         String response1 = JOptionPane.showInputDialog(null,"Enter the site name:");
         name = new JTextField(response1,20);
         String response2 = JOptionPane.showInputDialog(null,"Enter the site address:");
         url = new JTextField(response2, 20);

         String[] choices ={"Personal","Commercial","Unkown"};
         int response3 = JOptionPane.showOptionDialog(null, "what type of site is it?",
           "site Type",0, JOptionPane.QUESTION_MESSAGE, null, choices, choices[0]);

        type = new JTextField(choices[response3],20);
        //add some panes..
        JPanel panel = new JPanel();
        panel.add(nameLabel);
        panel.add(name);
        panel.add(urlLabel);
        panel.add(url);
        panel.add(typeLabel);
        panel.add(type);
        this.add(panel);


        /*this.setLayout(new GridLayout(3,2));
       this.add(nameLabel);
       this.add(name);
       this.add(urlLabel);
       this.add(url);
       this.add(typeLabel);
       this.add(type);
       */

       this.setVisible(true);

    }
[/CODE]
Foi útil?

Solução

JPanel panel = new JPanel();
        panel.add(nameLabel);
        panel.add(name);
        panel.add(urlLabel);
        panel.add(url);
        panel.add(typeLabel);
        panel.add(type);
        this.add(panel);

When you are doing this, you are adding the JPanel onto the JFrame, but the JFrame's layout isn't changed which means it has BorderLayout by default. Try change the last line to: add(panel, BorderLayout.CENTER); You can read more about JFrame here

Also, a JPanel has FlowLayout as default, which means objects will be added in a "flowing line" You should set the layout on your JPanel to get desired result

Outras dicas

JPanel does not perform layout by itself, it uses a LayoutManager. In your case you want to override the default FlowLayout and use GridLayout (just like you did for your JFrame). So do so!

JPanel panel = new JPanel(new GridLayout(3, 2));
...

Your program isn't working as expected, with the second section of code, because you're using

JPanel panel = new JPanel();

By default, with this line, as you can see from the JPanel API, you are creating a JPanel with layout as FlowLayout. The JPanel API reports, about the empty contructor:

JPanel() 
Creates a new JPanel with a double buffer and a flow layout.

Generally, a flow layout is the simplest layout for a JPanel, and it isn't possible to load Swing content in an understandable and easy way. The GridLayout, that you're using, offers the ability to load the components in a grid, that's good to display the GUI of Java applications.

Now, you know because the program, with a basic FlowLayout, isn't displaying all your swing components as expected, like in the first section of code, when you are using a GridLayout. If you want to create a JPanel with a GridLayout, just type:

JPanel panel = new JPanel(new GridLayout(3, 2));

In this page and here there are some tutorials about JPanel and layouts.

There are two important facts to know. JFrame uses a BorderLayout by default. JPanel uses a Flowlayout by default. This is why you are seeing different behaviors depending on which component you are adding your other components to.

FlowLayout is nearly worthless so when using a JPanel you almost always want to change it. @Mark Peters shows you how to do that above. A BorderLayout is almost always perfect to use for a top-level container and I rarely change that. I tend to use BoxLayout for my nested JPanels almost exclusively.

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top