框架不会显示我的面板

时间:2013-02-11 04:07:55

标签: java swing jframe jpanel

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

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单元测试verison的东西,所以真的很难找出我做错了什么。这是代码还是别的什么?

1 个答案:

答案 0 :(得分:0)

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

现在,针对您的问题:

private JButton B1, B2, B3;

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

编辑:

我刚刚为你发布的代码添加了3行代码(并删除了动作监听器),代码运行正常,所以我不确定你在做什么。

B1 = new JButton("B1"); // added the creation of the button   
B1.setMnemonic('c');