java编程:在jframe内单击鼠标,在单击出画一个红色的圆,麻烦大神编一下,急求!!!!!!!
java编程:在jframe内单击鼠标,在单击出画一个红色的圆,麻烦大神编一下,急求!!!!!!!万分感谢!!!!!...
java编程:在jframe内单击鼠标,在单击出画一个红色的圆,麻烦大神编一下,急求!!!!!!!万分感谢!!!!!
展开
1个回答
展开全部
public class Canvs extends JFrame {
private JPanel contentPane;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
Canvs frame = new Canvs();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the frame.
*/
public Canvs() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 500, 500);
contentPane = new MyPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
contentPane.setLayout(new BorderLayout(0, 0));
setContentPane(contentPane);
}
class MyPanel extends JPanel implements MouseListener{
@Override
public void paint(Graphics g) {
addMouseListener(this);
}
@Override
public void mouseClicked(MouseEvent e) {
// TODO Auto-generated method stub
Graphics2D g=(Graphics2D) getGraphics();
g.setColor(Color.red);
int d_width=120;
int xx=e.getX()-d_width/2;
int yy=e.getY()-d_width/2;
g.drawOval(xx,yy,d_width,d_width);//画椭圆
//repaint();
}
@Override
public void mouseEntered(MouseEvent e) {
// TODO Auto-generated method stub
}
@Override
public void mouseExited(MouseEvent e) {
// TODO Auto-generated method stub
}
@Override
public void mousePressed(MouseEvent e) {
// TODO Auto-generated method stub
}
@Override
public void mouseReleased(MouseEvent e) {
// TODO Auto-generated method stub
}
}
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询
广告 您可能关注的内容 |