改后的代码,请你仔细比较
import java.applet.*;
import java.awt.*;
import java.awt.event.*;
public class ChangeFont extends Applet implements ActionListener {
int size = 10;
Button btnBig = new Button("放大");
Button btnSmall = new Button("缩小");
public void init() {
add(btnBig);
add(btnSmall);
btnBig.addActionListener(this);
btnSmall.addActionListener(this);
}
public void actionPerformed(ActionEvent e) {
if (e.getSource() == btnBig) {
size = size + 1;
repaint();
} else if (e.getSource() == btnSmall) {
size = size - 1;
repaint();
}
}
public void paint(Graphics g) {
Font f = new Font("宋体", Font.BOLD, size);
g.setFont(f);
g.setColor(Color.RED);
g.drawString("Hello World!", 10, 100);
}
}