我一直在尝试编写代码,并让按钮具有不同的颜色。我已经尝试了一些方法,并相信切换可能是我需要的解决方案。但是,当我运行它并激活开关时,会在控制台中得到以下信息:
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at Program$8$1.actionPerformed(Program.java:277)
at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)
at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.setPressed(Unknown Source)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(Unknown Source)
at java.awt.Component.processMouseEvent(Unknown Source)
at javax.swing.JComponent.processMouseEvent(Unknown Source)
at java.awt.Component.processEvent(Unknown Source)
at java.awt.Container.processEvent(Unknown Source)
at java.awt.Component.dispatchEventImpl(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Window.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
at java.awt.EventQueue.access$500(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue$4.run(Unknown Source)
at java.awt.EventQueue$4.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source)
我该怎么做才能防止此错误?同样是有问题的代码在下面(“显示”是第二个代码)(切换开始的行是274)
import java.awt.event.*;
import javax.swing.*;
import java.awt.*;
import java.util.concurrent.ThreadLocalRandom;
public class Program extends Display {
private static final long serialVersionUID = 1L;
public static void main(String[] args) {
Display display = new Display();
display.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
display.setSize(1600, 800);
display.setResizable(false);
//-------------------------------------------------------------------
//Main Menu
JButton select = new JButton("Select");
select.setPreferredSize(new Dimension(400,100));
select.setFont(new Font("Arial", Font.PLAIN, 40));
JButton absent = new JButton("Absentees");
absent.setPreferredSize(new Dimension(400,100));
absent.setFont(new Font("Arial", Font.PLAIN, 40));
JButton exit = new JButton("Exit");
exit.setPreferredSize(new Dimension(400,100));
exit.setFont(new Font("Arial", Font.PLAIN, 40));
exit.setLocation(800, 700);
exit.setBackground(Color.red);
exit.setForeground(Color.white);
JButton select1 = new JButton("Select 1");
JButton select2 = new JButton("Select 2");
JButton select3 = new JButton("Select 3");
JButton select4 = new JButton("Select 4");
JButton select5 = new JButton("Select 5");
JButton RS1 = new JButton();
JButton back = new JButton("Back");
back.setPreferredSize(new Dimension(400,100));
back.setFont(new Font("Arial", Font.PLAIN, 40));
select1.setPreferredSize(new Dimension(400,100));
select1.setFont(new Font("Arial", Font.PLAIN, 40));
RS1.setFont(new Font("Arial", Font.PLAIN, 40));
RS1.setPreferredSize(new Dimension(400,100));
RS1.setFont(new Font("Arial", Font.PLAIN, 40));
RS1.setVisible(false);
RS1.setOpaque(false);
RS1.setContentAreaFilled(false);
RS1.setBorderPainted(false);
RS1.setVisible(false);
RS1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
}});
select1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
RS1.setVisible(true);
int select1student1 = ThreadLocalRandom.current().nextInt(1, 10 + 1);
if (select1student1 == 1) {
RS1.setText("Student One");
}
if (select1student1 == 2) {
RS1.setText("Student Two");
}
if (select1student1 == 3) {
RS1.setText("Student Three");
}
if (select1student1 == 4) {
RS1.setText("Student Four");
}
if (select1student1 == 5) {
RS1.setText("Student Five");
}
if (select1student1 == 6) {
RS1.setText("Student Six");
}
if (select1student1 == 7) {
RS1.setText("Student Seven");
}
if (select1student1 == 8) {
RS1.setText("Student Eight");
}
if (select1student1 == 9) {
RS1.setText("Student Nine");
}
if (select1student1 == 10) {
RS1.setText("Student Ten");
}
}});
select2.setPreferredSize(new Dimension(400,100));
select2.setFont(new Font("Arial", Font.PLAIN, 40));
select2.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
}});
select3.setPreferredSize(new Dimension(400,100));
select3.setFont(new Font("Arial", Font.PLAIN, 40));
select3.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
}});
select4.setPreferredSize(new Dimension(400,100));
select4.setFont(new Font("Arial", Font.PLAIN, 40));
select4.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
}});
select5.setPreferredSize(new Dimension(400,100));
select5.setFont(new Font("Arial", Font.PLAIN, 40));
select5.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
}});
select.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
select.setVisible(false);
absent.setVisible(false);
exit.setVisible(false);
display.remove(select);
display.remove(absent);
display.remove(exit);
back.setVisible(true);
display.add(back);
display.add(select1);
display.add(select2);
display.add(select3);
display.add(select4);
display.add(select5);
display.add(RS1);
select1.setVisible(true);
select2.setVisible(true);
select3.setVisible(true);
select4.setVisible(true);
select5.setVisible(true);
back.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
RS1.setText("");
RS1.setVisible(false);
display.add(select);
display.add(absent);
display.add(exit);
display.remove(back);
select1.setVisible(false);
select2.setVisible(false);
select3.setVisible(false);
select4.setVisible(false);
select5.setVisible(false);
select.setVisible(true);
display.remove(select1);
display.remove(select2);
display.remove(select3);
display.remove(select4);
display.remove(select5);
absent.setVisible(true);
exit.setVisible(true);
back.setVisible(false);
}
});
}});
//Mark Absents
//
//
//
//
absent.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
JButton back = new JButton("Back");
back.setPreferredSize(new Dimension(400,100));
back.setFont(new Font("Arial", Font.PLAIN, 40));
//Mark The Absentees Below
JButton AB1 = new JButton("Student One");
AB1.setPreferredSize(new Dimension(400,100));
AB1.setFont(new Font("Arial", Font.PLAIN, 40));
JButton AB2 = new JButton("Student Two");
AB2.setPreferredSize(new Dimension(400,100));
AB2.setFont(new Font("Arial", Font.PLAIN, 40));
JButton AB3 = new JButton("Student Three");
AB3.setPreferredSize(new Dimension(400,100));
AB3.setFont(new Font("Arial", Font.PLAIN, 40));
JButton AB4 = new JButton("Student Four");
AB4.setPreferredSize(new Dimension(400,100));
AB4.setFont(new Font("Arial", Font.PLAIN, 40));
JButton AB5 = new JButton("Student Five");
AB5.setPreferredSize(new Dimension(400,100));
AB5.setFont(new Font("Arial", Font.PLAIN, 40));
JButton AB6 = new JButton("Student Six");
AB6.setPreferredSize(new Dimension(400,100));
AB6.setFont(new Font("Arial", Font.PLAIN, 40));
JButton AB7 = new JButton("Student Seven");
AB7.setPreferredSize(new Dimension(400,100));
AB7.setFont(new Font("Arial", Font.PLAIN, 40));
JButton AB8 = new JButton("Student Eight");
AB8.setPreferredSize(new Dimension(400,100));
AB8.setFont(new Font("Arial", Font.PLAIN, 40));
JButton AB9 = new JButton("Student Nine");
AB9.setPreferredSize(new Dimension(400,100));
AB9.setFont(new Font("Arial", Font.PLAIN, 40));
JButton AB10 = new JButton("Student Ten");
AB10.setPreferredSize(new Dimension(400,100));
AB10.setFont(new Font("Arial", Font.PLAIN, 40));
AB1.setBackground(Color.green);
AB2.setBackground(Color.green);
AB3.setBackground(Color.green);
AB4.setBackground(Color.green);
AB5.setBackground(Color.green);
AB6.setBackground(Color.green);
AB7.setBackground(Color.green);
AB8.setBackground(Color.green);
AB9.setBackground(Color.green);
AB10.setBackground(Color.green);
//Mark The Absentees Above
select.setVisible(false);
absent.setVisible(false);
exit.setVisible(false);
display.remove(select);
display.remove(absent);
display.remove(exit);
back.setVisible(true);
display.add(back);
display.add(AB1);
display.add(AB2);
display.add(AB3);
display.add(AB4);
display.add(AB5);
display.add(AB6);
display.add(AB7);
display.add(AB8);
display.add(AB9);
display.add(AB10);
AB1.addActionListener(new ActionListener() {
String AB1Color;
@Override
public void actionPerformed(ActionEvent e) {
switch(AB1Color) {
case "Green": System.out.println(AB1Color);
case "Red": System.out.println(AB1Color);
default: System.out.println("Default Value");
}
}
});
back.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
display.add(select);
display.add(absent);
display.add(exit);
AB1.setVisible(false);
AB2.setVisible(false);
AB3.setVisible(false);
AB4.setVisible(false);
AB5.setVisible(false);
AB6.setVisible(false);
AB7.setVisible(false);
AB8.setVisible(false);
AB9.setVisible(false);
AB10.setVisible(false);
select.setVisible(true);
display.remove(back);
display.remove(AB1);
display.remove(AB2);
display.remove(AB3);
display.remove(AB4);
display.remove(AB5);
display.remove(AB6);
display.remove(AB7);
display.remove(AB8);
display.remove(AB9);
display.remove(AB10);
absent.setVisible(true);
exit.setVisible(true);
back.setVisible(false);
}
});
}});
exit.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
System.exit(0);
}});
display.setVisible(true);
display.add(select);
display.add(absent);
display.add(exit);
}}
import javax.swing.*;
import java.awt.*;
public class Display extends JFrame{
private static final long serialVersionUID = 1L;
public Display() {
super("Student Selector");
setLayout(new FlowLayout());
}
}