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.

I used the drop-down box.

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");

}

}

}

}