代码如下
package workflow;import java.awt.*;
import java.awt.event.*;
import java.applet.*;
import com.borland.jbcl.layout.*;
import javax.swing.*;
public class Applet2 extends Applet implements ActionListener, MouseMotionListener,MouseListener{
  JScrollPane jScrollPane1 = new JScrollPane();
  MyButton b=new MyButton();
  MyButton[] b1=new MyButton[10];
  MyButton b3=new MyButton();
  int count=0;
  int x=0;
  int y=0;
  int count1=0;
  XYLayout xYLayout1 = new XYLayout();
   JPanel panel1 = new JPanel();
  JButton button1 = new JButton();
  JButton button2 = new JButton();
  JButton button3 = new JButton();
  JButton button4 = new JButton();
  JButton button5 = new JButton();
  JButton button6 = new JButton();
  JOptionPane jOptionPane1 = new JOptionPane();
  XYLayout xYLayout2 = new XYLayout();
  JLabel label1 = new JLabel();
  JPopupMenu jPopupMenu1 = new JPopupMenu();
  JPanel panel2 = new JPanel();
  XYLayout xYLayout3 = new XYLayout();  public Applet2()
{
}  public void init() {    try
    {      jbInit();    }    catch(Exception e)
    {      e.printStackTrace();    }  }  private void jbInit() throws Exception {
    panel2.setBackground(Color.lightGray);
    panel2.setLayout(xYLayout3);
    this.setLayout(xYLayout1);
    panel1.setLayout(xYLayout2);
    label1.setBackground(Color.black);
    label1.setFont(new java.awt.Font("Dialog", 1, 16));
    label1.setForeground(Color.white);
    jPopupMenu1.addMouseListener(this) ;    panel1.setBackground(SystemColor.desktop);
    panel1.addMouseListener(this);
    xYLayout1.setWidth(728);
    xYLayout1.setHeight(500);
    this.setBackground(SystemColor.desktop);///    panel1.add(jOptionPane1,      new XYConstraints(185, 68, 137, 83));
///    panel1.add(button1,  new XYConstraints(13, 0, -1, -1));
    panel1.add(button2,  new XYConstraints(13, 45, -1, -1));
    panel1.add(button3, new XYConstraints(13, 84, -1, -1));
    panel1.add(button4, new XYConstraints(13, 125, -1, -1));
    panel1.add(button5,  new XYConstraints(13, 165, -1, -1));
    panel1.add(button6,  new XYConstraints(13, 205, -1, -1));
    panel1.add(label1,  new XYConstraints(13, 239, 80, 27));
    this.add(panel1,  new XYConstraints(4, 88, 90, 381));
    this.add(jScrollPane1,                                          new XYConstraints(113, 4, 556, 488));
    jScrollPane1.getViewport().add(panel2, null);    button1.setIcon (new ImageIcon(this.getClass ().getResource ("start.gif")));
    button2.setIcon (new ImageIcon(this.getClass ().getResource ("stop1.gif")));
    button3.setIcon (new ImageIcon(this.getClass ().getResource ("doc2.gif")));
    button4.setIcon (new ImageIcon(this.getClass ().getResource ("reduce.gif")));
    button5.setIcon (new ImageIcon(this.getClass ().getResource ("zoom.gif")));
    button6.setIcon (new ImageIcon(this.getClass ().getResource ("doc2.gif")));    button1.addMouseListener(this);
    button2.addMouseListener(this);
    button3.addMouseListener(this);
    button4.addMouseListener(this);
    button5.addMouseListener(this);
    button6.addMouseListener(this);
    button1.addActionListener(this);
    button2.addActionListener(this);
    button3.addActionListener(this);
    button4.addActionListener(this);
    button5.addActionListener(this);
    button6.addActionListener(this);
 }
public void mouseExited(MouseEvent e)
{
        panel1_mouseExited(e);
 } public void mouseClicked(MouseEvent e)
 {
int button=e.getClickCount();
if(button==2)
{
String classname=jOptionPane1.showInputDialog(this,"请输入类名","请输入类名",2);
int waycount =new Untitled1().waycount(classname);
int x= (int) b1[0].getLocation().getX()+60;
int y= (int) b1[0].getLocation().getY()+15;
int dy=waycount*30;
panel2.getGraphics().drawLine(x+20,y+dy/2,x+20,y-dy/2);
}
}
//注释按钮地作用
  public void mouseEntered(MouseEvent e){    if(e.getSource()==(Object) button1)
       label1.setText("开始流程");
       label1.setBackground(Color.red);    if(e.getSource()==(Object) button2)
       label1.setText("结束流程");    if(e.getSource()==(Object) button3)
       label1.setText("加入子事件");
    if(e.getSource()==(Object) button6)
       label1.setText("加入同级事件");    if(e.getSource()==(Object) button4)
       label1.setText("放大视图");    if(e.getSource()==(Object) button5)
       label1.setText("缩小视图");}
public void mouseReleased(MouseEvent e)
{}public void mousePressed(MouseEvent e)
{}public void mouseDragged(MouseEvent e)
{}
//清空注释框
  void panel1_mouseExited(MouseEvent e)
  {    label1.setText("");  }
  public void mouseMoved(MouseEvent e)
  {  }  public void actionPerformed(ActionEvent e)  {
if(e.getSource()==(Object) button1)
{
   count1=3;
   XYLayout f=new XYLayout();
   panel2.setLayout(f);
   panel2.add(b,new XYConstraints(20, 160, 60, 25));
   b.setIcon (new ImageIcon(this.getClass ().getResource ("start.gif")));
   b.item=1.0;
   panel2.invalidate();
   panel2.validate();
   x= (int) b.getLocation().getX();
   y= (int) b.getLocation().getY();
   panel2.getGraphics().drawLine(x+60,y+15,x+80,y+15);   b1[count]=new MyButton();
   panel2.add(b1[count],new XYConstraints(x+80, y, 60, 25));
   b1[count].setIcon (new ImageIcon(this.getClass ().getResource ("doc2.gif")));
   panel2.invalidate();
   panel2.validate();
   panel2.getGraphics().drawLine((int) b1[count].getLocation().getX()+60,(int) b1[count].getLocation().getY()+15,(int) b1[count].getLocation().getX()+80,(int) b1[count].getLocation().getY()+15);   b1[count].addMouseListener(this);
   count=count+1;}if(e.getSource()==(Object) button3 )
{   b1[count]=new MyButton();
   panel2.add(b1[count],new XYConstraints((int) b1[count].getLocation().getX()+210, y-30,60, 25));
   b1[count].setIcon (new ImageIcon(this.getClass ().getResource ("doc2.gif")));
   panel2.invalidate();
   panel2.validate();
   drawline();
   count=count+1;
}if(e.getSource()==(Object) button4 )
{
}  }  public void paint(Graphics g)
  {
  panel1.updateUI();
  panel2.updateUI();
  drawline();
  b.updateUI();for (int i=0 ;i<10;i++ )
{
if (b1[i]!=null)
    {
  b1[i].updateUI();
    }
 }    System.out.println("paint");  }  public void update(Graphics g)
  {
System.out.println("update");
  }
public void repaint()
{
System.out.println("repaint");
}
  public void drawline()
  {  for (int i=0;i<10;i++)
  {
  if (b1[i]!=null )
  {
  int fillx=(int) b1[i].getLocation().getX();
  int filly=(int) b1[i].getLocation().getY();
  panel2.getGraphics().drawLine(fillx+60,filly+15,fillx+100,filly+15);
  }  }}
  }

