我试图创建我的第一个学校课程之一。 目前,我得到了例外,我无法弄清楚原因;请帮帮我。
当我尝试按 Eng 和 LoginAdmin 按钮时,会显示错误。它应该将JPanel从 Paneel 切换到 Paneel2 / Paneel3
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class Main extends JFrame {
static Main frame1;
static Main frame2;
static Main frame3;
public static void main(String[] args) {
frame1 = new Main();
frame1.setSize(800, 800);
frame1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame1.setTitle("CityTours Nederlands ");
JPanel Paneel = new Paneel();
frame1.setContentPane(Paneel);
frame1.setVisible(true);
frame2 = new Main();
frame2.setSize(800, 800);
frame2.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame2.setTitle("CityTours English");
JPanel Paneel2 = new Paneel2();
frame2.setContentPane(Paneel2);
frame2.setVisible(false);
frame3 = new Main();
frame3.setSize(800, 800);
frame3.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame3.setTitle("Admin Login NL/ENG");
JPanel Paneel3 = new Paneel3();
frame3.setContentPane(Paneel3);
frame3.setVisible(false);
}
}
class Paneel extends JPanel {
private JButton Eng, LoginAdminNl, LoginGebruiker;
private JTextField Text;
public Paneel() {
setLayout(null);
Eng = new JButton("Bring me to the English version");
Eng.setBounds(250, 20, 300, 20);
Eng.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
if(Main.frame1.isVisible()) {
Main.frame1.setVisible(false);
Main.frame2.setVisible(true);
Main.frame3.setVisible(false);
} else {
Main.frame1.setVisible(true);
Main.frame2.setVisible(false);
Main.frame3.setVisible(false);
}
}
});
Text = new JTextField(" Welkom bij CityTours ");
Text.setBounds(100, 80, 600, 600);
Text.setEditable(false);
LoginAdminNl = new JButton("Inloggen administrator");
LoginAdminNl.setBounds(100, 720, 200, 20);
LoginAdminNl.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
if(Main.frame1.isVisible()) {
Main.frame1.setVisible(false);
Main.frame3.setVisible(true);
} else {
Main.frame1.setVisible(true);
Main.frame2.setVisible(false);
Main.frame3.setVisible(false);
}
}
});
LoginGebruiker = new JButton("Inloggen gebruiker");
LoginGebruiker.setBounds(500, 720, 200, 20);
add(Eng);
add(Text);
add(LoginAdminNl);
add(LoginGebruiker);
}
}
class Paneel2 extends JPanel {
private JButton Nl, LoginAdminEng, LoginUser;
private JTextField Text2;
public Paneel2() {
setLayout(null);
Nl = new JButton("Breng me naar de Nederlandse versie");
Nl.setBounds(250, 20, 300, 20);
Nl.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
if(Main.frame1.isVisible()) {
Main.frame1.setVisible(false);
Main.frame2.setVisible(true);
Main.frame3.setVisible(false);
} else {
Main.frame1.setVisible(true);
Main.frame2.setVisible(false);
Main.frame3.setVisible(false);
}
}
});
Text2 = new JTextField(" Welcome to CityTours ");
Text2.setBounds(100, 80, 600, 600);
Text2.setEditable(false);
LoginAdminEng = new JButton("Login admin");
LoginAdminEng.setBounds(100, 720, 200, 20);
LoginAdminEng.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
if(Main.frame2.isVisible()) {
Main.frame1.setVisible(false);
Main.frame2.setVisible(false);
Main.frame3.setVisible(true);
} else {
Main.frame1.setVisible(false);
Main.frame2.setVisible(true);
Main.frame3.setVisible(false);
}
}
});
LoginUser = new JButton("Login User");
LoginUser.setBounds(500, 720, 200, 20);
add(Nl);
add(Text2);
add(LoginAdminEng);
add(LoginUser);
}
}
class Paneel3 extends JPanel {
private JButton Check;
private JTextField InvoerNaam, InvoerPassword;
private JLabel Naam, Password;
public Paneel3() {
Check = new JButton ("Inloggen / Login");
InvoerNaam = new JTextField (15);
InvoerPassword = new JTextField (15);
Naam = new JLabel ("Naam/Name");
Password = new JLabel ("Wachtwoord/Password");
add (Naam);
add (InvoerNaam);
add (Password);
add (InvoerPassword);
add (Check);
}
}
错误:
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at Paneel$1.actionPerformed(Main.java:56)
at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:2022)
at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2346)
at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:402)
at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:259)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:252)
at java.awt.Component.processMouseEvent(Component.java:6525)
at javax.swing.JComponent.processMouseEvent(JComponent.java:3324)
at java.awt.Component.processEvent(Component.java:6290)
at java.awt.Container.processEvent(Container.java:2234)
at java.awt.Component.dispatchEventImpl(Component.java:4881)
at java.awt.Container.dispatchEventImpl(Container.java:2292)
at java.awt.Component.dispatchEvent(Component.java:4703)
at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4898)
at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4533)
at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4462)
at java.awt.Container.dispatchEventImpl(Container.java:2278)
at java.awt.Window.dispatchEventImpl(Window.java:2750)
at java.awt.Component.dispatchEvent(Component.java:4703)
at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:758)
at java.awt.EventQueue.access$500(EventQueue.java:97)
at java.awt.EventQueue$3.run(EventQueue.java:709)
at java.awt.EventQueue$3.run(EventQueue.java:703)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:75)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:86)
at java.awt.EventQueue$4.run(EventQueue.java:731)
at java.awt.EventQueue$4.run(EventQueue.java:729)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:75)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:728)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:201)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:116)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:105)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:101)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:93)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:82)
Process completed.
请帮帮我;)