-3

In java, I made a registration form using swing, the program gets compiled but when I run it no new window opens with the registration form that i created. And in command prompt also the cursor goes to the next command line as if the program has been executed and form window closed by the user. the code:

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

class M {

    JFrame f;
    JPanel p;
    JButton b1, b2;
    TextField t;
    JLabel l1, l2;

    M() {
        f = new JFrame("FIRST");
        p = new JPanel();
        f.getContentPane().add(p);
        p.setVisible(true);
        b1 = new JButton("Save");
        b1.setBounds(10, 10, 10, 10);
        p.add(b1);
        b2 = new JButton("Exit");
        b2.setBounds(25, 10, 10, 10);
        p.add(b2);
    }

    public static void main(String arr[]) {
        M m1 = new M();
    }
}
mKorbel
  • 109,525
  • 20
  • 134
  • 319
Crate
  • 1
  • 2

2 Answers2

1

You need to set the JFrame to visible ,not the JPanel.

In your case f , not p.

user3224416
  • 522
  • 5
  • 15
1

Set the frame to Visible

f.setVisible(true);

Also to set the size correctly use

f.pack();
Revive
  • 2,248
  • 1
  • 16
  • 23
  • whats the difference between f.setsize() and f.pack()? – Crate Jun 26 '14 at 15:27
  • @Crate Check out this related question for the answer http://stackoverflow.com/questions/6384702/setsize-v-s-setpreferredsize-and-pack – Revive Jun 26 '14 at 15:29