实例(二)——制作个人简历
程序员文章站
2022-04-19 18:45:42
...
制作个人简历。(实现单选按钮,多选按钮,复选框,滚动窗格,注册监听、实现事件处理等)
Ⅰ、核心代码
初始化窗口:
private void init() {
this.setSize(400, 800);
this.setContentPane(getJContentPane());
this.setTitle("Resume");
this.addWindowListener(new java.awt.event.WindowAdapter() {
public void windowClosing(java.awt.event.WindowEvent e) {
System.exit(0);
}
});
this.setVisible(true);
}
单选按钮,比如:性别:只能选择男或者女,则使用单选按钮JRadioButton:
private JRadioButton getJRadioButton() {
if (rb1 == null) {
rb1 = new JRadioButton();
rb1.setBounds(new Rectangle(61, 62, 38, 26));
rb1.setText("男");
}
return rb1;
}
注册监听,实现事件处理:
jb1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent e) {})
提交信息后在文本区中显示
ta.setText("姓名:" + strname + "\r\n" + "性别:" + strsex + "\r\n" + "生日:" + strsr + "\r\n" + "专业:"+ strzy + "\r\n" + "文化:" + strwh + "\r\n" + "毕业院校:" + stryx + "\r\n" + "爱好:" + strhb1 + " "+ strhb2 + " " + strhb3 + " " + strhb4+ "\r\n" + "自我评价:" + strzp );
Ⅱ、运行结果
Ⅲ、 源代码
package edu.xatu.one ;
import java.awt.BorderLayout;
import java.awt.Color;
import javax.swing.*;
import java.awt.Dimension;
import java.awt.Rectangle;
public class Resume extends JFrame {
private JPanel jp;
private JLabel l1, l2, l3, l4, l5, l6, l7, l8;
private JTextField t, t1, t2;
private JRadioButton rb1, rb2;
private JComboBox cb1, cb2;
private JCheckBox jcb1, jcb2, jcb3, jcb4;
private JButton jb1, jb2;
private JTextArea ta, ta1;
private ButtonGroup mybg = new ButtonGroup();
public Resume() {
super();
init();
}
private void init() {
this.setSize(400, 800);
this.setContentPane(getJContentPane());
this.setTitle("Resume");
this.addWindowListener(new java.awt.event.WindowAdapter() {
public void windowClosing(java.awt.event.WindowEvent e) {
System.exit(0);
}
});
this.setVisible(true);
}
private JPanel getJContentPane() {
if (jp == null) {
l8 = new JLabel();
l8.setBounds(new Rectangle(20, 276, 65, 25));
l8.setText("自我评价:");
l7 = new JLabel();
l7.setBounds(new Rectangle(200, 66, 100, 25));
l7.setText("生日:");
l6 = new JLabel();
l6.setBounds(new Rectangle(20, 234, 65, 25));
l6.setText("爱好:");
l5 = new JLabel();
l5.setBounds(new Rectangle(20, 192, 65, 25));
l5.setText("毕业院校:");
l4 = new JLabel();
l4.setBounds(new Rectangle(20, 150, 65, 25));
l4.setText("文化程度:");
l3 = new JLabel();
l3.setBounds(new Rectangle(20, 108, 65, 25));
l3.setText("专业:");
l2 = new JLabel();
l2.setBounds(new Rectangle(20, 66, 65, 25));
l2.setText("性别:");
l1 = new JLabel();
l1.setBounds(new Rectangle(20, 24, 65, 25));
l1.setText("姓名:");
jp = new JPanel();
jp.setLayout(null);
jp.add(l1);
jp.add(l2);
jp.add(l3);
jp.add(l4);
jp.add(l5);
jp.add(l6);
jp.add(l7);
jp.add(l8);
jp.add(getJTextField());
jp.add(getJTextField1());
jp.add(getJTextField2());
jp.add(getJRadioButton());
jp.add(getJRadioButton1());
mybg.add(rb1);
mybg.add(rb2);
jp.add(getJComboBox());
jp.add(getJComboBox1());
jp.add(getJCheckBox());
jp.add(getJCheckBox1());
jp.add(getJCheckBox2());
jp.add(getJCheckBox3());
jp.add(getJButton());
jp.add(getJButton1());
JScrollPane sp = new JScrollPane(getJTextArea());
sp.setBounds(new Rectangle(20, 500, 350, 200));
jp.add(sp);
jp.add(getJTextArea1());
jp.setBackground(Color.CYAN);
}
return jp;
}
private JTextField getJTextField() {
if (t == null) {
t = new JTextField();
t.setBounds(new Rectangle(61, 24, 180, 25));
}
return t;
}
private JTextField getJTextField1() {
if (t1 == null) {
t1 = new JTextField();
t1.setBounds(new Rectangle(80, 192, 300, 25));
}
return t1;
}
private JTextField getJTextField2() {
if (t2 == null) {
t2 = new JTextField();
t2.setBounds(new Rectangle(235, 66, 100, 25));
}
return t2;
}
private JRadioButton getJRadioButton() {
if (rb1 == null) {
rb1 = new JRadioButton();
rb1.setBounds(new Rectangle(61, 62, 38, 26));
rb1.setText("男");
}
return rb1;
}
private JRadioButton getJRadioButton1() {
if (rb2 == null) {
rb2 = new JRadioButton();
rb2.setBounds(new Rectangle(120, 62, 38, 26));
rb2.setText("女");
}
return rb2;
}
private JComboBox getJComboBox() {
if (cb1 == null) {
String[] strcb = { "计算机", "机电", "经管" };
cb1 = new JComboBox(strcb);
cb1.setBounds(new Rectangle(62, 108, 93, 18));
}
return cb1;
}
private JComboBox getJComboBox1() {
if (cb2 == null) {
String[] strcb2 = { "大专", "本科", "硕士", "博士" };
cb2 = new JComboBox(strcb2);
cb2.setBounds(new Rectangle(92, 150, 125, 18));
}
return cb2;
}
private JCheckBox getJCheckBox() {
if (jcb1 == null) {
jcb1 = new JCheckBox();
jcb1.setBounds(new Rectangle(60, 234, 58, 17));
jcb1.setText("跳舞");
}
return jcb1;
}
private JCheckBox getJCheckBox1() {
if (jcb2 == null) {
jcb2 = new JCheckBox();
jcb2.setBounds(new Rectangle(120, 234, 58, 17));
jcb2.setText("唱歌");
}
return jcb2;
}
private JCheckBox getJCheckBox2() {
if (jcb3 == null) {
jcb3 = new JCheckBox();
jcb3.setBounds(new Rectangle(180, 234, 68, 17));
jcb3.setText("打篮球");
}
return jcb3;
}
private JCheckBox getJCheckBox3() {
if (jcb4 == null) {
jcb4 = new JCheckBox();
jcb4.setBounds(new Rectangle(245, 234, 58, 17));
jcb4.setText("看书");
}
return jcb4;
}
private JButton getJButton() {
if (jb1 == null) {
jb1 = new JButton();
jb1.setBounds(new Rectangle(100, 425, 60, 28));
jb1.setText("提交");
jb1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent e) {
String strname = t.getText();
String stryx = t1.getText();
String strsr = t2.getText();
String strsex = "男";
if (jb2.isSelected()) {
strsex = "女";
}
String strhb1 = "";
if (jcb1.isSelected()) {
strhb1 = "跳舞";
}
String strhb2 = "";
if (jcb2.isSelected()) {
strhb2 = "唱歌";
}
String strhb3 = "";
if (jcb3.isSelected()) {
strhb3 = "打篮球";
}
String strhb4 = "";
if (jcb4.isSelected()) {
strhb4 = "看书";
}
String strzy = cb1.getSelectedItem().toString();
String strwh = cb2.getSelectedItem().toString();
String strzp = ta1.getText();
ta.setText("姓名:" + strname + "\r\n" + "性别:" + strsex + "\r\n" + "生日:" + strsr + "\r\n" + "专业:"
+ strzy + "\r\n" + "文化:" + strwh + "\r\n" + "毕业院校:" + stryx + "\r\n" + "爱好:" + strhb1 + " "
+ strhb2 + " " + strhb3 + " " + strhb4+ "\r\n" + "自我评价:" + strzp );
}
});
}
return jb1;
}
private JButton getJButton1() {
if (jb2 == null) {
jb2 = new JButton();
jb2.setBounds(new Rectangle(200, 425, 60, 28));
jb2.setText("取消");
jb2.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent e) {
System.exit(0);
}
});
}
return jb2;
}
private JTextArea getJTextArea() {
if (ta == null) {
ta = new JTextArea();
}
return ta;
}
private JTextArea getJTextArea1() {
if (ta1 == null) {
ta1 = new JTextArea();
ta1.setBounds(new Rectangle(20, 300, 350, 100));
}
return ta1;
}
public static void main(String args[]) {
new Resume();
}
}