1

please help me. I'm a beginner in java. This is my StartUp_page button code. When ok button is clicked, it should call AdminLogin. It display 1 but AdminLogin didn't get called.

StartUp_page

btnOk.addActionListener(new ActionListener(){
            public void actionPerformed(ActionEvent e){
                String input = textField.getText();
                if (input.equals("1"))
                {
                    AdminLogin login=new AdminLogin();
                    login.setVisible(true);
                    setVisible(false);
                    System.out.println(input);
                    //dispose();
                }
                else
                {
                    System.out.println("2");                }
            }

AdminLogin

  package GUI;

import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.border.EmptyBorder;

public class AdminLogin extends JFrame{


    private JTextField textField;
    private JTextField textField_1;
    private JPanel contentPanel;

    /**
     * Launch the application.
     */
    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    AdminLogin frame = new AdminLogin();
                    frame.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

    /**
     * Create the application.
     */
    public AdminLogin() {
        initialize();
    }

    /**
     * Initialize the contents of the frame.
     */
    private void initialize() {
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setBounds(100,100,350,300);
        contentPanel= new JPanel();
        contentPanel.setBorder(new EmptyBorder(5,5,5,5));
        setContentPane(contentPanel);
        contentPanel.setLayout(null);


        JLabel lblName = new JLabel("Name");
        lblName.setBounds(53, 69, 46, 14);
        contentPanel.add(lblName);

        textField = new JTextField();
        textField.setBounds(119, 66, 121, 20);
        contentPanel.add(textField);
        textField.setColumns(10);

        JLabel lblPassword = new JLabel("Password");
        lblPassword.setBounds(53, 128, 46, 14);
        contentPanel.add(lblPassword);

        textField_1 = new JTextField();
        textField_1.setBounds(119, 125, 121, 20);
        contentPanel.add(textField_1);
        textField_1.setColumns(10);

        JButton btnNewButton = new JButton("OK");
        btnNewButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent arg0) {
            }
        });
        btnNewButton.setBounds(211, 183, 89, 23);
        contentPanel.add(btnNewButton);
    }

    public void setVisible(boolean b) {
        // TODO Auto-generated method stub

    }

}
John
  • 13
  • 4
  • 1
    Start by having a look at [How to Use CardLayout](http://docs.oracle.com/javase/tutorial/uiswing/layout/card.html) and then have a look at [Model-View-Controller](http://en.wikipedia.org/wiki/Model%E2%80%93view%E2%80%93controller) and [How to Make Dialogs](http://docs.oracle.com/javase/tutorial/uiswing/components/dialog.html) probably won't hurt. – MadProgrammer Feb 20 '16 at 09:34

1 Answers1

1

The basic problem is, you're AdminLogin class is extending from JFrame but creating a second instance of JFrame from within the class, so which frame is actually been displayed?! Personally, I'd start by replacing extends JFrame with extends JPanel and simply add the panel to an instance of a JFrame or JDialog as required

You really should avoid using null layouts, pixel perfect layouts are an illusion within modern ui design. There are too many factors which affect the individual size of components, none of which you can control. Swing was designed to work with layout managers at the core, discarding these will lead to no end of issues and problems that you will spend more and more time trying to rectify

Have a look at Laying Out Components Within a Container for some details.

You might also like to have a look at The Use of Multiple JFrames, Good/Bad Practice? and How to Make Dialogs and How to Use CardLayout for some ideas about alternatives

Community
  • 1
  • 1
MadProgrammer
  • 343,457
  • 22
  • 230
  • 366