導入Java . awt . event . *;
導入Java . util . regex . matcher;
導入Java . util . regex . pattern;
導入javax . swing . *;
公共類TestRadioButton擴展JFrame實現ActionListener{
私有JButton objt 1;
private JLabel mobjlLabelTime
private JTextField mobjS
private JLabel objlabTemp
private JComboBox mobjComboBox = null;
private string El = " ^[1-9]\\d*$ ";
公共TestRadioButton() {
this . setresizable(false);
setSize(300,200);
MobjlLabelTime = new JLabel("輸入值:");
mobjlabeltime . set bounds(10,45,60,30);
add(mobjlLabelTime);
mobjS = new JTextField(" 10 ");
mobjS.setBounds(60,50,50,20);
添加(mobjS);
String[] strBox = {"square "," factorial " };
mobjComboBox = new JComboBox(strBox);
mobjcombobox . set bounds(120,50,80,20);
add(mobjComboBox);
ObjlabTemp = new JLabel(" Result:");
objlabTemp.setBounds(10,80,300,20);
add(objlabTemp);
objt 1 = new JButton(" calculation ");
objT1.setBounds(220,40,60,30);
添加(objt 1);
objt 1 . addactionlistener(this);
//設置關閉程序
this . addwindowlistener(new window adapter(){
public void window closing(window event e){
system . exit(0);
}
});
}
公共靜態void main(String[] args) {
TestRadioButton obj pane = new TestRadioButton();
obj pane . add(new JPanel());
obj pane . set visible(true);
}
@覆蓋
public void action performed(action event e){
if(objt 1 = = e . getsource()){
string string = mobjcombobox . getselecteditem()。toString();
string strValue = mobjs . gettext();
模式p = pattern . compile(eL);
matcher m = p . matcher(strValue);
布爾b = m . matches();
如果(b) {
int ivalue = integer . parse int(str value);
If(“方”。等於(字符串)){
string = " Result:"+ivalue * ivalue;
}否則{
int isum = 1;
for(int I = 2;我& lt= ivaluei++) {
isum = isum * I;
}
string = " Result:"+isum;
}
objlabtemp . settext(string);
}否則{
ObjlabTemp.setText("請輸入有效數字");
}
}
}
}