一旦按下按钮在我的小程序上绘制答案,我想使用 drawString() ,但我无法弄清楚。我尝试了很多方法,我的程序可以编译,但在按下按钮时不会 drawString() 请帮忙。
import java.applet.Applet;
import java.awt.event.*;
import java.awt.Graphics.*;
import java.awt.*;
public class FortuneTellerApplet extends Applet {
Image image;
Button getFortune = new Button("Get Your Fortune");
Button tryAgain = new Button("Clear And Try Again");
TextField input = new TextField("Enter Question Here", 30);
public void init() {
image = getImage(getDocumentBase(), "webimages/crystalball.jpg");
getFortune.setBackground(Color.black);
getFortune.setForeground(Color.orange);
tryAgain.setBackground(Color.black);
tryAgain.setForeground(Color.orange);
input.setBackground(Color.black);
input.setForeground(Color.orange);
setLayout(new FlowLayout());
setBackground(Color.green);
add(getFortune);
add(tryAgain);
add(input);
MyHandler handler = new MyHandler();
getFortune.addActionListener(handler);
tryAgain.addActionListener(handler);
}
public void paint(Graphics g) {
g.drawImage(image, 12, 34, this);
}
public class MyHandler extends Button implements ActionListener {
public void actionPerformed(ActionEvent ev) {
if (ev.getSource()==getFortune) {
// >>>>>>>>> I want be able to use drawString() here <<<<<<<
} else if (ev.getSource()==tryAgain) {
input.setText("");
input.requestFocus();
}
}
}
}