我是 Java 新手,所以请帮忙 :) 我有一个名为MainFrame的超类 ,它带有 main 方法和构造函数类Run()。我从名为MenuBar的子类获得 JFrame 的菜单栏。菜单栏有多个实现 ActionListener 的子类。每当我尝试使用 ActionListener 调用MainFrame超类中的方法时,我都会收到错误消息,该方法从JPanel中删除所有元素,这些元素在MainFrame构造函数方法Run()中初始化MainFrame 代码:
import java.awt.Color;
import java.awt.Dimension;
import java.awt.GraphicsDevice;
import java.awt.GraphicsEnvironment;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.util.Arrays;
import javax.swing.BorderFactory;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
public class MainFrame {
JFrame mainFrame;
JPanel mainPanel;
JPanel[] pagePanels, tablePanels;
JScrollPane scrollBar;
private double zoom=1;
private int width;
private int height;
private GridBagConstraints c;
//private Page blankPage = new Page();
MenuBar menubar;
Page blankPage;
public static void main(String[] args) {
MainFrame generator = new MainFrame();
generator.Run();
}
public void Run() {
pagePanels = new JPanel[100];
tablePanels = new JPanel[100];
menubar = new MenuBar();
blankPage = new Page();
c = new GridBagConstraints();
mainFrame = new JFrame("Code Generator v.1.0");
mainFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
c.gridx=0;
c.insets = new Insets(20,0,20,0);
//get size of screen
GetScreenSize();
mainPanel=new JPanel(new GridBagLayout());
scrollBar=new JScrollPane(mainPanel);
scrollBar.setPreferredSize(new Dimension(width,height));
//create main frame with height and width equal to screen size (fullscreen)
mainFrame.setJMenuBar(menubar.getMenuBar());
mainFrame.getContentPane().add(scrollBar);
mainFrame.setSize(width,height);
mainFrame.setVisible(true);
}
public void GetScreenSize(){
GraphicsDevice gd = GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice();
width = gd.getDisplayMode().getWidth();
height = gd.getDisplayMode().getHeight();
}
public void RemoveAllfromMainPanel(){
mainPanel.removeAll();
}
}
子类MenuBar的代码是:
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Arrays;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
public class MenuBar extends MainFrame{
public JMenuBar getMenuBar(){
JMenuBar menubar = new JMenuBar();
//add menus to menu bar
JMenu file = new JMenu("File");
menubar.add(file);
//add items to File menu
JMenuItem newReport = new JMenuItem("New report");
file.add(newReport);
newReport.addActionListener(new newReport());
JMenuItem exit = new JMenuItem("Exit");
file.add(exit);
exit.addActionListener(new exitAction());
return menubar;
}
class exitAction implements ActionListener {
public void actionPerformed(ActionEvent event) {
System.exit(0);
}
}
class newReport implements ActionListener {
public void actionPerformed(ActionEvent event) {
RemoveAllfromMainPanel();
}
}
}
我认为它必须与继承有关,但这是我最近开始研究的主题。您能否向我解释一下为什么会出现此错误:
MainFrame.TEST 处的线程“AWT-EventQueue-0”java.lang.NullPointerException 中的异常(MainFrame.java:125)
在这一行:
mainPanel.removeAll();
先感谢您!干杯!