- Java code
import java.awt.*;import javax.swing.*;import java.awt.event.*;class DrawPic extends JFrame implements MouseListener,MouseMotionListener{ int x1,x2,y1,y2; boolean line,circle,rect; static CheckboxGroup cbg ; DrawPic(){ addMouseListener(this); x1 = 0; x2 = 0; y1 = 0; y2 = 0; line = false; circle = false; rect = false; } public void go(){ setLayout(new GridLayout(3,1)); cbg = new CheckboxGroup(); add(new Checkbox("line",cbg,true)); add(new Checkbox("rect",cbg,false)); add(new Checkbox("circle",cbg,false)); } public static void main(String args[]){ DrawPic draw = new DrawPic(); draw.setSize(400,300); //draw.go(); draw.setVisible(true); } public void mouseClicked(MouseEvent e){ System.out.println("Mouse Clicked"); } public void mouseReleased(MouseEvent e){ x2 = e.getX(); y2 = e.getY(); /*if(cbg.getSelectedCheckbox().getLabel().equals("line")) line = true; else if(cbg.getSelectedCheckbox().getLabel().equals("rect")) rect = true; else if(cbg.getSelectedCheckbox().getLabel().equals("circle")) circle = true;*/ repaint(); line = false; circle = false; rect = false; } public void mouseEntered(MouseEvent e){} public void mouseExited(MouseEvent e){} public void mousePressed(MouseEvent e){ x1 = e.getX(); y1 = e.getY(); } public void mouseDragged(MouseEvent e){ } public void mouseMoved(MouseEvent e){} public void paint(Graphics g){ System.out.println("painting.."); line = true; g = this.getContentPane().getGraphics(); if(line) g.drawLine(x1,y1,x2,y2); else if(circle) g.drawOval(x1,y1,x2-x1,y2-y1); else if(rect) g.drawRect(x1,y1,x2-x1,y2-y1); }}
------解决方案--------------------
三个checkbox占据了整个frame而他们是不会响应你的mouseevent的
加一个panel在第一行,然后2 3 行中进行操作
代码稍微改了下可以运行
- Java code
/* * To change this template, choose Tools | Templates * and open the template in the editor. */package test;import java.awt.*;import javax.swing.*;import java.awt.event.*;class DrawPic extends JFrame implements MouseListener,MouseMotionListener{ int x1,x2,y1,y2; boolean line,circle,rect; static CheckboxGroup cbg ; DrawPic(){ addMouseListener(this); x1 = 0; x2 = 0; y1 = 0; y2 = 0; line = false; circle = false; rect = false; setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE); } public void go(){ setLayout(new GridLayout(3,1)); JPanel pnl = new JPanel(); add(pnl); cbg = new CheckboxGroup(); pnl.add(new Checkbox("line",cbg,true)); pnl.add(new Checkbox("rect",cbg,false)); pnl.add(new Checkbox("circle",cbg,false)); } public static void main(String args[]){ DrawPic draw = new DrawPic(); draw.setSize(400,300); draw.go(); draw.setVisible(true); } public void mouseClicked(MouseEvent e){ System.out.println("Mouse Clicked"); } public void mouseReleased(MouseEvent e){ x2 = e.getX(); y2 = e.getY(); line = false; circle = false; rect = false; if(cbg.getSelectedCheckbox().getLabel().equals("line")) line = true; else if(cbg.getSelectedCheckbox().getLabel().equals("rect")) rect = true; else if(cbg.getSelectedCheckbox().getLabel().equals("circle")) circle = true; repaint(); } public void mouseEntered(MouseEvent e){} public void mouseExited(MouseEvent e){} public void mousePressed(MouseEvent e){ x1 = e.getX(); y1 = e.getY(); } public void mouseDragged(MouseEvent e){ } public void mouseMoved(MouseEvent e){} public void paint(Graphics g){ System.out.println("painting.."); //line = true; g = this.getContentPane().getGraphics(); if(line) g.drawLine(x1,y1,x2,y2); else if(circle) g.drawOval(x1,y1,x2-x1,y2-y1); else if(rect) g.drawRect(x1,y1,x2-x1,y2-y1); else{ g.drawLine(x1,y1,x2,y2); } }}