我需要我的程序的帮助,因为我想给我的程序输入密码和用户名,所以如果用户名是test && password是12345,我的程序将出现一个新的框架,但是不幸的是我的第二个标签和按钮在这里没有用到目前为止,我的代码是什么。

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

public class SwapFields extends JFrame

{

    public static void main(String[] args)
    {
        SwapFields frameTabel = new SwapFields();
    }

        JButton blogin = new JButton("Login");
        JPanel panel = new JPanel();
        JTextField txuser = new JTextField(15);
        JPasswordField pass = new JPasswordField(15);
        JLabel lab = new JLabel("Username :");
        JLabel pas = new JLabel("Password :");
        JLabel cos;
        //JPanel panel = new JPanel();
        JButton y1;
        JButton y2;

    SwapFields()
        {
            super("Enter Your Account !");
            setSize(300,200);
            setLocation(500,280);
            panel.setLayout (null);


            txuser.setBounds(90,30,150,20);
            pass.setBounds(90,65,150,20);
            blogin.setBounds(110,100,80,20);
            lab.setBounds(15,28,150,20);
            pas.setBounds(15,63,150,20);

            panel.add(lab);
            panel.add(pas);
            panel.add(blogin);
            panel.add(txuser);
            panel.add(pass);

            getContentPane().add(panel);
            setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            setVisible(true);
            actionlogin();
        }

            public void actionlogin()
        {
            blogin.addActionListener(new ActionListener()
            {
        public void actionPerformed(ActionEvent ae)
             {
                String puname = txuser.getText();
                String ppaswd = pass.getText();
                if(puname.equals("test") && ppaswd.equals("12345"))
            {

                JFrame frame = new JFrame("Customer");
                JPanel panel1 = new JPanel();

                frame.setVisible(true);
                frame.setSize(300,200);
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);


                    cos = new JLabel("Do you have a Customer ?");
                    y1 = new JButton("Yes");
                    y2 = new JButton("No");


                        panel1.setLayout(null);
                        cos.setBounds(70,30,150,20);
                        y1.setBounds(80,65,150,20);
                        y2.setBounds(140,65,150,20);

                        y1.setSize(55,30);
                        y2.setSize(55,30);
                        panel1.add(y1);
                        panel1.add(y2);
                        panel1.add(cos);
                        getContentPane().add(panel1);
//

                    dispose();
            }
            else
                {

                    JOptionPane.showMessageDialog(null,"Wrong Password / Username");
                    txuser.setText("");
                    pass.setText("");
                    txuser.requestFocus();
                }

            }
        });
    }
}

最佳答案

您将新控件/面板等添加到原始框架,而不是新框架。

getContentPane().add(panel1);


应该可能正在访问新框架中的内容窗格...

10-07 16:08
查看更多