定义了一个多线程的panel,按照一定时间间隔在上面绘图,当线程中止的时候,如何清楚panel上留下的内容,还有,线程如何销毁,即与new对应的操作是什么,谢谢!
我简单写了一个程序如下,如何在stop之后清空panel上的内容,即如何实现clear按钮。谢谢!
import java.awt.*;
import java.awt.event.*;
import java.awt.font.*;
import java.awt.geom.*;
import java.applet.*;
import javax.swing.*;
import javax.swing.event.*;
import javax.swing.table.*;
import java.util.*;
import java.sql.*;
/**
 */public class iSnamp extends Applet {
  private boolean isStandalone = false;  JPanel NodeListPanel = new JPanel();
  ClassLinePlot lp = new ClassLinePlot();
  JButton jButton1 = new JButton();
  JButton jButton2 = new JButton();
  JButton jButton3 = new JButton();
  JLabel label = new JLabel();// for test
//  DefaultTableModel NodeList = new DefaultTableModel();//Get a parameter value
  public String getParameter(String key, String def) {
    return isStandalone ? System.getProperty(key, def) :
        (getParameter(key) != null ? getParameter(key) : def);
  }  //Construct the applet
  public iSnamp() {
  }
  //Initialize the applet
  public void init() {
    try {
      jbInit();
    }
    catch(Exception e) {
      e.printStackTrace();
    }
  }
  //Component initialization
  private void jbInit() throws Exception {
    this.setLayout(null);
    jButton1.setBounds(new Rectangle(10, 150, 80, 25));
    jButton1.setText("Run");
    jButton1.addActionListener(new iSnamp_jButton1_actionAdapter(this));
    jButton2.setBounds(new Rectangle(10, 200, 80, 25));
    jButton2.setText("Hide");
    jButton2.addActionListener(new iSnamp_jButton2_actionAdapter(this));
    jButton3.setBounds(new Rectangle(10, 250, 80, 25));
    jButton3.setText("Clear");
    jButton3.addActionListener(new iSnamp_jButton3_actionAdapter(this));
    lp.setBounds(0,0,100,100);
    lp.reveal();    this.add(jButton1);
    this.add(jButton2);
    this.add(jButton3);
    this.add(lp);  }
//Start the applet
  public void start() {
  }//Stop the applet
  public void stop() {
  }//Destroy the applet
  public void destroy() {
  }//Get Applet information
  public String getAppletInfo() {
    return "Applet Information";
  }//Get parameter info
  public String[][] getParameterInfo() {
    return null;
  }//Main method
  public static void main(String[] args) {
    iSnamp applet = new iSnamp();
    applet.isStandalone = true;
    Frame frame;
    frame = new Frame();
    frame.setTitle("Applet Frame");
    frame.add(applet, BorderLayout.CENTER);
    applet.init();
    applet.start();
//    frame.setSize(400,320);
    Dimension d = Toolkit.getDefaultToolkit().getScreenSize();
    frame.setLocation((d.width - frame.getSize().width) / 2, (d.height - frame.getSize().height) / 2);
    frame.setVisible(true);
  }  void jButton1_actionPerformed(ActionEvent e) {
    if (jButton1.getText() == "Run")
    {
      lp.start();
      jButton1.setText("Stop");
    }
    else
    {
      lp.stop();
      jButton1.setText("Run");
    }  }  void jButton2_actionPerformed(ActionEvent e) {
    if (jButton2.getText() == "Hide")
    {
      lp.hide();
      jButton2.setText("Show");
    }
    else
    {
      lp.reveal();
      jButton2.setText("Hide");
    }  }  void jButton3_actionPerformed(ActionEvent e) {
//    lp.clear();
  }
}class ClassMultiThreadPanel extends JPanel implements Runnable
{
  int id = 0;
  int SleepInterval;//sleep interval.
  boolean RunThread;    //flag that control the running of the thread
  boolean Visible;
  Thread PanelThread=null;  ClassMultiThreadPanel()
  {
    SleepInterval = 100;
    RunThread = true;
    Visible = false;
  }
  public void hide()
  {
    Visible = false;
  }  public void reveal()
  {
    Visible = true;
  }  public void runthread()
  {
    RunThread = true;
  }  public void pause()
  {
    RunThread = false;
  }  public void setSleepInterval(int sleepinterval)
  {
    SleepInterval = sleepinterval;
  }  public int getSleepInterval()
  {
    return SleepInterval;
  }
  public void start()
  {
    RunThread = true;
    if (PanelThread==null)
    {
      PanelThread=new Thread (this);
      PanelThread.start();
    }
  }  public void stop()
  {
    RunThread = false;
    if ((PanelThread != null))//&&(PanelThread.isAlive()))
    {
      PanelThread = null;
    }
  }  public void run()
  {
    while(RunThread)
    {
      repaint();    //clear the canvas and repaint
      try{Thread.sleep(SleepInterval);}    //sleep
      catch(InterruptedException e){}
    }
  }
  public void repaint()
  {
    super.repaint();//important! clear the canvas
  }
}class ClassLinePlot extends ClassMultiThreadPanel
{  ClassLinePlot()
  {
    super();
  }  void show(Graphics g, int id)  //show the plot
  {
    if (Visible)
      g.drawString(Integer.toString(id),10,30);    //update data first!
    //draw the line plot here!
  }
  void clear()
  {
    super.repaint();
  }  public void paint(Graphics g)
  {
    super.paint(g);
    id++;
    this.show(g,id);            //show the line plot
  }
}
class iSnamp_jButton1_actionAdapter implements java.awt.event.ActionListener {
  iSnamp adaptee;  iSnamp_jButton1_actionAdapter(iSnamp adaptee) {
    this.adaptee = adaptee;
  }
  public void actionPerformed(ActionEvent e) {
    adaptee.jButton1_actionPerformed(e);
  }
}class iSnamp_jButton2_actionAdapter implements java.awt.event.ActionListener {
  iSnamp adaptee;  iSnamp_jButton2_actionAdapter(iSnamp adaptee) {
    this.adaptee = adaptee;
  }
  public void actionPerformed(ActionEvent e) {
    adaptee.jButton2_actionPerformed(e);
  }
}class iSnamp_jButton3_actionAdapter implements java.awt.event.ActionListener {
  iSnamp adaptee;  iSnamp_jButton3_actionAdapter(iSnamp adaptee) {
    this.adaptee = adaptee;
  }
  public void actionPerformed(ActionEvent e) {
    adaptee.jButton3_actionPerformed(e);
  }
}

