1.11M Members

Question about JPanel/JApplet/BoxLayout

 
0
 

Hello,

When I run the following program, the applet window displays nothing. I will be grateful for any help.

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

public class BoxPanel extends JPanel
{
   private JTextField label1, label2;
      
      
   public BoxPanel()
   {
         setLayout(new BoxLayout(this, BoxLayout.X_AXIS));
         label1 = new JTextField(20);
         label2 = new JTextField(20);
         add(label1);
         add(label2);
    }
    
    
    
}
import javax.swing.*;
import java.awt.*;
public class App extends JApplet
{
    private JLabel label1, label2;
    private BoxPanel panelA, panelB;
    
    public void init()
    {
        panelA = new BoxPanel();
        Container c = getContentPane();
        
        c.add(panelA);
             
        
    }
    
}

Thank you!

 
2
 

I can't see anywhere that you have constructed any frame or panel in which to add to the frame to then set it frame visible, you can also use a Container...

e.g.

JPanel p = new JPanel();
p.setLayout(new BoxLayout(p, BoxLayout.PAGE_AXIS));
p.add(label1);         
p.add(label2);

I would suggest you look at the BoxLayout or JFrames/Panels tutorials so you can see how a basic class is put together as a whole. Oracle uses a container or you can construct a frame and panel to then add the panel to it. Links provided below:

Box Layout Examplehttp://docs.oracle.com/javase/tutorial/displayCode.html?code=http://docs.oracle.com/javase/tutorial/uiswing/examples/layout/BoxLayoutDemoProject/src/layout/BoxLayoutDemo.java

Box Layouts http://docs.oracle.com/javase/tutorial/uiswing/layout/box.html

Panels http://docs.oracle.com/javase/tutorial/uiswing/components/panel.html#creating

Frames: http://docs.oracle.com/javase/tutorial/uiswing/components/frame.html

I would also make the lables public as you want to use them in a public method.

Hope that helps

 
1
 

It works for me. I see a vertical dividing line between the two JTextFields.

Try typing something into the text fields.
Try putting a String into them in the constructor.

You
This article has been dead for over six months: Start a new discussion instead
Post:
Start New Discussion
Tags Related to this Article