Twoo8 2016-12-13 09:52 采纳率: 50%
浏览 1420

初学Java画直线出现异常,求助各位大神

“************”报出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) {}  

}

  • 写回答

2条回答 默认 最新

  • devmiao 2016-12-13 16:38
    关注
    评论

报告相同问题?

悬赏问题

  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法
  • ¥15 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP