新手萌新
话不多说
直接上代码
(ps:必要解释代码注释了,就不再赘述了。)
import javax.swing.JFrame;//视窗设计 恒定
import javax.swing.JPanel;//内窗口,必须有这个才可以在窗口创建按钮、文本、改变颜色等
import java.awt.Color;//调用颜色
import javax.swing.JButton;//按钮
import javax.swing.JLabel;//文字输入
import javax.swing.JTextField;//文本框
import javax.swing.JRadioButton;//选择按钮
import javax.swing.ButtonGroup;
import javax.swing.JComboBox;//组合框
public class MyLogin extends JFrame {
JPanel panel;
JButton b1,b2,b3;
JLabel c1,c2,c3,c4,c5,c6;
JTextField d1,d2,d5;
JRadioButton e1,e2;
ButtonGroup e;
JComboBox f;
//创建一系列必要的变量
MyLogin()
{
//create object of JPanel
panel = new JPanel();
panel.setSize(500, 500);//same as JFrame
panel.setBackground(Color.white);
panel.setLayout(null);
add(panel);
//create object of JButton
b1 = new JButton("Exit");
b2= new JButton("Register");
b3 = new JButton("Reset");
b1.setBounds(100, 300, 80, 30);
b2.setBounds(200, 300, 80, 30);
b3.setBounds(300, 300, 80, 30);
//add button to panel
panel.add(b1);
panel.add(b2);
panel.add(b3);
//create object of Label
c0 = new JLabel(" Registration Form ");
c1 = new JLabel("ID ");
c2 = new JLabel("Name ");
c3 = new JLabel("Gender ");
c4 = new JLabel("Adress ");
c5 = new JLabel("Contact ");
c6 = new JLabel("Select Country ");
c0.setBounds(180,10,150,30);
c1.setBounds(100,50,150,30);
c2.setBounds(100,80,150,30);
c3.setBounds(100,110,150,30);
c4.setBounds(100,140,150,30);
c5.setBounds(100,200,150,30);
c6.setBounds(100,230,150,30);
//前两位数字是坐标,后两位是长宽。
panel.add(c0);
panel.add(c1);
panel.add(c2);
panel.add(c3);
panel.add(c4);
panel.add(c5);
panel.add(c6);
//create object of JTextField
d1 = new JTextField();
d2 = new JTextField();
d5 = new JTextField();
d1.setBounds(220,50,150,28);
d2.setBounds(220,80,150,28);
d5.setBounds(220,200,150,28);
//add JTextField to panel
panel.add(d1);
panel.add(d2);
panel.add(d5);
//create object of JRadioButton
e1 = new JRadioButton("Male");
e2 = new JRadioButton("Female");
e = new ButtonGroup();
e.add(e1);
e.add(e2);
e1.setBounds(220,110,70,30);
e2.setBounds(300,110,70,30);
panel.add(e1);
panel.add(e2);
//create object of JComboBox
f = new JComboBox();
f.addItem("China");
f.addItem("India");
f.addItem("the UK");
f.addItem("the USA");
f.addItem("Japan");
f.addItem("Canda");
f.addItem("");
f.setBounds(220,240,90,30);
panel.add(f);
//Frame properties
this.setTitle("Login window");
this.setSize(500,500);
//to close Jframe
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setLayout(null);
this.setVisible(true);
}
public static void main(String[] args) {
MyLogin obj = new MyLogin();
}
}
运行结果如下:
萌新上路 不喜勿喷。
希望对大家有所帮助。