2013-02-11 114 views
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的東西,所以真的很難找出我做錯了什麼。它是代碼還是其他的東西?

回答

0

首先使用適當的Java變量命名約定。變量名不能以上下角色開始。只有類名以大寫字母開頭,所以你的代碼難以閱讀。看起來你正在調用一個類的靜態方法。現在

,對於您的問題:

private JButton B1, B2, B3; 

你永遠不會創建按鈕,以便這些變量是空的。

編輯:

我只是增加了3行代碼到你貼的代碼(和刪除的動作監聽器)和代碼工作得很好,所以我不知道你在做什麼。

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

啊對不起約會的事情。感謝您的幫助。 – user1873746 2013-02-11 05:26:31

+0

我做了你建議的事情,但它仍然沒有在窗口框架上顯示我的面板,地獄一個windowFrame甚至沒有顯示出來,它一直在終止。 – user1873746 2013-02-11 06:08:29

+0

@ user1873746,請參閱編輯。如果您仍有問題,請發佈SSCCE。 – camickr 2013-02-11 16:58:48