2
public class Test extends JFrame
{
    private static final long serialVersionUID = 1L;

    public static void main(String [] args)
    {

        EventQueue.invokeLater(new Runnable()
        {
            public void run()
            {
               MyPanel p = new MyPanel();
               p.setVisible(true);
            }

       });
   }
}

The Panel code is dictates how this MyPanel should look.

public class MyPanel extends JPanel 
{
private static final long serialVersionUID = 1L;
private JTextField txtUsername;

public MyPanel() 
{
    setLayout(null);

    JPanel panel = new JPanel();
    panel.setLayout(null);
    panel.setBorder(new EtchedBorder(EtchedBorder.LOWERED, null, null));
    panel.setBackground(SystemColor.control);
    panel.setBounds(0, 0, 500, 500);
    add(panel);

    ImageIcon icon= new ImageIcon("C:/Users/Admin/Desktop/testPic.jpg");
    JLabel wlabel = new JLabel(icon);
    wlabel.setBounds(20, 10, 400, 222);
    panel.add(wlabel);

    JPanel panel_1 = new JPanel();
    panel_1.setBounds(36, 244, 614, 159);
    panel.add(panel_1);
    panel_1.setLayout(null);

    JLabel lblUsername = new JLabel("Username:");
    lblUsername.setBounds(40, 40, 100, 20);
    panel_1.add(lblUsername);
    lblUsername.setFont(new Font("Tahoma", Font.PLAIN, 18));

    txtUsername = new JTextField();
    txtUsername.setBounds(179, 52, 195, 30);
    panel_1.add(txtUsername);
    txtUsername.setColumns(10);

    JButton btnSubmit = new JButton("SUBMIT");
    btnSubmit.setBounds(424, 65, 145, 44);
    panel_1.add(btnSubmit);
    btnSubmit.setFont(new Font("Tahoma", Font.PLAIN, 18));

    btnSubmit.addActionListener(new ActionListener() 
    {
        public void actionPerformed(ActionEvent arg0) 
        {
        }
    });
}
}

Why don't I see the actual panel? The code compiles and runs but I don't see anything on my screen.

Mike John
  • 818
  • 4
  • 11
  • 29

1 Answers1

2

You've got to add your JPanel to your JFrame. It's the JFrame that is the top level window that displays the entire GUI. Without a top level window either created directly, as described above, or indirectly, such as when you create a JOptionPane, the JPanel will never be seen.

So, rather than this:

public void run()
{
   MyPanel p = new MyPanel();
   p.setVisible(true);
}

Do this:

public void run()
{
   Test test = new Test();
   test.setVisible(true);
}

And then create your MyPanel in the Test constructor, and add it to Test there via a call to add(...).

Next we'll talk about why null layouts and setBounds(...) is a very bad thing.

Key tutorial links:

Hovercraft Full Of Eels
  • 283,665
  • 25
  • 256
  • 373
  • @MikeJohn: I could be snarky and ask if you've even looked at the tutorials, yet, but seriously, it's simply you call `add(...)` on your Test instance. And yeah, please read the tutorials. That's what they're there for. – Hovercraft Full Of Eels Nov 05 '13 at 04:24
  • @MikeJohn: Please check out my key tutorial links in my answer above. – Hovercraft Full Of Eels Nov 05 '13 at 04:28
  • You are right, I just started throwing code, which is a bad idea. I will read the links, thanks. One more question though; when you say "call add(...) which add is this? – Mike John Nov 05 '13 at 04:30
  • @MikeJohn Say you have a `JFrame` called `frame`. Then you call `frame.add(panel);` – Justin Nov 05 '13 at 04:33
  • @MikeJohn: As Quincunx states, if you create a JFrame instance, then call add on it. Otherwise if you're inside of the Test JFrame constructor, then call `add(...)` by itself as you'll be calling it on the `this` Test JFrame instance. For example, look at my code in my answer to another question [here](http://stackoverflow.com/a/6105437/522444). – Hovercraft Full Of Eels Nov 05 '13 at 04:34
  • 2
    I sort of assumed MikeJohn would know this since he is extending a class. (I personally think that everyone who uses extend/implements should know how to use interfaces and superclasses well. Also, anyone making objects should know how to refer to it with `this`). – Justin Nov 05 '13 at 04:36
  • Thank you both. That was very helpful and informative. I was able to run my little test code and get a feel of how panels work. I'm on my way to read those links. – Mike John Nov 05 '13 at 04:37