0

我无法在我创建的框架类上显示我的面板类。这是我的面板和框架

public class PasswordCheckerPanel extends JPanel 
{
    private String string;
    private JButton B1, B2, B3;
    private JLabel Rules, L1, L2, L3, Type, Retype;
    private JTextField TF1, TF2;
    private ArrayList<String> AR1 = new ArrayList<String>();
    private ArrayList<String> AR2 = new ArrayList<String>();
    private JFileChooser Input, Output;


    public PasswordCheckerPanel()
    {
        this.setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));
        JPanel Panel1 = new JPanel();
        TitledBorder Titled1 = BorderFactory.createTitledBorder("Password Checker Rules");
        Rules = new JLabel();
        Rules.setText("Use the following rules when creating your passwords");
        L1= new JLabel();
        L1.setText("1. Length must be greater than 6.");
        L2 = new JLabel();
        L2.setText("2. Must contain atleast one alpha character");
        L3 = new JLabel();
        L3.setText("3. Must contain atleast one numeric character");
        Panel1.setBorder(Titled1);
        Panel1.add(Rules);
        Panel1.add(L1);
        Panel1.add(L2);
        Panel1.add(L3);
        add(Panel1);

        JPanel Panel2 = new JPanel();
        Type = new JLabel();
        Type.setText("Password: ");
        Retype = new JLabel();
        Retype.setText("Re-Type Password: ");
        TF1 = new JTextField(20);
        TF2 = new JTextField(20);
        Panel2.add(Type);
        Panel2.add(TF1);
        Panel2.add(Retype);
        Panel2.add(TF2);

        add(Panel2);

        JPanel Panel3 = new JPanel();
        B1.setMnemonic('c');
        B1.setText("Check Password");
        B1.setToolTipText("Check Password");
        B1.addActionListener(new ButtonListener());

        B2.setMnemonic('f');
        B2.setText("Check Password in File");
        B2.setToolTipText("Check Password in File");
        B2.addActionListener(new ButtonListener());

        B3.setMnemonic('e');
        B3.setText("Exit");
        B3.setToolTipText("Exit");
        B3.addActionListener(new ButtonListener());

        Panel3.add(B1);
        Panel3.add(B2);
        Panel3.add(B3);

        add(Panel3);


    }

}

这是我的框架

public class PasswordCheckerFrame extends JFrame
{
    private PasswordCheckerPanel Panel = new PasswordCheckerPanel(); // Creates a PasswordCheckerPanel object

    public PasswordCheckerFrame() // PasswordCheckerFrame Constructor
    {

            JFrame WindowBox = new JFrame(); // Creates a new JFrame

            WindowBox.setTitle("Password Checker"); // sets JFrame title
            WindowBox.setSize(500, 500); // sets JFrame size
            WindowBox.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // sets the operation as to what happens when you close the gui
            WindowBox.add(Panel); // adds the Panel to the JFrame
            WindowBox.pack(); // Packs for size refitting
            WindowBox.setVisible(true); // sets visibility of the Frame

        }
        public static void main(String[] args)
        {
            new PasswordCheckerFrame(); // creates an instance of the PasswordCheckerFrame class
        }

    }

你能指出我可能犯的任何错误吗?我不断得到一个 J 单元测试版本,所以真的很难找出我做错了什么。是代码还是其他?

4

1 回答 1

0

首先使用正确的 Java 变量命名约定。变量名不以大写字符开头。只有类名以大写字符开头,因此您的代码难以阅读。看起来您正在调用一个类的静态方法。

现在,对于您的问题:

private JButton B1, B2, B3;

您永远不会创建按钮,因此这些变量为空。

编辑:

我刚刚向您发布的代码添加了 3 行代码(并删除了动作侦听器)并且代码运行良好,所以我不确定您在做什么。

B1 = new JButton("B1"); // added the creation of the button   
B1.setMnemonic('c');
于 2013-02-11T04:38:13.913 回答