Package com.lovo.frame;
Import Java.awt.Color;
Import Java.awt.Container;
Import Java.awt.Font;
Import Java.awt.Toolkit;
Import Javax.swing.BorderFactory;
Import Javax.swing.ButtonGroup;
Import Javax.swing.ImageIcon;
Import Javax.swing.JButton;
Import Javax.swing.JCheckBox;
Import Javax.swing.JComboBox;
Import Javax.swing.JFrame;
Import Javax.swing.JLabel;
Import Javax.swing.JPasswordField;
Import Javax.swing.JRadioButton;
Import Javax.swing.JScrollPane;
Import Javax.swing.JTextArea;
Import Javax.swing.JTextField;
public class MyFrame extends jframe{
Private Container contentp;//Content panel
Private JLabel usernamelab;//Label
Private JLabel imglab;//Label
Private JButton okbtn;//button
Private JTextField inputtxt;//text box
Private JPasswordField passwordtxt;//Password box
Private JComboBox classcomb;//drop-down box
Private Jradiobutton malerad;//male Radio Box
Private Jradiobutton femalerad;//female Radio Box
Private Jcheckbox swimcheck;//check box
Private JTextArea msgtxtarea;//Text field
Public MyFrame () {
Toolkit tk = Toolkit.getdefaulttoolkit ();//Get Tool class
This.setsize (500, 400);//Set size
This.setlocation ((int) (Tk.getscreensize (). GetWidth ()-500)/2,
(int) (Tk.getscreensize (). GetHeight ()-400)/2);//Set Location
This.settitle ("My first form");//Set Caption
This.seticonimage (Tk.createimage ("img/wtp_icon_x16.gif"));//Set Icon file
This.setresizable (false);//form size is not variable
This.setdefaultcloseoperation (this. Exit_on_close);//Set the form to close the program end
This.addcontent ();
This.setvisible (TRUE);//Set form visible---written in the last
}
This method specifically completes the operation of the content panel and its inside components
private void Addcontent () {
THIS.CONTENTP = This.getcontentpane ();//Get Content panel
This.contentP.setBackground (Color.White);//Set the background color of the content panel
This.contentP.setLayout (null);//Set the layout manager of the content panel to be empty, so that the component location and size we give will only work
Use of components
Text labels
This.usernamelab = new JLabel ();//Generate Component Object
This.usernameLab.setText ("User name:");//Set Label text
This.usernameLab.setFont (New Font ("script", Font.Bold, 24));//Set Text font
This.usernameLab.setForeground (new color (203,86,220));//Set text color
This.usernameLab.setSize (120, 50);
This.usernameLab.setLocation (100, 0);
Debug the unique code of the label size location-comment out after the adjustment
This.usernameLab.setBorder (Borderfactory.createlineborder (Color.Black));
This.contentP.add (This.usernamelab);//represents placing the label in the container where it resides
Picture label
This.imglab = new JLabel ();
This.imgLab.setIcon (New ImageIcon ("Img/mypic. JPG ");//Set Picture
This.imgLab.setSize (161,34);
This.imgLab.setLocation (230, 5);
This.contentP.add (This.imglab);
Button
THIS.OKBTN = new JButton ();
This.okBtn.setText ("OK");
This.okBtn.setEnabled (false);//Set Component not available
This.okBtn.setFont (New Font ("Microsoft Jas Black", font.plain,18));
This.okBtn.setForeground (New Color (203,86,220));
This.okBtn.setBounds (0,60,100,40);
This.contentP.add (THIS.OKBTN);
text box
This.inputtxt = new JTextField ();
This.inputTxt.setFont (New Font ("Microsoft Jas Black", font.plain,16));
This.inputTxt.setForeground (New Color (203,86,220));
This.inputTxt.setBounds (110, 60, 120, 30);
This.contentP.add (This.inputtxt);
Password box
This.passwordtxt = new JPasswordField ();
This.passwordTxt.setEchoChar (' * ');
This.passwordTxt.setFont (New Font ("Microsoft Jas Black", font.plain,16));
This.passwordTxt.setForeground (New Color (203,86,220));
This.passwordTxt.setBounds (240, 60, 120, 30);
This.contentP.add (This.passwordtxt);
drop-down box
This.classcomb = new JComboBox ();
This.classComb.addItem ("J130");
This.classComb.addItem ("J131");
This.classComb.addItem ("J132");
This.classComb.setSelectedIndex (2);
This.classComb.setEditable (TRUE);//settings can be edited
This.classComb.setBounds (2, 110, 120, 30);
This.contentP.add (This.classcomb);
Radio Box
This.malerad = new Jradiobutton ("male");
This.femalerad = new Jradiobutton ("female");
This.maleRad.setSelected (TRUE);//Set Radio box is selected by default
This.maleRad.setBackground (Color.White);
This.femaleRad.setBackground (Color.White);
This.maleRad.setBounds (125, 110, 50, 30);
This.femaleRad.setBounds (180, 110, 50, 30);
This.contentP.add (This.malerad);
This.contentP.add (This.femalerad);
Buttongroup bg = new Buttongroup ();
Bg.add (This.malerad);
Bg.add (This.femalerad);
check box
This.swimcheck = new Jcheckbox ("swimming");
This.swimCheck.setSelected (TRUE);
This.swimCheck.setBounds (240, 110, 80, 30);
This.contentP.add (This.swimcheck);
Text fields--No outside border by default
This.msgtxtarea = new JTextArea ();
This.msgTxtArea.setFont (New Font ("Microsoft Jas Black", font.plain,16));
JScrollPane sp = new JScrollPane (This.msgtxtarea);
Sp.setbounds (5, 150, 200, 200);
This.contentP.add (SP);
}
}
Java Graphical user interface programming