这是一个画点的程序,但是一旦失去焦点,被其他界面挡住,或者最小化,画好的点就会消失,为什么啊import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class D {
public static void main(String[] args){
new MyFrame("D");
}
}class MyFrame extends JFrame{
MyFrame(String s){
super(s);
this.setLayout(null);
this.setBounds(100,100,200,300);
this.setBackground(Color.white );
this.setVisible(true);
this.addMouseListener(new MouseAdapter(){
public void mousePressed(MouseEvent e){
int x= e.getX();
int y =e.getY();
Graphics g = getGraphics();
draw(g,x,y);
}
}
);
this.addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e){
setVisible(false);
System.exit(0);
}});
}public void draw(Graphics g,int x,int y){
g.setColor(Color.blue);
g.fillOval(x,y,10,10);
}
}
javaswing graphics
import java.awt.event.*;
import javax.swing.*;
public class D {
public static void main(String[] args){
new MyFrame("D");
}
}class MyFrame extends JFrame{
MyFrame(String s){
super(s);
this.setLayout(null);
this.setBounds(100,100,200,300);
this.setBackground(Color.white );
this.setVisible(true);
this.addMouseListener(new MouseAdapter(){
public void mousePressed(MouseEvent e){
int x= e.getX();
int y =e.getY();
Graphics g = getGraphics();
draw(g,x,y);
}
}
);
this.addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e){
setVisible(false);
System.exit(0);
}});
}public void draw(Graphics g,int x,int y){
g.setColor(Color.blue);
g.fillOval(x,y,10,10);
}
}
javaswing graphics
解决方案 »
- 菜鸟-如下常量赋值中DELAY_LONG = 1 << 2, 1 << 2是怎么回事?
- replace如何替换count(*)为 ”aaa“
- 非常奇怪的字节丢失的现象,达人入
- 关于 main
- 想知道如何和银行系统对接实现网上支付吗
- Swing编程,如何实现窗体的打开和关闭
- 有没有监控JAVA程序内存使用情况的软件
- 对于.lnk的shortcut文件,如何在Java中取得它的实际地址?
- 经常看到有人问到 java 编辑工具的问题,在此作一调查,希望大家贴出自己喜欢用的java工具!
- 那里可以下载Visualage for java 3.02
- 使用apache zip 压缩,在同一压缩目录下,同一个文件被压入多次,为何这个文件没有被覆盖,会在同一压缩目录下存在多个相同文件名的文件?
- 构造方法
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;public class D {
public static void main(String[] args) {
MyFrame panel = new MyFrame("D");
JFrame frame = new JFrame();
frame.add(panel);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(200, 300);
frame.setVisible(true);
}
}class MyFrame extends JPanel{
private int x=0;
private int y = 0;
MyFrame(String s) {
this.requestFocus();
this.setFocusable(true);
this.setLayout(null);
this.setBounds(100, 100, 200, 300);
this.setBackground(Color.white);
this.setVisible(true);
this.addMouseListener(new MouseAdapter() {
public void mousePressed(MouseEvent e) {
x = e.getX();
y = e.getY();
paint(MyFrame.this.getGraphics());
}
});
} @Override
public void paint(Graphics g) {
// TODO Auto-generated method stub
g.setColor(Color.blue);
g.clearRect(0, 0, 200, 300);
g.fillOval(x, y, 10, 10);
}
}
这些都破坏了已经存在的窗口,界面需要重新绘制,会调用该JFrame里面的contentPane中的paintComponent方法。
这里我把每次draw的点记录下来,并重写paintCompont,每次在调用paintCompont的时候都重新绘制这些点即可
import java.awt.*;
import java.awt.event.*;
import java.util.ArrayList;
import java.util.Arrays;import javax.swing.*;
public class D {
public static void main(String[] args){
new MyFrame("D");
}
}
class MyFrame extends JFrame{
MyFrame(String s){
super(s);
this.setLayout(null);
this.setBounds(100,100,200,300);
this.setBackground(Color.white );
this.setVisible(true);
final MyComponent my= new MyComponent();setContentPane(my);
this.addMouseListener(new MouseAdapter(){
public void mousePressed(MouseEvent e){
int x= e.getX();
int y =e.getY();
my.draw(my.getGraphics(),x,y);
}
}
);
this.addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e){
setVisible(false);
System.exit(0);
}});
}
class MyComponent extends JComponent{
ArrayList<Point> p=new ArrayList<Point>();
@Override
public void paintComponent(Graphics g){
for(Point point:p){
g.setColor(Color.blue);
g.fillOval((int)point.getX(),(int)point.getY(),10,10);
}
System.out.println(p);
}
public void draw(Graphics g,int x,int y){
g.setColor(Color.blue);
g.fillOval(x,y,10,10);
p.add(new Point(x,y));
}
}
}