JAVA编程:编写一个applet,读取一个矩形的边长,然后输入一个空心矩形
import java.awt.*;
import javax.swing.*;
public class drawRech extends JApplet {
public void paint(Graphics g){
super.paint(g);
this.setBackground(Color.WHITE);
String width = JOptionPane.showInputDialog("Enter the width");
String height = JOptionPane.showInputDialog("Enter the height");
int wid = 0;
int heig = 0;
try{
wid = Integer.parseInt(width);
heig = Integer.parseInt(height);
}
catch(NumberFormatException e){
JOptionPane.showMessageDialog(null, "Wrong input");
}
g.drawRect(100, 100, wid, heig);
}
}
已添加异常处理,测试成功
多重随机标签