Importjava.awt.BorderLayout;ImportJava.awt.Color;Importjava.awt.GridLayout;Importjava.awt.event.ActionEvent;ImportJava.awt.event.ActionListener;ImportJava.util.Random;ImportJavax.swing.JButton;ImportJavax.swing.JFrame;ImportJavax.swing.JLabel;ImportJavax.swing.JPanel;ImportJavax.swing.JTextField; Public classRp_frame2extendsJFrame {Private Static Final LongSerialversionuid = 1L; Publicrp_frame2 () {setbounds (200, 200, 500, 300);//set the form size positionSettitle ("character calculator");//set the form titleJPanel Pnbasic=NewJPanel ();//Create a large canvasSetcontentpane (Pnbasic);//put in a formPnbasic.setlayout (NewGridLayout (2, 1));//Canvas follows a grid layout of two rows and rows and columns spaced 5 pixelsJPanel Pngreen =NewJPanel ();//recycled into a little green canvasJPanel Pnyellow =NewJPanel ();//into a little yellow canvas .Pnyellow.setbackground (Color.yellow);//Canvas Setting ColorPngreen.setbackground (Color.green);//Canvas Setting ColorPnbasic.add (Pnyellow); Pnbasic.add (Pngreen); //the bottom row of the green canvas adds labels as outputJLabel result =NewJLabel (); Pngreen.add (result); Result.settext (After entering the name, click on the ' Test character ' button to see the character value! "); //top row Yellow canvas re-layoutPnyellow.setlayout (NewBorderLayout ()); JLabel label=NewJLabel ();//Generate LabelsLabel.settext ("Enter Name"); Pnyellow.add (label, borderlayout.west);//put it on the left side of the yellow canvasLabel.setbackground (Color.yellow); JTextField text=NewJTextField (15);//Generate a text box of length 15Text.setbackground (Color.yellow); Pnyellow.add (text, borderlayout.center); JButton btn=NewJButton ();//Generate buttonBtn.setbackground (Color.yellow); Pnyellow.add (BTN, borderlayout.east);//put it on the right side of the yellow canvasBtn.settext ("Test character"); Btn.addactionlistener (NewActionListener () {@Override Public voidactionperformed (ActionEvent e) {Random ran=NewRandom (); intindex = Ran.nextint (101); if(Index >= 90) {Result.settext (Text.gettext ()+ "Your character value is" + Index + "level" + "Great God"); } Else if(Index >= 80) {Result.settext (Text.gettext ()+ "Your character value is" + Index + "level" + "Daniel"); } Else if(Index >= 60) {Result.settext (Text.gettext ()+ "Your character value is" + Index + "level" + "program Ape"); } Else if(Index >= 40) {Result.settext (Text.gettext ()+ "Your character value is" + Index + "rank" + "code Farm"); } Else if(Index >= 20) {Result.settext (Text.gettext ()+ "Your character value is" + Index + "rank" + "code Animal"); } Else{Result.settext (Text.gettext ()+ "Your character value is" + Index + "level" + "rookie"); } } }); Setdefaultcloseoperation (jframe.exit_on_close);//set the program to stop running when the form is closedSetVisible (true);//The Settings form is visible, otherwise nothing will be displayed } Public Static voidMain (string[] args) {Newrp_frame2 (); }}
Character Calculator JFrame form Software edition JPanel JTextField jtextarea Jbuttton JLabel Setcontentpane