2012-10-19 104 views
2

不久之前,我提出了一個類似的問題,那就是在改進代碼之後,我在使用CardLayout時遇到了一個障礙,在JFrame中爲JPanel使用多個類。將JPanel傳遞給CardLayout時發生NullPointerException

當tring顯示我的「2」屏幕,這是一個BaseScreen,我稍後將使用其他類來繼承它的屬性時,我得到一個NullPointerException。從

CardLayout cardL; 

我的構造器之外更改此代碼,

final CardLayout cardL = new CardLayout(); 

後,我現在拿不出JPanels出現!

這裏是我的代碼:

登錄類別:

/*Login Screen class for allowing 
multiple levels of access and security*/ 

//Imports library files 
import javax.swing.*; 
import java.awt.event.*; 
import java.awt.*; 
import java.io.*; 
import java.util.*; 

//Creates a LoginScreen class that extends the JFrame library class 
class LoginScreen extends JFrame { 

    //Creates a swing components and CardLayout for organising JPanels 
JFrame container = this; 
JPanel cardScreen; 
JPanel screen = new JPanel(); 
Image ProgramIcon = Toolkit.getDefaultToolkit().getImage("imageIco.png"); 
ImageIcon logo = new ImageIcon ("Logo.png"); 
JLabel icon = new JLabel(logo); 
JLabel username = new JLabel("Username"); 
JLabel password = new JLabel("Password"); 
JTextField user = new JTextField(18); 
JPasswordField pass = new JPasswordField(18); 
JButton login = new JButton("Login"); 
JLabel errorInfo = new JLabel(""); 
int WIDTH = 800; 
int HEIGHT = 500; 
int currentPanel = 1; 

public static void main(String[] args){ 
//Sets the GUI (Look and Feel) to the NimROD theme 
    try {UIManager.setLookAndFeel("com.nilo.plaf.nimrod.NimRODLookAndFeel");} 
    catch (UnsupportedLookAndFeelException e){ JOptionPane.showMessageDialog(null, "GUI Load Error: Unsupported");} 
    catch (ClassNotFoundException e) { JOptionPane.showMessageDialog(null, "GUI Load Error: NimROD Missing");} 
    catch (InstantiationException e) { JOptionPane.showMessageDialog(null, "GUI Load Error: Instantiation Missing");} 
    catch (IllegalAccessException e) { JOptionPane.showMessageDialog(null, "GUI Load Error: Illegal Access"); } 

//Creates a new LoginScreen via the LoginScreen method 
    LoginScreen LS = new LoginScreen(); 
} 

public LoginScreen(){ 
//Adds the JPanel to the JFrame and set the JFrame's properties 
//Sets the main JPanel to CardLayout platform and adds other JPanels it 
    final CardLayout cardL = new CardLayout(); 
    cardScreen = new JPanel(); 
    cardScreen.setLayout(cardL); 
    cardScreen.add(screen, "1");; 
    BaseScreen base = new BaseScreen(); 
    cardScreen.add(base, "2"); 
    container.setIconImage(ProgramIcon); 
    container.setTitle("Login"); 
    container.setSize(WIDTH,HEIGHT); 
    container.setResizable(false); 
    container.setVisible(true); 
    container.add(screen); 
    container.setDefaultCloseOperation(EXIT_ON_CLOSE); 

//Place the components on the JPanel and set there absolute posistions 
    screen.setLayout(null); 
    screen.add(username); 
    screen.add(password); 
    screen.add(user); 
    screen.add(pass); 
    screen.add(login); 
    screen.add(icon); 
    Dimension iconSize = icon.getPreferredSize(); 
    Dimension usernameSize = username.getPreferredSize(); 
    Dimension passwordSize = password.getPreferredSize(); 
    Dimension loginSize = login.getPreferredSize(); 
    Dimension userSize = user.getPreferredSize(); 
    Dimension passSize = pass.getPreferredSize(); 
    username.setBounds(252,170,usernameSize.width,usernameSize.height); 
    password.setBounds(495,170,passwordSize.width,passwordSize.height); 
    user.setBounds(180,200,userSize.width,userSize.height); 
    pass.setBounds(420,200,passSize.width,passSize.height); 
    login.setBounds(375,250,loginSize.width,loginSize.height); 
    icon.setBounds(250,50,iconSize.width,iconSize.height); 

    login.addActionListener(new ActionListener() { 
     public void actionPerformed(ActionEvent ae) { 
      cardL.show(cardScreen,"2"); 
      } 
     }); 
} 

BaseScreen類:

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


class BaseScreen extends JPanel{ 

JPanel screen = this; 
JButton logout = new JButton("Logout"); 
ImageIcon title = new ImageIcon("title.png"); 
JLabel header = new JLabel(title); 

public BaseScreen(){ 

    screen.setVisible(true); 
    screen.setLayout(null); 
    screen.add(logout); 
    screen.add(header); 
    Dimension headerSize = header.getPreferredSize(); 
    Dimension logoutSize = logout.getPreferredSize(); 
    logout.setBounds(720,440,logoutSize.width,logoutSize.height); 
    header.setBounds(0,0,headerSize.width,headerSize.height); 

    ButtonHandler handle = new ButtonHandler(); 
    logout.addActionListener(handle); 
} 

