0

我尝试添加一个按钮来提示调色板(由我查找的名为 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 ); 
    }
}

我想要实现的只是能够将线条的颜色更改为用户的颜色选择。

4

1 回答 1

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");
于 2013-06-30T18:53:50.133 回答