嘿,我明白了
Exception in thread "AWT-EventQueue-1" java.lang.NullPointerException
at P6.itemStateChanged(P6.java:215)
at javax.swing.AbstractButton.fireItemStateChanged(AbstractButton.java:2
023)
at javax.swing.AbstractButton$Handler.itemStateChanged(AbstractButton.ja
va:2325)
at javax.swing.DefaultButtonModel.fireItemStateChanged(DefaultButtonMode
l.java:440)
at javax.swing.JToggleButton$ToggleButtonModel.setSelected(JToggleButton
.java:255)
at javax.swing.ButtonGroup.setSelected(ButtonGroup.java:147)
at javax.swing.JToggleButton$ToggleButtonModel.setSelected(JToggleButton
.java:237)
at javax.swing.JToggleButton$ToggleButtonModel.setPressed(JToggleButton.
java:272)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonL
istener.java:236)
at java.awt.Component.processMouseEvent(Component.java:6267)
at javax.swing.JComponent.processMouseEvent(JComponent.java:3267)
at java.awt.Component.processEvent(Component.java:6032)
at java.awt.Container.processEvent(Container.java:2041)
at java.awt.Component.dispatchEventImpl(Component.java:4630)
at java.awt.Container.dispatchEventImpl(Container.java:2099)
at java.awt.Component.dispatchEvent(Component.java:4460)
at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4577
)
at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4238)
at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4168)
at java.awt.Container.dispatchEventImpl(Container.java:2085)
at java.awt.Component.dispatchEvent(Component.java:4460)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:599)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThre
ad.java:269)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.
java:184)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThre
ad.java:174)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:169)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:161)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:122)
在
import java.awt.Color;
import java.awt.Container;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import javax.swing.ButtonGroup;
import javax.swing.JApplet;
import javax.swing.JComboBox;
import javax.swing.JRadioButton;
public class P6 extends JApplet implements MouseListener, MouseMotionListener, ItemListener
{
private final int RED = 1;
private final int ORANGE = 2;
private final int YELLOW = 3;
private final int GREEN = 4;
private final int BLUE = 5;
private final int CYAN = 6;
private final int PINK = 7;
private final int MAGENTA =8;
private final int BLACK = 9;
private final int ARC = 1;
private final int LINE = 2;
private final int OVAL = 3;
private final int RECT = 4;
private final int POLY = 5;
private String[] colorNames = { "Red", "Orange", "Yellow", "Green", "Blue",
"Cyan", "Pink", "Magenta", "Black"};
private JComboBox colorComboBox;
private JRadioButton jrbArc;
private JRadioButton jrbLine;
private JRadioButton jrbOval;
private JRadioButton jrbRect;
private JRadioButton jrbPolygon;
private int color = 0;
private int shape = 0;
private int x1;
private int y1;
private int x2;
private int y2;
public void init()
{
Container c = getContentPane();
c.setLayout( new FlowLayout() );
c.setBackground( Color.orange );
JComboBox colorComboBox = new JComboBox(colorNames);
colorComboBox.addItemListener(this);
JRadioButton jrbArc = new JRadioButton("Arc");
jrbArc.addItemListener(this);
JRadioButton jrbLine = new JRadioButton("Line");
jrbLine.addItemListener(this);
JRadioButton jrbOval = new JRadioButton("Oval");
jrbOval.addItemListener(this);
JRadioButton jrbRect = new JRadioButton("Rectangle");
jrbRect.addItemListener(this);
JRadioButton jrbPolygon = new JRadioButton("Polygon");
jrbPolygon.addItemListener(this);
ButtonGroup btg = new ButtonGroup();
btg.add(jrbArc);
c.add(jrbArc);
btg.add(jrbLine);
c.add(jrbLine);
btg.add(jrbOval);
c.add(jrbOval);
btg.add(jrbRect);
c.add(jrbRect);
btg.add(jrbPolygon);
c.add(jrbPolygon);
c.add(colorComboBox);
addMouseMotionListener(this);
addMouseListener(this);
}
public void paint(Graphics g)
{
super.paint(g);
g.setColor(Color.white);
g.fillRect(20, 50, 500, 500);
g.setFont( new Font("Serif", Font.ITALIC+Font.BOLD, 18));
g.setColor(Color.black);
g.drawString("Start Here!", 10, 50);
g.drawString("End Here!", 510, 560);
switch (this.color)
{
case 1:
g.setColor(Color.red);
break;
case 2:
g.setColor(Color.orange);
break;
case 3:
g.setColor(Color.yellow);
break;
case 4:
g.setColor(Color.green);
break;
case 5:
g.setColor(Color.blue);
break;
case 6:
g.setColor(Color.cyan);
break;
case 7:
g.setColor(Color.pink);
break;
case 8:
g.setColor(Color.magenta);
break;
case 9:
g.setColor(Color.black);
break;
}
switch (shape)
{
case 1:
g.fillArc(this.x1, this.y1, this.x2 - this.x1, this.y2 - this.y1, 0, 90);
g.fillArc(this.x1, this.y1, this.x2 - this.x1, this.y2 - this.y1, 180,90);
break;
case 2:
g.drawLine(this.x1, this.y1, this.x2, this.y2);
break;
case 3:
g.fillOval(this.x1, this.y1, this.x2 - this.x1, this.y2 - this.y1);
break;
case 4:
g.fillRect(this.x1, this.y1, this.x2 - this.x1, this.y2 - this.y1);
break;
case 5:
int[] arr1 = { this.x1, this.x2, this.x1, this.x2, this.x1};
int[] arr2 = { this.y1, this.y1, this.y2 ,this.y2, this.y1};
g.fillPolygon(arr1, arr2, 5);
break;
}
}
public void mousePressed( MouseEvent e)
{
this.x1 = e.getX();
this.y1 = e.getY();
}
public void mouseDragged( MouseEvent e)
{
this.x2 = e.getX();
this.y2 = e.getY();
}
public void mouseReleased( MouseEvent e)
{
this.x2 = e.getX();
this.y2 = e.getY();
}
public void mouseClicked( MouseEvent e)
{
}
public void mouseEntered( MouseEvent e)
{
}
public void mouseExited( MouseEvent e)
{
}
public void mouseMoved( MouseEvent e)
{
showStatus( " (" + e.getX() + ", " + e.getY() +")");
}
public void itemStateChanged(ItemEvent i)
{
if(this.jrbArc.isSelected())
this.shape = ARC;
else if( this.jrbLine.isSelected())
this.shape = LINE;
else if( this.jrbOval.isSelected())
this.shape = OVAL;
else if( this.jrbRect.isSelected())
this.shape = RECT;
else if( this.jrbPolygon.isSelected())
this.shape = POLY;
this.color = ( this.colorComboBox.getSelectedIndex() + 1 );
repaint();
}
}
我一直在试图解决这个问题,但是没有运气。
有什么帮助吗?
215行是:
if(this.jrbArc.isSelected())
最佳答案
代替
JRadioButton jrbArc = new JRadioButton("Arc");
你需要
jrbArc = new JRadioButton("Arc");
还有其他控件的类似内容。
关于java - 线程 “AWT-EventQueue-1”中的异常java.lang.NullPointerException,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/5086559/