package frame;
import javax.swing.*;
import java.awt.*;
public class Jframedemo extends JFrame {
JFrame jFrame = new JFrame();
Container container;
void way1() {
jFrame.setSize(1000, 1000);
jFrame.setVisible(false);
jFrame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
container = jFrame.getContentPane();
container.setLayout(null);
}
void way2() {
JMenuBar menubar;
JMenu menu1;
JMenu menu2;
JLabel nameL;
JLabel ageL;
JLabel highL;
JLabel weightL;
JLabel homeL;
JLabel schoolL;
JLabel deptL;
JTextField nameF;
JTextField ageF;
JTextField highF;
JTextField weightF;
JTextField homeF;
JComboBox<String> comboBox_1;
JScrollPane gd;
Font font = new Font("宋体", Font.PLAIN, 25);
//设置菜单栏
menubar = new JMenuBar();
menu1 = new JMenu("文件(F)");
menu2 = new JMenu("编辑(E)");
menubar.add(menu1);
menubar.add(menu2);
container.add(menubar);
menubar.setBounds(0, 0, 1000, 40);
//姓名身高年龄体重标签与文本框
//姓名
nameL = new JLabel("姓名 ");
nameL.setBounds(20, 50, 80, 50);
nameL.setFont(font);
nameF = new JTextField();
nameF.setBounds(90, 60, 300, 30);
container.add(nameF);
container.add(nameL);
//年龄
ageL = new JLabel("年龄 ");
ageL.setBounds(450, 50, 80, 50);
ageL.setFont(font);
ageF = new JTextField();
ageF.setBounds(520, 60, 300, 30);
container.add(ageF);
container.add(ageL);
//身高
highL = new JLabel("身高 ");
highL.setBounds(20, 150, 80, 50);
highL.setFont(font);
highF = new JTextField();
highF.setBounds(90, 160, 300, 30);
container.add(highF);
container.add(highL);
//体重
weightL = new JLabel("体重 ");
weightL.setBounds(450, 150, 80, 50);
weightL.setFont(font);
weightF = new JTextField();
weightF.setBounds(520, 160, 300, 30);
container.add(weightL);
container.add(weightF);
//家庭住址
homeL = new JLabel("家庭住址");
homeL.setBounds(0, 250, 120, 50);
homeL.setFont(font);
homeF = new JTextField();
homeF.setBounds(110, 260, 300, 30);
container.add(homeL);
container.add(homeF);
//选择学校
schoolL = new JLabel("请选择大学");
comboBox_1 = new JComboBox<>();
String items_1[] = {"郑州大学", "郑州西亚斯", "师范"};
// 下行不加<String>会因版本问题报错
ComboBoxModel<String> cm_1 = new DefaultComboBoxModel<>(items_1);
comboBox_1.setModel(cm_1);
comboBox_1.setFont(font);
schoolL.setBounds(0, 350, 200, 50);// 下拉框的坐标、尺寸。绝对布局
schoolL.setFont(font);
comboBox_1.setBounds(150, 360, 150, 30);
container.add(schoolL);
container.add(comboBox_1);
//选择系别
String[] xl = {"计算机科学与技术", "大专", "本科", "硕士", "博士"};
deptL = new JLabel("选择系别");
deptL.setBounds(0, 450, 200, 50);
deptL.setFont(font);
gd = new JScrollPane(new JList(xl));
gd.setFont(font);
gd.setBounds(150, 460, 150, 30);
container.add(gd);
container.add(deptL);
//按钮
JButton jButton1 = new JButton("注册");
JButton jButton2 = new JButton("取消");
jButton1.setBounds(300, 560, 80, 60);
jButton2.setBounds(400, 560, 80, 60);
container.add(jButton1);
container.add(jButton2);
}
void way3() {
JFrame jFrame1 = new JFrame();
jFrame1.setSize(1000, 500);
jFrame1.setVisible(true);
jFrame1.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
container = jFrame1.getContentPane();
container.setLayout(null);
Font font = new Font("宋体", Font.PLAIN, 25);
//姓名
JLabel nameL = new JLabel("username ");
nameL.setBounds(50, 50, 150, 50);
nameL.setFont(font);
JTextField nameF = new JTextField();
nameF.setBounds(190, 60, 200, 30);
container.add(nameF);
container.add(nameL);
JLabel ageL = new JLabel("password ");
ageL.setBounds(500, 50, 150, 50);
ageL.setFont(font);
JPasswordField ageF = new JPasswordField();
ageF.setBounds(620, 60, 200, 30);
container.add(ageF);
container.add(ageL);
JRadioButton teacher = new JRadioButton("教师");
JRadioButton student = new JRadioButton("学生");
JRadioButton admin = new JRadioButton("管理员");
//add the JRadioButtons to the ButtonGroup
ButtonGroup buttonGroup = new ButtonGroup();
buttonGroup.add(teacher);
buttonGroup.add(student);
buttonGroup.add(admin);
//add the JRadioButtons to the contentPane ,and set the Layout
container.add(teacher);
container.add(student);
container.add(admin);
teacher.setBounds(150, 100, 80, 30);
student.setBounds(300, 100, 80, 30);
admin.setBounds(450, 100, 80, 30);
JButton jButton = new JButton("确定");
jButton.setBounds(300, 200, 80, 50);
container.add(jButton);
jButton.addActionListener(e -> {
System.out.println("pass");
if (nameF.getText().equals("1234") && ageF.getText().equals("admin")&&student.isSelected()) {
jFrame.setVisible(true);
}
});
}
public static void main(String[] args) {
Jframedemo jframedemo = new Jframedemo();
jframedemo.way3();
jframedemo.way1();
jframedemo.way2();
}
}
标签:setBounds,container,50,add,窗体,跳转,new,JLabel,页面 From: https://blog.51cto.com/u_13529088/8965602