https://blog.csdn.net/qq_64314976/article/details/125843147

import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JPasswordField;
import javax.swing.JRadioButton;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;

public class MyJFrame extends JFrame{
JLabel jl1,jl2,jl3,jl4,jl5,jl6,jl7;
JTextField jtf;//文本框
JPasswordField jpf1,jpf2;//密码
JTextArea jta;
JButton jb1,jb2;
JRadioButton jrb1,jrb2;
JCheckBox jcb1,jcb2,jcb3;
JComboBox<String> jc;
public MyJFrame() {
this.setTitle("注册");
setBounds(300,300,500,600);
//设置窗体为流式布局
//setLayout(new FlowLayout());
//设置窗体为网格布局
setLayout(new GridLayout(20,1));
//空布局
setLayout(null);
init();
setVisible(true);
setDefaultCloseOperation(EXIT_ON_CLOSE);
jb1.addActionListener(new ActionListener(){

@Override
public void actionPerformed(ActionEvent arg0) {
String info="";
info="姓名:"+jtf.getText();
info+="\r\n"+"密码:"+jpf1.getText();
info+="\r\n"+"密码:"+jpf2.getText();
info+="\r\n"+"性别:";
if (jrb1.isSelected())
info+=jrb1.getText();
if (jrb2.isSelected())
info+=jrb2.getText();
info+="\r\n爱好:";
if(jcb1.isSelected())
info+=jcb1.getText();
if(jcb2.isSelected())
info+=jcb2.getText();
if(jcb3.isSelected())
info+=jcb3.getText();
info+="\r\n学历:"+jc.getSelectedItem();
info+="\r\n简历:"+jta.getText();
JOptionPane.showMessageDialog(null, info);
}

});
jb2.addActionListener(new ActionListener() {

@Override
public void actionPerformed(ActionEvent e) {
// System.exit(0);
//获取事件源对象
JButton jb=(JButton)e.getSource();
jtf.setText(jb.getText());
}
});
}
//注册用的组件初始化
public void init() {
// TODO Auto-generated method stub
jl1=new JLabel("用户名:");
jl2=new JLabel("密码:");
jl7=new JLabel("再次密码:");
jl3=new JLabel("简历:");
jl4=new JLabel("性别:");
jtf=new JTextField(10);
jpf1=new JPasswordField(10);
jpf2=new JPasswordField(10);
jta=new JTextArea(4,20);
jb1=new JButton("注册");
jb2=new JButton("取消");
jl1.setBounds(100,30,180,30);
jtf.setBounds(200,30,180,30);
jl2.setBounds(100, 70, 180, 30);
jpf1.setBounds(200, 70, 180, 30);
jl7.setBounds(100, 110, 180, 30);
jpf2.setBounds(200, 110, 180, 30);
jl3.setBounds(100, 280, 100, 30);
jta.setBounds(100,280,400,200);
JScrollPane jsp=new JScrollPane(jta);
jsp.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
jsp.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
jsp.setBounds(170, 280, 240, 100);
jb1.setBounds(100, 400, 100, 30);
jb2.setBounds(220, 400, 100, 30);
jrb1=new JRadioButton("男");
jrb2=new JRadioButton("女");
jl4.setBounds(100, 150, 180, 30);
jrb1.setBounds(200,150, 100, 30);
jrb2.setBounds(300,150, 100, 30);
ButtonGroup bg=new ButtonGroup();
bg.add(jrb1);
bg.add(jrb2);
jl5=new JLabel("爱好:");
jcb1=new JCheckBox("游戏");
jcb2=new JCheckBox("篮球");
jcb3=new JCheckBox("旅游");
jl5.setBounds(100 , 240, 100, 30);
jcb1.setBounds(160 , 240, 100, 30);
jcb2.setBounds(260 , 240, 100, 30);
jcb3.setBounds(360 , 240, 100, 30);
jl6=new JLabel("学历:");
jc=new JComboBox<>();
jc.addItem("高中");
jc.addItem("大专");
jc.addItem("本科");
jc.addItem("研究生");
jl6.setBounds(100 , 200, 100, 30);
jc.setBounds(200 , 200, 100, 30);
add(jl1);
add(jtf);
add(jl2);
add(jpf1);
add(jl7);
add(jpf2);
add(jl3);
add(jsp);
add(jl4);
add(jrb1);
add(jrb2);
add(jl5);
add(jcb1);
add(jcb2);
add(jcb3);
add(jl6);
add(jc);
add(jb1);
add(jb2);
}
public static void main(String[] args) {
new MyJFrame();
}
}

posted @ 2024-04-18 14:55  一只蜗牛hhhh  阅读(5)  评论(0编辑  收藏  举报