解决方案 »

  1.   

    线程不用销毁,run方法结束线程就自动销毁了,不过线程销毁了但是线程类还在,清除上面的内容很简单,你随便做一个通知的方法,在结束之前调用一下不就行了?
      

  2.   

    没仔细看你的程序。
    给线程对象的引用赋个空值就销毁了,
    用clearRect(int x, int y, int width, int height)清除
    你以前用VC吧?
      

  3.   

    清除很容易啊
    调用一下super.paint()就可以了
      

  4.   

    谢谢大家!
    呵呵,能否稍微详细一点,改写一下jButton3_actionPerformed,谢谢!
    我以前用Delphi的,初学JAVA,请多指教,谢谢!
      

  5.   

    to sxnucseven(No.7):
    在哪里调用clearRect(int x, int y, int width, int height)呀,我改写clear()方法,如果要使用clearRect函数就必须传递Graphics参数,但是调用clear()的时候怎么传Graphics参数呢?panel里的Graphics参数是怎么回事呢,有没有这个成员变量啊。
    to lbfhappy(千里冰封) :
    在哪里调用super.paint()呢,paint要有参数Graphic把?初学者,问题可能很白痴,请包涵,谢谢!
      

  6.   

    程序里的stop()方法可以销毁进程把:
      public void stop()
      {
        RunThread = false;
        if ((PanelThread != null))//&&(PanelThread.isAlive()))
        {
          PanelThread = null;
        }
      }
    还有程序开始的时候就在panel上输出了一个1,如何让程序只在点击run的时候才运行?
    谢谢!
      

  7.   

    Graphics 只是一个接口,具体实现是不需要知道的,在这里是通过this.getGraphics()得到的,这是jPanel父类JComponent的方法。
      

  8.   

    还有程序开始的时候就在panel上输出了一个1,如何让程序只在点击run的时候才运行?
    谢谢!=============================把线程的start()方法放在run按钮的响应函数里就行了
      

  9.   

    第一个问题,我知道了,谢谢!
    我现在是这么做的呀:
      void jButton1_actionPerformed(ActionEvent e) {
        if (jButton1.getText() == "Run")
        {
          lp.start();
          jButton1.setText("Stop");
        }
        else
        {
          lp.stop();
          jButton1.setText("Run");
        }  }
    别的地方都没有出现lp.start();
      

  10.   

    但是在创建的时候还是会显示,是不是在new的时候出现的?
      

  11.   

    调试一下看看还有哪调用start方法了。
    不被调用是不会run的
      

  12.   

    使用clearRect(),会使(int x, int y, int width, int heigh)的范围变成白色,而如果这个panel在另一个panel上,则会变成一块白色,有没有更好的方法?比如调用什么repaint()。
    谢谢!
      

  13.   

    调用自己的repaint,会显示当前的内容,调用super.repaint没反映。
    能否麻烦在我上面的代码上实现一下,多谢了!
      

  14.   

    去看看JDK文档或者其它比较专业的技术网站吧
      

  15.   

    关于panel的清楚,还请大家关注~
      

  16.   

    那天下班了,我试了一下这样可以。
    在ClassMultiThreadPanel类里写一个方法
    void paintIt() {
    super.paintComponent(this.getGraphics());
    }
    方法jButton3_actionPerformed(ActionEvent e)里写
    lp.stop();
    lp.paintIt();