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!

Recommended Answers

All 2 Replies

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

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.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.