“************”报出AWT-EventQueue-0异常,请问怎么改,谢谢!
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Painter extends JFrame{
public Color c=Color.RED;
Painter(){
super();
JFrame fm=new JFrame("Painter");
Graphics g = this.getGraphics();
fm.setLayout(new BorderLayout());
GridBagLayout gridbag = new GridBagLayout();
GridBagConstraints c = new GridBagConstraints();
JPanel pan=new JPanel(new BorderLayout());
JPanel p1=new JPanel(new GridLayout(2,5));
JMenuBar mb=new JMenuBar();
JMenu []m = {
new JMenu("File"),
new JMenu("Edit"),
new JMenu("Help"),
};
mb.add(m[0]);
mb.add(m[1]);
mb.add(m[2]);
setJMenuBar(mb);
fm.add(mb,"North");
JLabel lab1=new JLabel("Shape Color");
p1.add(lab1);
JComboBox com1 = new JComboBox();
com1.setSize(5,5);
p1.add(com1);
JLabel lab2=new JLabel("Text",JLabel.RIGHT);
p1.add(lab2);
JTextField txt=new JTextField(200);
txt.setColumns(200);;
p1.add(txt);
JLabel lab0=new JLabel("");
p1.add(lab0);
JLabel lab3=new JLabel("Background");
p1.add(lab3);
JComboBox com2 = new JComboBox();
p1.add(com2);
JLabel lab4=new JLabel("Size",JLabel.RIGHT);
p1.add(lab4);
JComboBox com3 = new JComboBox();
p1.add(com3);
JCheckBox cb=new JCheckBox("Fill the region");
p1.add(cb);
JPanel p2=new JPanel(new GridLayout(7,1));
ButtonGroup bg = new ButtonGroup();
for(int i=0;i<5;i++){
JToggleButton jrb = new JToggleButton();
jrb.setIcon(new ImageIcon("face.gif"));
jrb.setSelectedIcon(new ImageIcon("face1.gif"));
jrb.setBorder(null);
bg.add(jrb);
p2.add(jrb);
}
JPanel draw = new JPanel();
draw.setBackground(Color.YELLOW);
pan.add(p1,"North");
pan.add(p2,"West");
pan.add(draw,"Center");
fm.add(pan,"Center");
fm.setSize(600, 600);
fm.setVisible(true);
DrawListener dl=new DrawListener(g);
draw.addMouseListener(dl);
}
public static void main(String[] args){
Painter test=new Painter();
test.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
class DrawListener implements MouseListener{
public Graphics g;
public int x1,y1,x2,y2;
public ButtonGroup bg;
public String command=new String("pic10");
public Color color;
public Painter pt;
//构造函数
public DrawListener(Graphics g1){
g=g1;
}
public DrawListener(Graphics g2, ButtonGroup bg2) {
g=g2;
bg=bg2;
}
public DrawListener(Graphics g2, ButtonGroup bg2, Painter pt1) {
g=g2;
bg=bg2;
pt=pt1;
}
public void mousePressed(MouseEvent e) {
x1=e.getX();
y1=e.getY();
//判断选择的是左面板中的那个按钮被选中(前面已经设置每个按钮的名称了)
ButtonModel bm=bg.getSelection();//拿到按钮组中被选中的按钮
// command=bm.getActionCommand();//拿到选中按钮的名字
color=pt.c;//设置画笔颜色
g.setColor(color);
}
public void mouseReleased(MouseEvent e) {
x2=e.getX();
y2=e.getY();
//如果选中的是绘制直线的按钮,那么根据鼠标按下点的坐标和释放点的左边绘制直线(两点确定一条直线)
if("pic10".equals(command))
{
g.drawLine(x1, y1, x2, y2); //************这儿,AWT-EventQueue-0异常
}//绘制矩形
else if("pic12".equals(command)){
g.drawRect(Math.min(x2, x1),Math.min(y2, y1), Math.abs(x2-x1),Math.abs(y1-y2));
}//绘制椭圆
else if("pic14".equals(command)){
g.drawOval(Math.min(x2, x1),Math.min(y2, y1), Math.abs(x2-x1),Math.abs(y1-y2));
}
}
public void mouseClicked(MouseEvent e) {}
public void mouseEntered(MouseEvent e) {}
public void mouseExited(MouseEvent e) {}
}