2

不久前,我问了一个类似的问题,关于在 JFrame 中为 JPanel 使用多个类,在对我的代码进行了很大改进之后,我现在遇到了 CardLayout 的障碍。

当我试图显示我的“2”屏幕时,我得到一个 NullPointerException,这只是一个 BaseScreen,我稍后将使用其他类来继承它的属性。更改此代码后,

final CardLayout cardL = new CardLayout();

CardLayout cardL;

在我的构造函数之外,我现在没有出现任何 JPanel!

这是我的代码:

登录类:

/*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){

        }
    }
  }

}

提前感谢您的任何帮助/提示。

这是旧线程:使用具有相同 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)
4

1 回答 1

2

我不知道你的NullPointerException,但你的例子不起作用。

class LoginScreen extends JFrame

很少需要从顶级容器扩展,例如JFrame,这是不必要的,只会使事情复杂化,请改用类似的东西JPanel并将其添加到您创建的框架中......

JFrame container = this;

恕我直言,这是一个坏主意。它只是混淆了这个问题。如果你真的需要某种方式来引用父对象,this直接使用...

您在完成向框架添加组件之前调用container.setVisible(true),这绝不是一个好主意。事实上,当我正确加载您的程序时(从 EDT 内开始),框架上什么也没有出现 - 但这可能是由于下一个问题......

你创造cardScreen,你创造screen,你添加screencardScreen然后你screen直接添加到框架中......这已经screencardScreen容器中有效地删除......这可能会解释你NPE

screen.setLayout(null)这绝不是一个好主意。就个人而言,我无法忍受不可调整大小的框架,我知道有时会使用它们,但这并不意味着我需要喜欢它们。我想我可能会在没有充分理由的情况下停止回答使用空布局的问题:P

花时间学习和理解布局管理器,它们是 Swing 最强大的功能之一

于 2012-10-19T10:30:40.263 回答