2016.3.11 (Java graphical user interface)

Source: Internet
Author: User

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)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.