0

btn3 is not showing when I run it in Netbeans. when i ran it in NotePad it shows third button. i cant really figure what the problem is.

import javax.swing.*;

import java.awt.*;

import java.awt.event.*;

public class Regform2 extends JFrame implements ActionListener {

    JLabel l1, l2, l3, l4, l5, l6, l7, l8;
    JTextField tf1, tf2, tf5, tf6, tf7;
    JButton btn1, btn2, btn3;
    JPasswordField p1, p2;

    Regform2() {

        setTitle("Registration Form in Java");

        l1 = new JLabel("Registration Form in Windows Form:");
        l1.setForeground(Color.blue);
        l1.setFont(new Font("Serif", Font.BOLD, 20));

        l2 = new JLabel("Name:");
        l3 = new JLabel("Email-ID:");
        l4 = new JLabel("Create Passowrd:");
        l5 = new JLabel("Confirm Password:");
        l6 = new JLabel("Country:");
        l7 = new JLabel("State:");
        l8 = new JLabel("Phone No:");
        tf1 = new JTextField();
        tf2 = new JTextField();
        p1 = new JPasswordField();
        p2 = new JPasswordField();
        tf5 = new JTextField();
        tf6 = new JTextField();
        tf7 = new JTextField();

        btn1 = new JButton("Submit");
        btn2 = new JButton("Clear");
        btn3 = new JButton("nxt");

        btn1.addActionListener(this);
        btn2.addActionListener(this);
        btn3.addActionListener(this);

        l1.setBounds(100, 30, 400, 30);
        l2.setBounds(80, 70, 200, 30);
        l3.setBounds(80, 110, 200, 30);
        l4.setBounds(80, 150, 200, 30);
        l5.setBounds(80, 190, 200, 30);
        l6.setBounds(80, 230, 200, 30);
        l7.setBounds(80, 270, 200, 30);
        l8.setBounds(80, 310, 200, 30);
        tf1.setBounds(300, 70, 200, 30);
        tf2.setBounds(300, 110, 200, 30);
        p1.setBounds(300, 150, 200, 30);
        p2.setBounds(300, 190, 200, 30);
        tf5.setBounds(300, 230, 200, 30);
        tf6.setBounds(300, 270, 200, 30);
        tf7.setBounds(300, 310, 200, 30);
        btn1.setBounds(50, 350, 100, 30);
        btn2.setBounds(170, 350, 100, 30);
        btn3.setBounds(300, 350, 100, 30);
        add(l1);
        add(l2);
        add(tf1);
        add(l3);
        add(tf2);
        add(l4);
        add(p1);
        add(l5);
        add(p2);
        add(l6);
        add(tf5);
        add(l7);
        add(tf6);
        add(l8);
        add(tf7);
        add(btn1);
        add(btn2);
        add(btn3);
        setVisible(true);
        setSize(700, 700);
        setLayout(null);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }

    public void actionPerformed(ActionEvent e) {
        if (e.getSource() == btn1) {
            int x = 0;
            String s1 = tf1.getText();
            String s2 = tf2.getText();

            char[] s3 = p1.getPassword();
            char[] s4 = p2.getPassword();
            String s8 = new String(s3);
            String s9 = new String(s4);

            String s5 = tf5.getText();
            String s6 = tf6.getText();
            String s7 = tf7.getText();
            if (s8.equals(s9)) {
                JOptionPane.showMessageDialog(btn1, "Data Saved Successfully");
            } else {
                JOptionPane.showMessageDialog(btn1, "Password Does Not Match");
            }



        } else if (e.getSource() == btn2) {
            tf1.setText("");
            tf2.setText("");
            p1.setText("");
            p2.setText("");
            tf5.setText("");
            tf6.setText("");
            tf7.setText("");


        } else {

            this.dispose();
            NewJFrame nw = new NewJFrame();
            nw.setVisible(true);


        }
    }

