标签:you 确认密码 java 单选框 UI mil visible sum list
1.实现当点击“+”按钮时,标签给出点击次数。
1 package 点击;
2
3 import java.awt.*;
4 import java.awt.event.*;
5 import javax.swing.*;
6
7 public class TestClickSum extends JFrame implements ActionListener {
8 private static final long serialVersionUID = 1L;
9 JLabel j1;
10 JPanel panel;
11 JButton button;
12 int i=0;
13
14 public TestClickSum(){
15 this.setTitle("点击窗口");
16 this.setSize(500, 500);
17
18 panel = new JPanel();
19 j1 = new JLabel("点击这里");
20 button = new JButton("+");
21 panel.setBackground(Color.WHITE);
22 //button按钮是一个事件发生者,在这里注册监听器
23 button.addActionListener(this);
24 button.setForeground(Color.GRAY);
25
26 panel.add(j1);
27 panel.add(button);
28
29 this.add(panel,BorderLayout.CENTER);
30 this.setResizable(true);//窗口大小是否可变
31 this.setVisible(true);//设置其可见性
32 }
33
34 public static void main(String[] args) {
35 new TestClickSum();
36 }
37
38
39 public void actionPerformed(ActionEvent e) {
40 if(e.getSource()==button) {
41 i++;
42 j1.setText("你已经点击了"+i+"次");
43 }
44 }
45 }
2.创建一个会员注册窗体,要求使用上JCheckBox,JRadioButton,JTextFiled,JTextArea等组件,并将用户的注册信息显示到文本区中
1 package 会员注册;
2
3 import java.awt.Color;
4 import java.awt.event.ActionEvent;
5 import java.awt.event.ActionListener;
6
7 import javax.swing.ButtonGroup;
8 import javax.swing.JButton;
9 import javax.swing.JCheckBox;
10 import javax.swing.JFrame;
11 import javax.swing.JLabel;
12 import javax.swing.JPanel;
13 import javax.swing.JRadioButton;
14 import javax.swing.JTextField;
15 import javax.swing.JTextArea;
16
17 public class TestJLableAndJTextField extends JFrame implements ActionListener {
18 private static final long serialVersionUID = 1L;
19 //创建JPanel对象
20 private JPanel jp;
21 //建立所有标签
22 private JLabel jlabel1=new JLabel("注册账号:");
23 private JLabel jlabel2=new JLabel("输入密码:");
24 private JLabel jlabel3=new JLabel("确认密码:");
25 private JLabel jlabel4;
26 private JLabel jlabel5;
27 private JLabel jlabel6;
28 //建立所有文本框
29 private JTextField jtf1;
30 private JTextField jtf2;
31 private JTextField jtf3;
32 //建立做最终文本区域框
33 private JTextArea jtr;
34 //建立复选框
35 private JCheckBox jcb1;
36 private JCheckBox jcb2;
37 private JCheckBox jcb3;
38 //建立单选框并将两个按钮放置于一个按钮group中
39 private ButtonGroup group;
40 private JRadioButton jrb1;
41 private JRadioButton jrb2;
42 //建立 确认 和 取消按钮
43 private JButton button1;
44 private JButton button2;
45
46
47 public TestJLableAndJTextField(){
48 this.setTitle("注册页面");
49 this.setBounds(100, 100, 700, 300);
50 //单击窗口的关闭按钮时程序执行的操作
51 this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
52
53 jp = new JPanel();
54 jtf1 = new JTextField(50);
55 jtf2 = new JTextField(50);
56 jtf3 = new JTextField(50);
57 //添加三行标签注册账号,输入密码,确认密码
58 jp.setBackground(Color.GRAY);
59 jp.add(jlabel1);
60 jp.add(jtf1);
61 jp.add(jlabel2);
62 jp.add(jtf2);
63 jp.add(jlabel3);
64 jp.add(jtf3);
65 //设置复选框
66 jlabel4 = new JLabel("爱好选择");
67 jp.add(jlabel4);
68 jcb1 = new JCheckBox("音乐");
69 jcb2 = new JCheckBox("旅游");
70 jcb3 = new JCheckBox("读书");
71 jp.add(jcb1);
72 jcb1.addActionListener(this);
73 jp.add(jcb2);
74 jcb2.addActionListener(this);
75 jp.add(jcb3);
76 jcb3.addActionListener(this);
77 //设置单选框
78 jlabel5 = new JLabel("性别");
79 jp.add(jlabel5);
80 group = new ButtonGroup();
81 jrb1 = new JRadioButton("男");
82 jrb2 = new JRadioButton("女");
83 group.add(jrb1);
84 group.add(jrb2);
85 jrb1.addActionListener(this);
86 jrb2.addActionListener(this);
87 jp.add(jrb1);
88 jp.add(jrb2);
89 //设置显示文本框
90 jlabel6 = new JLabel("显示信息");
91 jp.add(jlabel6);
92 jtr = new JTextArea(5,10);
93 jp.add(jtr);
94 //设置按钮,确定和取消
95 button1 = new JButton("确认");
96 button2 = new JButton("取消");
97 jp.add(button1);
98 button1.addActionListener(this);
99 jp.add(button2);
100 button2.addActionListener(this);
101
102 this.add(jp);
103 this.setVisible(true);
104 }
105
106
107
108
109
110 public static void main(String[] args) {
111 new TestJLableAndJTextField();
112 }
113
114
115
116
117
118 public void actionPerformed(ActionEvent e) {
119 if(e.getSource()==button2) {
120 //若点击了 取消 按钮时,清空所有选项
121 group.clearSelection();
122 jcb1.setSelected(false);
123 jcb2.setSelected(false);
124 jcb3.setSelected(false);
125 jtf1.setText(" ");
126 jtf2.setText(" ");
127 jtf3.setText(" ");
128 jtr.setText(" ");
129 }else {
130 StringBuffer temp = new StringBuffer("您的账号是:");
131 temp.append(jtf1.getText());
132 //temp.append(jtf2.getText());
133 //temp.append(jtf3.getText());
134 String text2=jtf2.getText();
135 String text3=jtf3.getText();
136 if(text2.equals(text3)/*jtf2.getText()==jtf3.getText()*/) {
137 temp.append("\r\n"+"您的密码是:"+jtf2.getText());
138 }else{
139 temp.append("\r\n"+"两次输入密码不一致");
140 jtr.setText(temp.append("。").toString());
141 return;
142 }
143 if(jcb1.isSelected()){
144 temp.append("\r\n"+"您的爱好有:"+jcb1.getText());
145 }
146 if(jcb2.isSelected()){
147 temp.append("\r\n"+"您的爱好有:"+jcb2.getText());
148 }
149 if(jcb3.isSelected()){
150 temp.append("\r\n"+"您的爱好有:"+jcb3.getText());
151 }
152 if(jrb1.isSelected()){
153 temp.append("\r\n"+"您的性别是:"+jrb1.getText());
154 }
155 if(jrb2.isSelected()){
156 temp.append("\r\n"+"您的性别是:"+jrb2.getText());
157 }
158 jtr.setText(temp.append("。").toString());
159 }
160 }
161 }
标签:you 确认密码 java 单选框 UI mil visible sum list
原文地址:https://www.cnblogs.com/TimhLiu/p/9245832.html