Etch-A-Sketch带有不断变化的颜色按钮?

时间:2013-06-29 04:59:40

标签: java nullpointerexception jbutton jcolorchooser

我尝试添加一个按钮,提示调色板(由我查找的类称为JColorChooser)来更改所绘制线条的颜色。单击按钮(我命名为“更改颜色”)并从调色板中选择一种颜色并尝试重绘后,该行的颜色保持黑色,并在我的控制台上弹出此消息:

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at Etch_A_Sketch.actionPerformed(Etch_A_Sketch.java:45)
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)

等......列表继续

以下是我目前为代码所做的事情:

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class Etch_A_Sketch implements MouseListener, MouseMotionListener, ActionListener
{
    JFrame window;
    Container content;
    int mouseX,mouseY,oldX,oldY;
    JLabel coords;
    JButton color;
    Color lineColor = Color.black;


    public Etch_A_Sketch()
    {
        JFrame window = new JFrame("Classic Etch a Sketch");
        content = window.getContentPane();
        content.setLayout( new FlowLayout() );
        color = new JButton("Change color");
        color.addActionListener(this);

        coords = new JLabel();
        coords.setFont(new Font("TimesRoman", Font.ITALIC + Font.BOLD, 32));

        content.add(color);
        content.add( coords); 
        content.addMouseListener(this);
        content.addMouseMotionListener(this);
        window.setSize(640,480);
        window.setVisible(true);
    }

    public void actionPerformed( ActionEvent e)
    {
        if(e.getSource() == color)
        {
            lineColor = JColorChooser.showDialog(window, "Pick a color", lineColor);
            if(lineColor != null)
            {
                window.getContentPane().setBackground(lineColor);
            }
        }
    }

    public void mouseClicked( MouseEvent me)
    {
        mouseX = me.getX();
        mouseY = me.getY();
        reportCoords("Mouse clicked at: " + mouseX + "," + mouseY);

    }
    public void mousePressed( MouseEvent me)
    {
        mouseX = me.getX();
        mouseY = me.getY();
        reportCoords("Mouse Pressed at: " + mouseX + "," + mouseY);

    }

    public void mouseReleased( MouseEvent me)
    {
        mouseX = me.getX();
        mouseY = me.getY();
        reportCoords("Mouse released at: " + mouseX + "," + mouseY);

    }

    public void mouseExited( MouseEvent me)
    {
        mouseX = me.getX();
        mouseY = me.getY();
        reportCoords("Mouse exited at: " + mouseX + "," + mouseY);

    }

    public void mouseEntered( MouseEvent me)
    {
        mouseX = me.getX();
        mouseY = me.getY();
        reportCoords("Mouse Entered at: " + mouseX + "," + mouseY);

    }

    public void mouseDragged( MouseEvent me)
    {
        mouseX = me.getX();
        mouseY = me.getY();

        if (oldX ==0 )
        {
            oldX=mouseX;
            oldY=mouseY;
            return;
        }

        Graphics g = content.getGraphics();
        g.drawLine( oldX,oldY, mouseX, mouseY );
        oldX = mouseX;
        oldY = mouseY;
        reportCoords("Mouse Dragged at: " + mouseX + "," + mouseY);

    }

    public void mouseMoved( MouseEvent me)
    {
        mouseX = me.getX();
        mouseY = me.getY();
        reportCoords("Mouse Moved at: " + mouseX + "," + mouseY);

}



// ..............................................................


    public static void main( String[] args)
    {
        new Etch_A_Sketch();
    }

    private void reportCoords( String msg )
    {
        coords.setText( msg ); 
    }
}

我想要达到的目的是能够将线条的颜色更改为用户的颜色选择。

1 个答案:

答案 0 :(得分:0)

构造函数中的代码:

public Etch_A_Sketch()
{
    JFrame window = new JFrame("Classic Etch a Sketch");
    content = window.getContentPane();
    content.setLayout( new FlowLayout() );
    color = new JButton("Change color");
    color.addActionListener(this);

    coords = new JLabel();
    coords.setFont(new Font("TimesRoman", Font.ITALIC + Font.BOLD, 32));

    content.add(color);
    content.add( coords); 
    content.addMouseListener(this);
    content.addMouseMotionListener(this);
    window.setSize(640,480);
    window.setVisible(true);
}

这一行:

JFrame window = new JFrame("Classic Etch a Sketch");

在名为window的构造函数中创建一个局部变量。因此,所有初始化都是对局部变量进行的,当构造函数终止时,该变量超出了范围。 actionPerformed()方法使用尚未初始化的实例变量,因此使用null。因此,从window.getContentPane()内调用actionPerformed()时,尝试在null引用上调用方法,从而生成NullPointerException

要解决问题,而不是在构造函数中声明局部变量,如下所示:

JFrame window = new JFrame("Classic Etch a Sketch");

使用已经声明的实例变量:

window = new JFrame("Classic Etch a Sketch");