import java.applet.Applet;
import java.awt.Button;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JOptionPane;public class Task6_18 extends Applet {
int size ;
public void init(){
Button bt = new Button("点击我弹出输入框");
bt.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e) {
String s = JOptionPane.showInputDialog(null, "请输入:");
size = Integer.parseInt(s);
repaint();
}
});
this.add(bt);
}
public void paint(Graphics g){
if(size>0){
squareOfAsterisks(g,size);
}
}
public void squareOfAsterisks(Graphics g, int size){
String str="";
int x=50,y=100;
for(int j=0;j<size;j++)
str+="* ";
for(int i=0;i<size;i++){
g.drawString(str, x, y);
y+=10;
}
}
}
相关分类