我正在用 Java 编写一个实现 ActionListener 的图形用户界面程序。该程序是一个使用 2D 图形的吉他和弦可视化器。我有一个工具栏,用户可以在其中选择要显示的和弦。因此在 actionPerformed(ActionEvent e) 方法中,当用户选择某个和弦时,该选择会调用显示该和弦的方法。然而,当我测试程序时,我得到了大量的错误。这是错误消息
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at fretboard.displayAMajor(fretboard.java:493)
at fretboard.actionPerformed(fretboard.java:74)
at java.awt.MenuItem.processActionEvent(MenuItem.java:650)
at java.awt.MenuItem.processEvent(MenuItem.java:609)
at java.awt.MenuComponent.dispatchEventImpl(MenuComponent.java:343)
at java.awt.MenuComponent.dispatchEvent(MenuComponent.java:331)
at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:707)
at java.awt.EventQueue.access$400(EventQueue.java:82)
at java.awt.EventQueue$2.run(EventQueue.java:663)
at java.awt.EventQueue$2.run(EventQueue.java:661)
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$3.run(EventQueue.java:677)
at java.awt.EventQueue$3.run(EventQueue.java:675)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.AccessControlContext$1.doIntersectionPrivilege(AccessControlContext.java:87)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:674)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:296)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:211)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:201)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:196)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:188)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:122)
另外,我没有使用 JPanel 或 JFrame。我只是使用框架。我的程序有什么问题?谢谢!
这是我的一些代码(截至目前,该程序已超过 500 行)。
public class fretboard extends Frame implements ActionListener{
public static void main(String[] args) {
Frame frame = new fretboard();
frame.setSize(1280, 960);
frame.setVisible(true);
}
/**
* Create the menu bar and set title
*/
public fretboard() {
// Change the title of the window
setTitle("Fretboard");
// Create a menu bar where user will be given choice of chords
MenuBar mb = new MenuBar();
setMenuBar(mb);
Menu menu = new Menu("Chords");
mb.add(menu);
}
public void actionPerformed(ActionEvent e) {
String command = e.getActionCommand();
if("A Major".equals(command)) {
displayAMajor();
}
This method contains the bulk of my program. Here are just a few lines.
public void paint(Graphics g) {
// Declare local variables
int h = 40, w = 26, x = 695, y = 230;
Graphics2D g2 = (Graphics2D) g;
Font font = new Font("SansSerif", Font.BOLD, 28);
Font font1 = new Font("SansSerif", Font.BOLD, 18);
// Declare the note variables
// First string
Ellipse2D E1 = new Ellipse2D.Double(x, y-110, w, h);
// Open the image
File fretBoardFile = new File("/Users/macbook/desktop/Gibson_Fretboard.jpg");
BufferedImage bi = null;
try {
bi = ImageIO.read(fretBoardFile);
g.drawImage(bi, 25, 25, null);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
// Draw notes
// Draw the E note on the open 1st string
// Change color to blue
g2.setColor(Color.blue);
g2.draw(E1);
g2.fill(E1);
g2.setColor(Color.white);
g2.setFont(font);
g2.drawString("E", x+5, y-80);
// Change color back to blue
g2.setColor(Color.blue);
public void displayAMajor() {
// Declare local variables
int h = 40, w = 26, x = 695, y = 230;
Graphics g = null;
Graphics2D g2 = (Graphics2D) g;
//Graphics2D g2 = new Graphics();
Font font = new Font("SansSerif", Font.BOLD, 28);
// Declare notes
Ellipse2D E1 = new Ellipse2D.Double(x, y-110, w, h);
// Display notes for the A Major chord
// Draw the E note on the open 1st string
// Change color to red
g2.setColor(Color.red);
g2.draw(E1);
g2.fill(E1);
g2.setColor(Color.white);
g2.setFont(font);
g2.drawString("E", x+5, y-80);
// Change color back to blue
g2.setColor(Color.blue);
repaint();
}