Joke Collection Website - Public benefit messages - Java programming input 1 integer in the window, select whether to calculate the square value or factorial through the radio box, and output the calculation result.
Java programming input 1 integer in the window, select whether to calculate the square value or factorial through the radio box, and output the calculation result.
Import java.awt.event. *;
Import java.util.regex.matcher;
Import java.util.regex.pattern;
Import javax.swing. *;
The public class TestRadioButton extends JFrame to implement ActionListener{
Private jbutton objt1;
private JLabel mobjlLabelTime
private JTextField mobjS
private JLabel objlabTemp
private JComboBox mobjComboBox = null;
private string El = " ^[ 1-9]\\d*$ ";
Public TestRadioButton() {
this . setresizable(false);
setSize(300,200);
MobjlLabelTime = new JLabel ("input value:");
mobjlabeltime . set bounds( 10,45,60,30);
add(mobjlLabelTime);
mobjS = new JTextField(" 10 ");
mobjS.setBounds(60,50,50,20);
Add (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 ");
objT 1.setBounds(220,40,60,30);
Add (objt1);
objt 1 . addactionlistener(this);
//Set the closing program
this . addwindowlistener(new window adapter(){
public void window closing(window event e){
system . exit(0);
}
});
}
Public static void main(String[] args) {
TestRadioButton obj pane = new TestRadioButton();
obj pane . add(new JPanel());
obj pane . set visible(true);
}
@ Overlay
public void action performed(action event e){
if(objt 1 = = e . getsource()){
string string = mobjcombobox . getselecteditem()。 toString();
string strValue = mobjs . gettext();
Pattern p = pattern. compile (El);
matcher m = p . matcher(strValue);
Boolean b = m.matches ();
If (b) {
int ivalue = integer . parse int(str value);
If ("square". Equal to (string)) (
string = " Result:"+ivalue * ivalue;
} Otherwise {
int isum = 1;
for(int I = 2; I<= ivaluei++) {
isum = isum * I;
}
string = " Result:"+isum;
}
objlabtemp . settext(string);
} Otherwise {
ObjlabTemp.setText ("Please enter a valid number");
}
}
}
}
- Previous article:Why does my telecom card not have the right to redeem points?
- Next article:Will I get a text message when I file on the motorcycle line?
- Related articles
- How to cancel SMS notification for debit card of China Bank?
- How about Red Flag h9? Is it worth buying?
- Who knows the detailed station name of the newly added No.45 bus in Shantou?
- How to reply to the boss's daughter's marriage
- Is it true that the online loan sent a text message saying that it was a home visit?
- How did Summer Redstone make his fortune?
- What is the format of the letter?
- Dream of being scolded and cried by others
- What should I do if my mobile phone can receive short messages but can't send them?
- Zhen Zhen called me.