行动中的未知来源

时间:2019-02-24 21:21:08

标签: java

这是我应该使用menuBar用不同颜色填充正方形的代码

import java.awt.*;
import java.awt.event.*;
import java.awt.font.TextAttribute;
import java.text.AttributedString;
import javax.swing.*;

public class Hot {
    public static void main(String[] args) {
        MenuScribble frame;
        frame = new MenuScribble("Square");
        frame.setVisible(true);
    }
}
class MenuScribble extends JFrame {

    public MenuScribble(String s) { 
        super(s);
        //setSize(500,500);
        setVisible(true);
        //pane.setSize(300, 300);
        //add(pane, BorderLayout.CENTER);
        MenuBar mb = new MenuBar();
        setMenuBar(mb);
        Menu menu = new Menu("Options");
        mb.add(menu);

        MenuShortcut keyYellow = new MenuShortcut(KeyEvent.VK_B);
        MenuShortcut keyRed = new MenuShortcut(KeyEvent.VK_N);
        MenuShortcut keyLabel = new MenuShortcut(KeyEvent.VK_M);

        MenuItem yellow = new MenuItem("Fill with yellow", keyYellow);
        MenuItem red = new MenuItem("Fill with red", keyRed);
        MenuItem label = new MenuItem("Add a label", keyLabel);

        yellow.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                Pan obj = new Pan();
                obj.draw(1);
                }
            });

        red.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                Pan obj = new Pan();
                obj.draw(2);
            }
        });
        label.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                Pan obj = new Pan();
                obj.draw(3);
            }
        });

        Pan p = new Pan(); 
        menu.add(yellow);
        menu.add(red);
        menu.add(label);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setSize(840, 500);
        add(p);
        p.setBounds(0, 0, 840, 370);
        p.setBackground(Color.BLACK);
    }

}

当我尝试单击出现错误的菜单项时:

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
    at Pan.draw(Hot.java:85)
    at MenuScribble$1.actionPerformed(Hot.java:41)
    at java.awt.MenuItem.processActionEvent(Unknown Source)
    at java.awt.MenuItem.processEvent(Unknown Source)
    at java.awt.MenuComponent.dispatchEventImpl(Unknown Source)

我应该在代码中进行哪些更改以避免错误? ................................................... ................................................... ................................................... ................................................... ................................................... ................................................... ................................................... ................................................... ................................................... ................................................... ................................................... ................................................... ................................................... ................................................... ................................................... ................................................... ................................................... .....................

0 个答案:

没有答案