执行我下面的这段代码后,为什么 在第一次点击鼠标时 无法画出圆, 再点击第二次时才能画出圆?import java.io.*;
import java.awt.*;
import java.awt.image.*;
import java.awt.event.*;
import javax.swing.*;
import javax.imageio.*;public class Object7
{
public static void main(String args[])
{
new NewFrame("Hello World");
}
}
class NewFrame extends JFrame
{
public NewFrame(String name)
{
super(name); this.setBounds(100, 100, 800, 800);
this.setResizable(false);
this.setVisible(true);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.addMouseListener(new NewMonitor());
}
public void paintComponent(Graphics g)
{
g.drawOval(0, 0, 500, 500);
}
class NewMonitor extends MouseAdapter
{
private int x, y;
public void mouseClicked(MouseEvent e)
{
x = e.getX();
y = e.getY();
getGraphics().fillOval(x, y, 50, 50);
}
}
}
import java.awt.*;
import java.awt.image.*;
import java.awt.event.*;
import javax.swing.*;
import javax.imageio.*;public class Object7
{
public static void main(String args[])
{
new NewFrame("Hello World");
}
}
class NewFrame extends JFrame
{
public NewFrame(String name)
{
super(name); this.setBounds(100, 100, 800, 800);
this.setResizable(false);
this.setVisible(true);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.addMouseListener(new NewMonitor());
}
public void paintComponent(Graphics g)
{
g.drawOval(0, 0, 500, 500);
}
class NewMonitor extends MouseAdapter
{
private int x, y;
public void mouseClicked(MouseEvent e)
{
x = e.getX();
y = e.getY();
getGraphics().fillOval(x, y, 50, 50);
}
}
}
解决方案 »
- java 通过jacob 调用outlook 发送邮件
- Spring Security3 有关权限的问题
- 关于获得日期的问题
- 请教 runtime.getruntime().exec(command) 死锁问题(一般方案无法解决)
- 请问System.setProperty()
- Applet to Swing (100分)
- 高分!!!Java中如何得知系统的某一进程正在运行
- 已知字符串,怎样附值给同名控件(用JScript)?
- about package(高分求解)
- 谁有基于AWT的CHART BEAN?
- 请问:比“快速排序”效率高的算法是什么?
- JPanel和getContentPane()的问题,急
被 public void paintComponent(Graphics g) 中的 g.drawOval(0, 0, 500, 500) 刷新掉了?那该怎么解决?
可以的LZ的JDK啥版本哦.俺1.5可以的
class NewFrame extends JFrame {
private BufferedImage bufImg =
new BufferedImage(800, 800, BufferedImage.TYPE_INT_RGB);
public NewFrame(String name) {
super(name); this.setBounds(100, 100, 800, 800);
this.setResizable(false);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.addMouseListener(new MouseAdapter() {
public void mousePressed(MouseEvent e) {
bufImg.getGraphics().fillOval(e.getX(), e.getY(), 50, 50);
repaint();
}
});
this.setVisible(true);
} @Override
public void paint(Graphics g) {
g.drawImage(bufImg, 0, 0, null);
}
}看上面的带缓冲的代码,即使窗体没最小化再正常显示你画的圆也不会消失。
有个小问题,JFrame没继承自JComponent,没有paintComponet方法。
还有mouseClicked改为mousePressed一般就不会出现第一次点击画不出来的情况了,鼠标按下和弹起之间位置没移动的情况下mouseClicked才会被执行。