    public static void main(String args[]) {
        new Regform2();
    }}
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
DAVE
  • 125
  • 1
  • 10
  • 2
    You're null layout isn't going to help. Between the frame been made visible and y calling setLayout, the container Amy be validated and the components laid out, which could be screwing with your attempt to do without it – MadProgrammer Sep 02 '15 at 09:16
  • You shouldn't `setLayout(null)`, but a `LayoutManager`, add to `getContentPane()` and call `pack` before making the `JFrame` visible. – Kalle Richter Sep 02 '15 at 09:16
  • 2
    `setLayout(null);` That's the problem. Java GUIs have to work on different OS', screen size, screen resolution etc. using different PLAFs in different locales. As such, they are not conducive to pixel perfect layout. Instead use layout managers, or [combinations of them](http://stackoverflow.com/a/5630271/418556) along with layout padding and borders for [white space](http://stackoverflow.com/a/17874718/418556). – Andrew Thompson Sep 02 '15 at 09:17
  • 1
    There's a (very good) reason why the JPasswordField returns a char array, I'd do some investigation into it and how you can use it to your advantage... – MadProgrammer Sep 02 '15 at 09:18
  • You example doesn't compile. `NewJFrame` isn't known. Please edit. – Kalle Richter Sep 02 '15 at 09:18
  • @Andrew Thompson I removed setLayout(null) it is giving wierd results everytime. – DAVE Sep 02 '15 at 09:36
  • BTW - what is the intended layout of the buttons? They seem to be 'left aligned' but I would expect them to be centered. – Andrew Thompson Sep 02 '15 at 09:36
  • *"..it is giving wierd results everytime."* There is more to using layouts than just **not** setting the layout to `null`. Provide ASCII art or a simple drawing of the *intended* layout of the GUI at minimum size, and if resizable, with more width and height. – Andrew Thompson Sep 02 '15 at 09:37
  • @Karl Richter I did these changes on your suggestion this.pack(); setVisible(true); //setLayout(null); now it shows only btn3 in the entire window nothing else – DAVE Sep 02 '15 at 09:37

3 Answers3

0

if you put setlayout(null) at first then setVisible(true),the btn3 will show in jframe. like this:

    setLayout(null);
    setVisible(true);
    setSize(700, 700);

    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Rafiq
  • 740
  • 1
  • 5
  • 17
0

You need to add a JPanel. To this panel you add your components. And don't forget to add your panel to your frame like this:

    JPanel contentPane = new JPanel();
    content.add(new JButton("Test"));

    this.setDefaultCloseOperation(3);
    this.setContentPane(contentPanel);
    this.setSize(Width, Height);
    this.setResizable(false);
    this.setVisible(true);
R. Suntjens
  • 121
  • 6
0

this code will work... please define the codes of main frame at beginning of the constructor, such as setTitle(); setSize(); setLayout();

import javax.swing.*;

import java.awt.*;

import java.awt.event.*;

public class Regform2 extends JFrame implements ActionListener {

JLabel l1, l2, l3, l4, l5, l6, l7, l8;
JTextField tf1, tf2, tf5, tf6, tf7;
JButton btn1, btn2, btn3;
JPasswordField p1, p2;

Regform2() {

    setTitle("Registration Form in Java");
    setSize(700, 700);
    setLayout(null);

    l1 = new JLabel("Registration Form in Windows Form:");
    l1.setForeground(Color.blue);
    l1.setFont(new Font("Serif", Font.BOLD, 20));

    l2 = new JLabel("Name:");
    l3 = new JLabel("Email-ID:");
    l4 = new JLabel("Create Passowrd:");
    l5 = new JLabel("Confirm Password:");
    l6 = new JLabel("Country:");
    l7 = new JLabel("State:");
    l8 = new JLabel("Phone No:");
    tf1 = new JTextField();
    tf2 = new JTextField();
    p1 = new JPasswordField();
    p2 = new JPasswordField();
    tf5 = new JTextField();
    tf6 = new JTextField();
    tf7 = new JTextField();

    btn1 = new JButton("Submit");
    btn2 = new JButton("Clear");
    btn3 = new JButton("Next");

    btn1.addActionListener(this);
    btn2.addActionListener(this);
    btn3.addActionListener(this);

    l1.setBounds(100, 30, 400, 30);
    l2.setBounds(80, 70, 200, 30);
    l3.setBounds(80, 110, 200, 30);
    l4.setBounds(80, 150, 200, 30);
    l5.setBounds(80, 190, 200, 30);
    l6.setBounds(80, 230, 200, 30);
    l7.setBounds(80, 270, 200, 30);
    l8.setBounds(80, 310, 200, 30);
    tf1.setBounds(300, 70, 200, 30);
    tf2.setBounds(300, 110, 200, 30);
    p1.setBounds(300, 150, 200, 30);
    p2.setBounds(300, 190, 200, 30);
    tf5.setBounds(300, 230, 200, 30);
    tf6.setBounds(300, 270, 200, 30);
    tf7.setBounds(300, 310, 200, 30);
    btn1.setBounds(50, 350, 100, 30);
    btn2.setBounds(170, 350, 100, 30);
    btn3.setBounds(290, 350, 100, 30);
    add(l1);
    add(l2);
    add(tf1);
    add(l3);
    add(tf2);
    add(l4);
    add(p1);
    add(l5);
    add(p2);
    add(l6);
    add(tf5);
    add(l7);
    add(tf6);
    add(l8);
    add(tf7);
    add(btn1);
    add(btn2);
    add(btn3);
    setVisible(true);

    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}

public void actionPerformed(ActionEvent e) {
    if (e.getSource() == btn1) {
        int x = 0;
        String s1 = tf1.getText();
        String s2 = tf2.getText();

        char[] s3 = p1.getPassword();
        char[] s4 = p2.getPassword();
        String s8 = new String(s3);
        String s9 = new String(s4);

        String s5 = tf5.getText();
        String s6 = tf6.getText();
        String s7 = tf7.getText();
        if (s8.equals(s9)) {
            JOptionPane.showMessageDialog(btn1, "Data Saved Successfully");
        } else {
            JOptionPane.showMessageDialog(btn1, "Password Does Not Match");
        }



    } else if (e.getSource() == btn2) {
        tf1.setText("");
        tf2.setText("");
        p1.setText("");
        p2.setText("");
        tf5.setText("");
        tf6.setText("");
        tf7.setText("");


    } else {

        this.dispose();
        //NewJFrame nw = new NewJFrame();
        //nw.setVisible(true);


    }
}

public static void main(String args[]) {
    new Regform2();
}

}

ccc
  • 370
  • 5
  • 19