0

When I run this code ( having simple buttons and a LOGIN button with an action listener), it terminates without running and without showing screen.

I have tried System.exit(0); in main function to overcome this terminating issue but all in vain

   public class HOme extends JFrame{

Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
int width = (int) screenSize.getWidth();
int height = (int) screenSize.getHeight();
Color cardinal = new Color(194, 35, 38);
int w=155;
int h=50;
public HOme(String title) {

    super(title);

    getContentPane().setSize(width,height);
    getContentPane().setBackground(Color.WHITE);
    getContentPane().setLayout(null);


    final JPanel panel2 = new JPanel();
    panel2.setBounds(364, 33, 664, 344);
    getContentPane().add(panel2);

    JPanel panel3 = new JPanel();
    panel3.setBackground(Color.WHITE);
    panel3.setBounds(81, 382, 947, 243);
    getContentPane().add(panel3);
    panel3.setLayout(null);

    JButton btnHome = new JButton("Home");
    btnHome.setFont(new Font("Times New Roman", Font.PLAIN, 20));
    btnHome.setForeground(Color.WHITE);
    btnHome.setBackground(cardinal);
    btnHome.setBounds(517, 33, w, h);
    btnHome.setContentAreaFilled(false);
    btnHome.setOpaque(true);
    panel3.add(btnHome);

    JButton btnClients = new JButton("Clients");
    btnClients.setFont(new Font("Times New Roman", Font.PLAIN, 20));
    btnClients.setForeground(Color.WHITE);
    btnClients.setBounds(690, 33, w, h);
    btnClients.setBackground(cardinal);
    btnClients.setContentAreaFilled(false);
    btnClients.setOpaque(true);
    panel3.add(btnClients);

    JButton btnClose = new JButton("Close");
    btnClose.setFont(new Font("Times New Roman", Font.PLAIN, 20));
    btnClose.setForeground(Color.WHITE);
    btnClose.setBounds(690, 198, w, h);
    btnClose.setBackground(cardinal);
    btnClose.setContentAreaFilled(false);
    btnClose.setOpaque(true);
    panel3.add(btnClose);

    JButton btnLogin = new JButton("Admin Login");
    btnLogin.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent arg0) {
            Login l=new Login();
            panel2.add(l);
        }
    });
    btnLogin.setFont(new Font("Times New Roman", Font.PLAIN, 20));
    btnLogin.setForeground(Color.WHITE);
    btnLogin.setBounds(517, 116, w, h);
    btnLogin.setBackground(cardinal);
    btnLogin.setContentAreaFilled(false);
    btnLogin.setOpaque(true);
    panel3.add(btnLogin);

    JPanel panel1 = new JPanel();
    panel1.setBorder(new EtchedBorder(EtchedBorder.LOWERED, new Color(204,       51, 0), null));
    panel1.setBackground(Color.WHITE);
    panel1.setBounds(81, 33, 263, 344);
    getContentPane().add(panel1);
    panel1.setLayout(null);


    JButton btnStartMonitoring = new JButton("");
    btnStartMonitoring.setIcon(new ImageIcon(path1));
    btnStartMonitoring.setBackground(cardinal);

    btnStartMonitoring.setForeground(Color.WHITE);
    btnStartMonitoring.setFont(new Font("Tahoma", Font.PLAIN, 15));
    btnStartMonitoring.setBounds(10, 274, 239, 59);

    panel1.add(btnStartMonitoring);

    JLabel lblLogo = new JLabel("New label");
    lblLogo.setIcon(new ImageIcon(path2));
    lblLogo.setBounds(0, 11, 263, 253);
    panel1.add(lblLogo);

}


public static void main(String args[]) {

      new HOme("HOme");
        //System.exit(0);
}

}

Edited

I have a login class extended from JPanel. When I click on Login Button from Home. It is not showing Login panel Login.class

   public class Login extends JPanel {
   private JTextField txtPassword;
   private JTextField txtID;
   Color cardinal = new Color(194, 35, 38);
   int w=155;
   int h=50;
   public Login() {
    setBackground(Color.WHITE);
setLayout(null);

JLabel lblLogin = new JLabel("Login   ");
lblLogin.setBackground(Color.ORANGE);
lblLogin.setHorizontalAlignment(SwingConstants.RIGHT);
lblLogin.setFont(new Font("Trajan Pro", Font.BOLD, 36));
lblLogin.setBounds(125, 0, 424, 59);
lblLogin.setBackground(cardinal);
//lblLogin.setContentAreaFilled(false);
lblLogin.setOpaque(true);
lblLogin.setForeground(Color.white);
add(lblLogin);

JLabel lblId = new JLabel("ID");
lblId.setHorizontalAlignment(SwingConstants.RIGHT);
lblId.setFont(new Font("Tekton Pro", Font.PLAIN, 23));
lblId.setBounds(181, 127, 66, 28);
add(lblId);

JLabel lblPassword = new JLabel("Password");
lblPassword.setHorizontalAlignment(SwingConstants.RIGHT);
lblPassword.setFont(new Font("Tekton Pro", Font.PLAIN, 23));
lblPassword.setBounds(136, 188, 111, 28);
add(lblPassword);

txtPassword = new JTextField();
lblPassword.setLabelFor(txtPassword);
txtPassword.setBounds(266, 183, 256, 41);
lblPassword.setForeground(cardinal);
add(txtPassword);
txtPassword.setColumns(10);

txtID = new JTextField();
lblId.setLabelFor(txtID);
txtID.setBounds(266, 123, 256, 39);
lblId.setForeground(cardinal);
add(txtID);
txtID.setColumns(10);

JButton btnLogin = new JButton("Login");
btnLogin.setForeground(Color.WHITE);
btnLogin.setFont(new Font("Times New Roman", Font.PLAIN, 20));
btnLogin.setBounds(324, 294, w, h);
btnLogin.setBackground(cardinal);
btnLogin.setContentAreaFilled(false);
btnLogin.setOpaque(true);
add(btnLogin);
setVisible(true);

}

0

3 Answers 3

1

You are not making your JFrame visible.

You can do either -

  1. In your constructor, make it visible by adding the following line at the end -

    setVisible(true);
    
  2. Or in your main() function you can do -

    HOme h = new HOme("HOme");
    h.setVisible(true);
    
Sign up to request clarification or add additional context in comments.

5 Comments

Oh yes. Thank you. Now it is working. One more Question please. I have a login class extended from JPanel. When I click on Login Button from Home. It is not showing Login panel, even it is not executing print statements inside action listener or btnLogin. Please Help.
Are you clicking on the Admin Login button?
Can you put a System.out.println inside the action listener and see if it prints anything.
No it does not print anything from inside the action listener
0

Answer to second part:

  1. Import MouseListener, import java.awt.event.MouseListener;

  2. Construct a MouseListener somewhere, include action of the button

  3. Where you define btnLogin, add a line btnLogin.addMouseListener(<name of MouseListener>);

An example: http://www.java2s.com/Code/Java/Swing-JFC/ButtonActionSample.htm

Comments

0

Add something like setVisible(true); at the end of the HOme method.

1 Comment

Oh yes. Thank you. This is my Login Class. When I click on Login Button from Home. It is not showing Login panel. Please Help.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.