Java Applet添加和删除标签

时间:2016-04-25 14:58:27

标签: java applet awt

我正在尝试制作代码,在我点击时添加标签,然后在按e时删除标签。有人可以帮助我做到这一点         我添加了标签,我得到它编译,但标签将不会出现,它为我的图形项目我工作真的很难

import java.awt.geom.*;
import java.awt.*;
import javax.swing.*;
import java.io.*;
import java.awt.event.*;
import java.applet.*;
import java.util.*;
import java.awt.Label;
public class Volcano_Client extends Applet implements MouseListener, KeyListener, Runnable
{
  Label label1;
 boolean pickE=false;
 boolean play = true;
 boolean makeLava=false;
 Thread t;
 Thread thr;
 Thread th;
 boolean mouseEntered;
 int count;
 Volcano v1;
 public void init()
 {
    label1 = new Label("You see lava flowing down as you press e");

    v1 = new Volcano();
    thr=new Thread(this);
    thr.start();
    t=new Thread(this);
    t.start();
    th= new Thread(this);
    count=0;
    addMouseListener(this);
    addKeyListener(this);
 }
 public void keyTyped(KeyEvent k)
 {
 }
 public void keyReleased(KeyEvent k)
 {
 }
 public void keyPressed(KeyEvent k)
 {
    if(k.getKeyCode() ==KeyEvent.VK_E)
    {
        pickE=true;
        repaint();
        k.consume();
    }
 }
    public void mouseExited(MouseEvent me)
 {
    mouseEntered=false;
    repaint();
 }
 public void mousePressed(MouseEvent me)
 {
 }
 public void mouseReleased(MouseEvent me)
 {
 }
 public void mouseEntered(MouseEvent me)
 {

    mouseEntered=true;
    repaint();
 }
 public void mouseClicked(MouseEvent me)
 {

    if(mouseEntered==true)
    {
        makeLava=true;
    }
    else
    {
        makeLava=false;
    }
    repaint();
 }
 public void run()
 {
     while(play==true)
    {
        try
        {
            Thread.sleep(100);
        }
        catch(InterruptedException e)
        {
            e.printStackTrace();
        }
        repaint();
    }
    while(true)
    {
        try{
            Thread.sleep(20);
        }
        catch(Exception e){};
        repaint();
    }
 }
 public void stop()
 {
 }
 public void start()
 {
 }
 public void paint(Graphics g)
 {
    Graphics2D g2 = (Graphics2D)g;
    label1 = new Label("You see lava flowing down as you press e");
    v1.makeVolcano(g2);
    if(makeLava)
    {
      v1.makeLavaUp(g2);
      g.setColor(Color.black);
      g.drawString("KABOOM!!!",200,70);
      add(label1);

    }
    else
    {
        g.setColor(Color.white);
        g.drawString("Please Click For Eruption part 1",160,250);
    }
    if(pickE)
    {
        v1.makeLavaFlow(g2);
        remove(label1);
    }
    else
    {
        //g.setColor(Color.white);
        //g.drawString("Please press 'E' for Eruption part 2",160,276);
    }

 }

}

1 个答案:

答案 0 :(得分:-1)

要显示标签,下面是修改后的init()函数:

 public void init()
`{`
`label1 = new Label("You see lava flowing down as you press e");`

       //v1 = new Volcano();
    thr=new Thread(this);
    thr.start();
    t=new Thread(this);
    t.start();
    th= new Thread(this);
    count=0;

    addMouseListener(this);
    addKeyListener(this);

    add(label1); //to display label
 }