JPanel的repaint调用时应该是先自调用update(),再调用paint()
为什么我每次画的时候没有清空,而是新添画了一个
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
class Show extends JPanel{
ArrayList price;
static int w=500;
static int h=400;
public Show(ArrayList al){
price=al;
this.setSize(600,500);
}
public void setPrice(ArrayList al){
price=al;
}
public void paint(Graphics g){
if(price.size()>=2){
int[] x=new int[price.size()];
int[] y=new int[price.size()];
int mw=500/x.length;
for(int i=0;i<x.length;i++){
x[i]=w-i*mw;
y[i]=400-Integer.parseInt(price.get(i).toString());
}
g.fillOval(x[x.length-1]+50, y[x.length-1]+100, 10, 10);
/*for(int i=0;i<x.length;i++){
//g.fillOval(x[i]+50, y[i]+100, 3, 3);
}
for(int i=0;i<x.length-1;i++){
//g.drawLine(x[i]+50, y[i]+100, x[i+1]+50, y[i+1]+100);
}*/
}
}
}public class GuPiao extends JFrame implements Runnable,ActionListener{
public ArrayList price=new ArrayList();
public Show panel=new Show(price);;
JButton btn;
public GuPiao(){
btn=new JButton("a");
btn.setBounds(500,300, 35, 25);
//price.add(new Integer(200));
Container c = this.getContentPane();
//c.setBounds(50, 50, 600, 500);
c.setLayout(null);
c.add(panel);
c.add(btn);
btn.addActionListener(this);
this.setSize(650, 400);
this.setLocation((Toolkit.getDefaultToolkit().getScreenSize().width - this.getSize().width) / 2, (Toolkit.getDefaultToolkit().getScreenSize().height - this.getSize().height) / 2);
this.setVisible(true);
}
public void actionPerformed(ActionEvent e){
int p=(int)(Math.random()*100)+300;
price.add(new Integer(p));
panel.setPrice(price);
panel.repaint();
}
public void run(){
try{
for(int i=0;i<50;i++){
int p=(int)(Math.random()*100)+300;
System.out.println(p);
price.add(new Integer(p));
panel.setPrice(price);
this.panel.repaint();
Thread.sleep(1000);
}
}catch(Exception e){
System.out.println(e);
}
}
public static void main(String[] args){
GuPiao gp=new GuPiao();
//g.setPrice();
//Thread t=new Thread(gp);
//t.start();
}
}
为什么我每次画的时候没有清空,而是新添画了一个
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
class Show extends JPanel{
ArrayList price;
static int w=500;
static int h=400;
public Show(ArrayList al){
price=al;
this.setSize(600,500);
}
public void setPrice(ArrayList al){
price=al;
}
public void paint(Graphics g){
if(price.size()>=2){
int[] x=new int[price.size()];
int[] y=new int[price.size()];
int mw=500/x.length;
for(int i=0;i<x.length;i++){
x[i]=w-i*mw;
y[i]=400-Integer.parseInt(price.get(i).toString());
}
g.fillOval(x[x.length-1]+50, y[x.length-1]+100, 10, 10);
/*for(int i=0;i<x.length;i++){
//g.fillOval(x[i]+50, y[i]+100, 3, 3);
}
for(int i=0;i<x.length-1;i++){
//g.drawLine(x[i]+50, y[i]+100, x[i+1]+50, y[i+1]+100);
}*/
}
}
}public class GuPiao extends JFrame implements Runnable,ActionListener{
public ArrayList price=new ArrayList();
public Show panel=new Show(price);;
JButton btn;
public GuPiao(){
btn=new JButton("a");
btn.setBounds(500,300, 35, 25);
//price.add(new Integer(200));
Container c = this.getContentPane();
//c.setBounds(50, 50, 600, 500);
c.setLayout(null);
c.add(panel);
c.add(btn);
btn.addActionListener(this);
this.setSize(650, 400);
this.setLocation((Toolkit.getDefaultToolkit().getScreenSize().width - this.getSize().width) / 2, (Toolkit.getDefaultToolkit().getScreenSize().height - this.getSize().height) / 2);
this.setVisible(true);
}
public void actionPerformed(ActionEvent e){
int p=(int)(Math.random()*100)+300;
price.add(new Integer(p));
panel.setPrice(price);
panel.repaint();
}
public void run(){
try{
for(int i=0;i<50;i++){
int p=(int)(Math.random()*100)+300;
System.out.println(p);
price.add(new Integer(p));
panel.setPrice(price);
this.panel.repaint();
Thread.sleep(1000);
}
}catch(Exception e){
System.out.println(e);
}
}
public static void main(String[] args){
GuPiao gp=new GuPiao();
//g.setPrice();
//Thread t=new Thread(gp);
//t.start();
}
}
解决方案 »
- 关于Java输入流的问题
- 请问大虾,jsp自定义标签类是多个线程共享一个实例还是每个线程有自己的实例?
- 求一个算法,2个1维数组互相查找里面是否有不同的数据
- unix下java编译环境的配置问题
- 请教一个简单的问题,送分
- 如何把12345.67格式化成123,45.7
- JNI:如何在C++中调用Java Method?
- 一个小问题
- 请问师傅们这个类为什么没有构造函数呀?什么样的类不需要构造函数?
- 各位:用JAVA开发的应用程序窗体,难道在窗体的左上角必须要有一个咖啡杯的图标吗?能改称自己的吗?在那里改?
- java6如何读取office2007的pptx,excelx格式的文件内容?
- (!!!急)如何部分重绘 Java Swing Panel
不过楼主可以把Container c该为GuPiao类的字段,然后再把actionPerformed中该为c.repaint()试下,应该没问题