解决方案 »

  1.   

    看睛面的代码你就会明白了。
    import java.applet.Applet;
    import java.awt.*;
    import java.awt.event.*;public class Polylines extends Applet {
    private static Color[] colors = { 
    Color.white, Color.black, Color.blue, Color.red, 
    Color.yellow, Color.orange, Color.cyan, Color.pink, 
    Color.magenta, Color.green }; public void init() {
    Button button = new Button("repaint");
    add(button);
    button.addActionListener(new ActionListener() {
    public void actionPerformed(ActionEvent event) {
    Polylines.this.repaint();
    }
    });
    }
    public void paint(Graphics g) {
    int arraySize = ((int)(Math.random()*100));
    int[] xPoints = new int[arraySize];
    int[] yPoints = new int[arraySize]; for(int i=0; i < xPoints.length; ++i) {
    xPoints[i] = ((int)(Math.random()*200)); 
    yPoints[i] = ((int)(Math.random()*200)); 
    }
    g.setColor(colors[(int)(Math.random()*10)]);
    g.drawPolyline(xPoints, yPoints, xPoints.length); showStatus(arraySize + " points");
    }
    }
      

  2.   

    我并没有在你的原代码里找到你重载了JPanel的paintComponent(Graphics g)
    方法啊!
    是这样的,如果你用jpanel.getGraphics()得到的Graphics句柄它只是某一个时候的对象,所以这个句柄是会丢失的。你可以试试下面这样
    class MyPanel extends JPanel
    {
        public void paintComponent(Graphics g)
        {
            g.setColor(Color.orange);
            g.fillRect(30,30,30,30);
        }
    }
    你把这个类的对象加到窗口中,应该就实现你的要求了