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 Unit test verison何かを取得し続けているので、私が間違っていることを見つけるのに本当に苦労しています。それはコードですか、それとも何か他のものですか?

4

1 に答える 1

0

まず、適切な Java 変数の命名規則を使用します。変数名は大文字で始めないでください。クラス名のみが大文字で始まるため、コードが読みにくくなります。クラスの静的メソッドを呼び出しているようです。

さて、あなたの問題について:

private JButton B1, B2, B3;

ボタンを作成することはないため、これらの変数は null です。

編集:

投稿されたコードに 3 行のコードを追加し (アクション リスナーを削除しました)、コードは正常に機能したので、何をしているのかわかりません。

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