    public class ButtonHandler implements ActionListener{ 

    public void actionPerformed(ActionEvent event){ 

     if (event.getSource() == logout){ 

     } 
    } 
    } 

} 

預先感謝任何幫助/提示。

這裏的舊線:Using multiple classes with the same JFrame

編輯:

這裏的錯誤:

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException 
at LoginScreen$1.actionPerformed(LoginScreen.java:84) 
at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:1995) 
at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2318) 
at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:387) 
at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:242) 
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:236) 
at java.awt.AWTEventMulticaster.mouseReleased(AWTEventMulticaster.java:272) 
at java.awt.Component.processMouseEvent(Component.java:6288) 
at javax.swing.JComponent.processMouseEvent(JComponent.java:3267) 
at java.awt.Component.processEvent(Component.java:6053) 
at java.awt.Container.processEvent(Container.java:2041) 
at java.awt.Component.dispatchEventImpl(Component.java:4651) 
at java.awt.Container.dispatchEventImpl(Container.java:2099) 
at java.awt.Component.dispatchEvent(Component.java:4481) 
at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4577) 
at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4238) 
at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4168) 
at java.awt.Container.dispatchEventImpl(Container.java:2085) 
at java.awt.Window.dispatchEventImpl(Window.java:2478) 
at java.awt.Component.dispatchEvent(Component.java:4481) 
at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:643) 
at java.awt.EventQueue.access$000(EventQueue.java:84) 
at java.awt.EventQueue$1.run(EventQueue.java:602) 
at java.awt.EventQueue$1.run(EventQueue.java:600) 
at java.security.AccessController.doPrivileged(Native Method) 
at java.security.AccessControlContext$1.doIntersectionPrivilege(AccessControlContext.java:87) 
at java.security.AccessControlContext$1.doIntersectionPrivilege(AccessControlContext.java:98) 
at java.awt.EventQueue$2.run(EventQueue.java:616) 
at java.awt.EventQueue$2.run(EventQueue.java:614) 
at java.security.AccessController.doPrivileged(Native Method) 
at java.security.AccessControlContext$1.doIntersectionPrivilege(AccessControlContext.java:87) 
at java.awt.EventQueue.dispatchEvent(EventQueue.java:613) 
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:269) 
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:184) 
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:174) 
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:169) 
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:161) 
at java.awt.EventDispatchThread.run(EventDispatchThread.java:122) 
+2

請顯示您的異常堆棧跟蹤。理想情況下,將您的示例代碼縮減爲一個簡短但完整的示例,展示該問題。 –

+0

謝謝,我現在沒有太多時間,但我會盡快減少代碼。只是學習StackOverflow的繩索。 – Zalx

回答

2

我不知道你的NullPointerException,但你的例子是行不通的。

class LoginScreen extends JFrame 

這是非常罕見的需要不斷從頂層容器延伸,像JFrame,這是不必要的,只是事情複雜化,使用類似JPanel,而不是將其添加到您創建了一個框架...

JFrame container = this; 

恕我直言,這是一個壞主意。它只是混淆了這個問題。如果您確實需要某種方法來引用父對象,請直接使用this ...

在向框架添加組件之前,您可以調用container.setVisible(true),這絕不是一個好主意。事實上,當我正確加載程序(從美國東部時間內開始),沒有出現在框架上 - 但是,這可能是由於下一個問題......

創建cardScreen,創建screen,你加screencardScreen然後你直接添加screen到框架...這有效地從cardScreen容器中刪除screen ...這可能會解釋你NPE

screen.setLayout(null)這從來不是一個好主意。就我個人而言,我無法忍受不可調整大小的框架,我知道有時可以使用它們,但這並不意味着我需要喜歡它們。我想我可能會停止接聽使用無正當理由空佈局的問題:P

花時間去學習和理解佈局管理器,它們的擺動

+0

感謝您的提示,它現在全部正常工作,並且我考慮了其他意見並追加了我的代碼。每天學習更多! – Zalx

相關問題