Steps to establish a swing program
1 Creating a container
2 Building Components
3 Adding a build to a container
4 Setting up layouts
public class Simpleframe extends JFrame {
private int width;
private int height;
Private Container CONTENTP;
Private JLabel Usernamelabel;
Private JLabel Passwordlabel;
Private JTextField Usernamefield;
Private JPasswordField Passwordfield;
Private Jradiobutton malebtn;
Private Jradiobutton femalebtn;
Private Jcheckbox HobbyCheckBox1;
Private Jcheckbox HobbyCheckBox2;
Private Jcheckbox HobbyCheckBox3;
Private JComboBox Addresscombbox;
Private JTextArea Describetextarea;
Private JButton submitbtn;
Private JButton cancelbtn;
Public Simpleframe () {
This.width = 1024;
This.height = 600;
This.settitle ("My first GUI form");
This.setsize (This.width, this.height);
Toolkit tool = Toolkit.getdefaulttoolkit ();
Double width = tool.getscreensize (). GetWidth ();
Double height = tool.getscreensize (). GetHeight ();
This.setlocation ((int) (width-this.width)/2,
(int) (height-this.height)/2);
This.seticonimage (Tool.createimage ("img/logo.png"));
This.setlocationrelativeto (null);//Set the form to center
This.setdefaultcloseoperation (Jframe.exit_on_close);
This.addcontent ();
This.setvisible (TRUE);
}
public void Addcontent () {
THIS.CONTENTP = This.getcontentpane ();
This.contentP.setLayout (NULL);
Text and Input boxes
This.usernamelabel = new JLabel ("User name");
This.usernameLabel.setBounds (350, 10, 90,35);
This.usernameLabel.setFont (New Font ("Arial", Font.plain, 16));
This.contentP.add (This.usernamelabel);//Add a text component
This.usernamefield = new JTextField ();
This.usernameField.setBounds (450, 10, 200,35);
This.contentP.add (This.usernamefield);
This.passwordlabel = new JLabel ("password");
This.passwordLabel.setBounds (350, 60, 90,35);
This.passwordLabel.setFont (New Font ("Arial", Font.plain, 16));
This.contentP.add (This.passwordlabel);//Add a text component
Password box
This.passwordfield = new JPasswordField ();
This.passwordField.setBounds (450, 60, 200,35);
This.passwordField.setEchoChar (' * ');
This.passwordField.setForeground (color.red);
This.contentP.add (This.passwordfield);
Radio Box
JLabel gendertitle = new JLabel ("gender");
Gendertitle.setbounds (350, 110, 80, 30);
Gendertitle.setfont (New Font ("Arial", Font.plain, 16));
This.contentP.add (Gendertitle);
THIS.MALEBTN = new Jradiobutton ("male");
This.maleBtn.setBounds (440, 110, 50,35);
This.contentP.add (THIS.MALEBTN);
THIS.FEMALEBTN = new Jradiobutton ("female");
This.femaleBtn.setBounds (500, 110, 50,35);
This.contentP.add (THIS.FEMALEBTN);
Buttongroup bp = new Buttongroup ();
Bp.add (THIS.MALEBTN);
Bp.add (THIS.FEMALEBTN);
check box
This.hobbycheckbox1 = new Jcheckbox ("movement");
This.hobbycheckbox2 = new Jcheckbox ("movie");
This.hobbycheckbox3 = new Jcheckbox ("Music");
This.hobbyCheckBox1.setBounds (350, 160, 80,35);
This.hobbyCheckBox2.setBounds (440, 160, 80,35);
This.hobbyCheckBox3.setBounds (540, 160, 80,35);
This.contentP.add (This.hobbycheckbox1);
This.contentP.add (THIS.HOBBYCHECKBOX2);
This.contentP.add (THIS.HOBBYCHECKBOX3);
drop-down box
This.addresscombbox = new JComboBox (new string[]{"Chengdu", "Deyang", "Mianyang", "Leshan", "Ziyang"});
This.addressCombBox.addItem ("Dazhou");
This.addressCombBox.addItem ("Zigong");
This.addressCombBox.setSelectedIndex (5);
This.addressCombBox.setBounds (350, 210, 120, 25);
This.contentP.add (This.addresscombbox);
Text field
This.describetextarea = new JTextArea ();
This.describeTextArea.setBounds (350, 250, 300, 100);
This.describeTextArea.setColumns (5);
This.describeTextArea.setRows (3);
This.describeTextArea.setBorder (Borderfactory.createlineborder (Color.Black));
This.contentP.add (This.describetextarea);
Button
THIS.SUBMITBTN = new JButton ("OK");
This.submitBtn.setBounds (400, 400, 80, 30);
This.contentP.add (THIS.SUBMITBTN);
THIS.CANCELBTN = new JButton ("Cancel");
This.cancelBtn.setBounds (500, 400, 80, 30);
This.contentP.add (THIS.CANCELBTN);
}
2016.3.11 (Java graphical user interface)