📄 scoreexceptiondemo.java
字号:
import java.awt.Frame;
import java.awt.Button;
import java.awt.GridLayout;
import java.awt.Label;
import java.awt.Panel;
import java.awt.TextField;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.BorderLayout;
class ScoreException extends Exception
{
public ScoreException()
{
super("Score is incorrect");
}
public String toString()
{
return "The score must between 0 and 100";
}
}
public class ScoreExceptionDemo extends Frame{
Label lbl1;
Label lbl2;
Button btn1;
Button btn2;
TextField tf1;
/**
* @param args
*/
public ScoreExceptionDemo()
{
lbl1=new Label("Score");
tf1=new TextField(20);
lbl2=new Label(" ");
btn1=new Button("ok");
btn2=new Button("canel");
Panel p1=new Panel();
p1.setLayout(new GridLayout(2,2));
p1.add(lbl1);
p1.add(tf1);
p1.add(btn1);
p1.add(btn2);
this.add(p1,BorderLayout.CENTER);
this.add(lbl2,BorderLayout.SOUTH);
btn1.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent ae)
{
try
{
int num=Integer.parseInt(tf1.getText());
lbl2.setText(new Integer(checkNUmber(num)).toString());
}
catch(ScoreException se)
{
lbl2.setText(se.toString());
}
}
});
btn2.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent ae)
{
System.exit(0);
}
});
this.setSize(260,200);
this.setVisible(true);
}
public int checkNUmber(int num) throws ScoreException
{
if(num<0 || num>100)
throw new ScoreException();
return num;
}
public static void main(String[] args) {
// TODO Auto-generated method stub
new ScoreExceptionDemo();
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -