0

我正在尝试用 Java 制作一个 Store Register 应用程序(为了好玩),但我是该语言的新手,并且不完全确定如何从我所在的位置开始。目前,我成功创建了一个登录系统,所以当我点击登录时,用户登录工作。现在,我想这样做,以便当我单击登录时,它会将我带到一个选项菜单。当用户单击登录时,我不想打开一个新的 JFrame,而是希望一切都发生在一个 JFrame 中。另外,我不希望你们为我做这件事,但我想知道如何解决这个问题。

这是我有参考的代码:

package schoolstoreregister;


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

public class SchoolStoreRegister extends JFrame {

        //declaring our swing components
    JLabel l_name,l_pass;
    JTextField t_name;
    JPasswordField t_pass;     //A special JTextField but hides input text
    JButton button;
    Container c;
    boolean checkLogin = false;

    //a inner class to handling ActionEvents
    handler handle;

    //a separate class for processing database connection and authentication
    database db;    

    SchoolStoreRegister()
    {
        super("School Store Register");

        c=getContentPane();
        c.setLayout(new FlowLayout());
        Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
        int width = (int) screenSize.getWidth();
        int height = (int) screenSize.getHeight();

        //extra classes
        db=new database();
            handle =new handler();

                //swing components
        //l_name=new JLabel("Username");
        l_pass=new JLabel("Password");
        //t_name=new JTextField(10);
        t_pass=new JPasswordField(10);
        button=new JButton("Login");

        //adding actionlistener to the button
        button.addActionListener(handle);

        //add to contaienr
       // c.add(l_name);
        //c.add(t_name);
        c.add(l_pass);
        c.add(t_pass);
        c.add(button);
        //visual
        setVisible(true);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setSize(width,height);

    }
    public static void main(String args[])
    {
            @SuppressWarnings("unused")
            SchoolStoreRegister sample=new SchoolStoreRegister();
    }

    class handler implements ActionListener        //This is triggered whenever the user clicks the login button
    {
        public void actionPerformed(ActionEvent ae) //checks if the button clicked
        {
            if(ae.getSource()==button)
            {
                char[] temp_pwd=t_pass.getPassword();
                String convert = t_pass.getText();
                int passID = Integer.parseInt(convert);

                //The entered username and password are sent via "checkLogin()" which return boolean
                if(db.checkLogin(passID))
                {
                    //a pop-up box
                    JOptionPane.showMessageDialog(null, "You have logged in successfully","Success", JOptionPane.INFORMATION_MESSAGE);
                    checkLogin = true;
                }
                else
                {
                    //a pop-up box
                    JOptionPane.showMessageDialog(null, "Login failed!","Failed!!", JOptionPane.ERROR_MESSAGE);
                    checkLogin = false;
                }
            }//if
        }//method

    }//inner class
}
4

1 回答 1

3

尝试CardLayout在您的JFrame.

于 2013-08-28T01:21:49.510 回答