JAVA问题,请高手帮我看下错在哪里啊!!!谢谢了!!

完整程序如下:http://hi.baidu.com/xiazhiwei0726/blog/item/5980fafd678ffa40d6887d84.html不行啊,运... 完整程序如下:
http://hi.baidu.com/xiazhiwei0726/blog/item/5980fafd678ffa40d6887d84.html
不行啊,运行时在显示出生年月时它只显示最后一个日,年和月不会显示,还有就是那个密码和确认密码框顺序不太对.
不好意思,我还有一个小小的问题了,就是,当单击“确定”时,如果第一次和第二次输入的密码不一致的话,弹出“密码错误,请重新输入”的提示框。谢谢了,如果这个也解决了,那就完美了,我会给你加分的。
展开
 我来答
siy513
2008-05-23 · TA获得超过181个赞
知道小有建树答主
回答量:165
采纳率:0%
帮助的人:102万
展开全部
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class RegisterExample1 extends JFrame{
String information;
JLabel nameLabel,passwordLabel,pwdcheckLabel,gendarLabel,birthLabel;
JTextField nameField;
JPasswordField passwordField,pwdcheckField;
JRadioButton radio1,radio2;
ButtonGroup group;
String birthYear[]={"1975","1976","1977","1978","1979","1980","1981","1982","1983","1984","1985","1986","1987","1988","1989","1990"};
JComboBox birthBox1,birthBox2,birthBox3;
String birthMonth[]={"01","02","03","04","05","06","07","08","09","10","11","12"};
String birthDay[]={"01","02","03","04","05","06","07","08","09","10","11","12","13","14","15","16","17","18","19","20","21","22","23","24","25","26","27","28","29","30","31"};
String name="",password="",gendar="",pwdcheck="",birth="";
JButton confirmBtn,cancelBtn;
Container container;
public RegisterExample1(){
super("用户注册示例");
container=getContentPane();
container.setLayout(new FlowLayout());

nameLabel=new JLabel("用户名");
nameField=new JTextField(10);
nameField.addActionListener(new NewActionListener());
container.add(nameLabel);
container.add(nameField);

passwordLabel=new JLabel("密码");
pwdcheckLabel=new JLabel("确认密码");
passwordField=new JPasswordField(10);
pwdcheckField=new JPasswordField(10);
passwordField.addActionListener(new NewActionListener());
pwdcheckField.addActionListener(new NewActionListener());
container.add(passwordLabel);
container.add(pwdcheckLabel);
container.add(passwordField);
container.add(pwdcheckField);

gendarLabel=new JLabel("性别");
radio1=new JRadioButton("男");
radio2=new JRadioButton("女");
radio1.addItemListener(new RadioListener());
radio2.addItemListener(new RadioListener());
group=new ButtonGroup();
group.add(radio1);
group.add(radio2);
container.add(gendarLabel);
container.add(radio1);
container.add(radio2);

birthLabel=new JLabel("出生年月");
birthBox1=new JComboBox(birthYear);
birthBox2=new JComboBox(birthMonth);
birthBox3=new JComboBox(birthDay);
birthBox1.addItemListener(new ComboListener());
birthBox2.addItemListener(new ComboListener());
birthBox3.addItemListener(new ComboListener());
container.add(birthLabel);
container.add(birthBox1);
container.add(birthBox2);
container.add(birthBox3);

confirmBtn=new JButton("确定");
cancelBtn=new JButton("取消");
confirmBtn.addActionListener(new NewActionListener());
cancelBtn.addActionListener(new NewActionListener());
container.add(confirmBtn);
container.add(cancelBtn);
setSize(480,140);
setVisible(true);
}

class RadioListener implements ItemListener{
public void itemStateChanged(ItemEvent e){
Object item=e.getItemSelectable();
if(item==radio1) gendar=radio1.getLabel();
else if(item==radio2) gendar=radio2.getLabel();
}
}

class ComboListener implements ItemListener{
public void itemStateChanged(ItemEvent e){
birth=(String)e.getItem();
}
}

class NewActionListener implements ActionListener{
public void actionPerformed(ActionEvent e){
if(e.getSource()==confirmBtn){
name+=nameField.getText();
password+=passwordField.getText();
pwdcheck+=pwdcheckField.getText();
information="姓名: "+name+"\n密码: "+password+"\n性别: "+gendar+"\n出生年月: "+birth;
JOptionPane.showMessageDialog(null,information);
}
else if(e.getSource()==nameField){
name+=nameField.getText();
}
else if(e.getSource()==passwordField){
password+=passwordField.getText();
}
else if(e.getSource()==pwdcheckField){
pwdcheck+=pwdcheckField.getText();
}
else if(e.getSource()==cancelBtn){
System.exit(0);
}
}
}

public static void main(String args[]){
RegisterExample1 re=new RegisterExample1();
re.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}

改好了
首先导包不是java.swing.* 而是javax.swing.*
container应该定义为全局变量
还有一个就是birthBox2写错了

又改了
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class RegisterExample1 extends JFrame{
String information;
JLabel nameLabel,passwordLabel,pwdcheckLabel,gendarLabel,birthLabel;
JTextField nameField;
JPasswordField passwordField,pwdcheckField;
JRadioButton radio1,radio2;
ButtonGroup group;
String birthYear[]={"1975","1976","1977","1978","1979","1980","1981","1982","1983","1984","1985","1986","1987","1988","1989","1990"};
JComboBox birthBox1,birthBox2,birthBox3;
String birthMonth[]={"01","02","03","04","05","06","07","08","09","10","11","12"};
String birthDay[]={"01","02","03","04","05","06","07","08","09","10","11","12","13","14","15","16","17","18","19","20","21","22","23","24","25","26","27","28","29","30","31"};
String name="",password="",gendar="",pwdcheck="",birth="";
JButton confirmBtn,cancelBtn;
Container container;
public RegisterExample1(){
super("用户注册示例");
container=getContentPane();
container.setLayout(new FlowLayout());

nameLabel=new JLabel("用户名");
nameField=new JTextField(9);
nameField.addActionListener(new NewActionListener());
container.add(nameLabel);
container.add(nameField);

passwordLabel=new JLabel("密码");
pwdcheckLabel=new JLabel("确认密码");
passwordField=new JPasswordField(9);
pwdcheckField=new JPasswordField(9);
passwordField.addActionListener(new NewActionListener());
pwdcheckField.addActionListener(new NewActionListener());
container.add(passwordLabel);
container.add(passwordField);
container.add(pwdcheckLabel);

container.add(pwdcheckField);

gendarLabel=new JLabel("性别");
radio1=new JRadioButton("男");
radio2=new JRadioButton("女");
radio1.addItemListener(new RadioListener());
radio2.addItemListener(new RadioListener());
group=new ButtonGroup();
group.add(radio1);
group.add(radio2);
container.add(gendarLabel);
container.add(radio1);
container.add(radio2);

birthLabel=new JLabel("出生年月");
birthBox1=new JComboBox(birthYear);
birthBox2=new JComboBox(birthMonth);
birthBox3=new JComboBox(birthDay);
birthBox1.addItemListener(new ComboListener());
birthBox2.addItemListener(new ComboListener());
birthBox3.addItemListener(new ComboListener());
container.add(birthLabel);
container.add(birthBox1);
container.add(birthBox2);
container.add(birthBox3);

confirmBtn=new JButton("确定");
cancelBtn=new JButton("取消");
confirmBtn.addActionListener(new NewActionListener());
cancelBtn.addActionListener(new NewActionListener());
container.add(new JLabel(" "));//NEW一个空LABEL将BUTTON撑下来
container.add(confirmBtn);
container.add(cancelBtn);
setSize(480,140);
setVisible(true);
}

class RadioListener implements ItemListener{
public void itemStateChanged(ItemEvent e){
Object item=e.getItemSelectable();
if(item==radio1) gendar=radio1.getLabel();
else if(item==radio2) gendar=radio2.getLabel();
}
}

class ComboListener implements ItemListener{
public void itemStateChanged(ItemEvent e){

//birth=(String)e.getItem(); //e.getItem()只获取得当前改变的选项的值
}
}

class NewActionListener implements ActionListener{
public void actionPerformed(ActionEvent e){
if(e.getSource()==confirmBtn){
name+=nameField.getText();
password+=passwordField.getText();
pwdcheck+=pwdcheckField.getText();
//通过JCombobox.getSelectedItem()直接获取所选项
birth=birthBox1.getSelectedItem().toString()+"年"+birthBox2.getSelectedItem().toString()+"月"+birthBox3.getSelectedItem().toString()+ "日";

information="姓名: "+name+"\n密码: "+password+"\n性别: "+gendar+"\n出生年月: "+birth;
JOptionPane.showMessageDialog(null,information);
}
else if(e.getSource()==nameField){
name+=nameField.getText();
}
else if(e.getSource()==passwordField){
password+=passwordField.getText();
}
else if(e.getSource()==pwdcheckField){
pwdcheck+=pwdcheckField.getText();
}
else if(e.getSource()==cancelBtn){
System.exit(0);
}
}
}

public static void main(String args[]){
RegisterExample1 re=new RegisterExample1();
re.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}

流布局是对组件水平放置,直到同一条线上再也没有适合的按钮。是自动排列的
所以建议不要用
你可以去看看不用布局的
直接设置组件坐标
只是需要计算
那样就可以随意摆放了
我都是以前学JAVA的时候用到过这些
都好久没碰过了 搞了我半天
JAVA其实根本就不适合做桌面应用
主要还是做网络这块的
你现在主要是多多熟悉语法就够了

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class RegisterExample1 extends JFrame{
String information;
JLabel nameLabel,passwordLabel,pwdcheckLabel,gendarLabel,birthLabel;
JTextField nameField;
JPasswordField passwordField,pwdcheckField;
JRadioButton radio1,radio2;
ButtonGroup group;
String birthYear[]={"1975","1976","1977","1978","1979","1980","1981","1982","1983","1984","1985","1986","1987","1988","1989","1990"};
JComboBox birthBox1,birthBox2,birthBox3;
String birthMonth[]={"01","02","03","04","05","06","07","08","09","10","11","12"};
String birthDay[]={"01","02","03","04","05","06","07","08","09","10","11","12","13","14","15","16","17","18","19","20","21","22","23","24","25","26","27","28","29","30","31"};
String name="",password="",gendar="",pwdcheck="",birth="";
JButton confirmBtn,cancelBtn;
Container container;
public RegisterExample1(){
super("用户注册示例");
container=getContentPane();
container.setLayout(new FlowLayout());

nameLabel=new JLabel("用户名");
nameField=new JTextField(9);
nameField.addActionListener(new NewActionListener());
container.add(nameLabel);
container.add(nameField);

passwordLabel=new JLabel("密码");
pwdcheckLabel=new JLabel("确认密码");
passwordField=new JPasswordField(9);
pwdcheckField=new JPasswordField(9);
passwordField.addActionListener(new NewActionListener());
pwdcheckField.addActionListener(new NewActionListener());
container.add(passwordLabel);
container.add(passwordField);
container.add(pwdcheckLabel);

container.add(pwdcheckField);

gendarLabel=new JLabel("性别");
radio1=new JRadioButton("男");
radio2=new JRadioButton("女");
radio1.addItemListener(new RadioListener());
radio2.addItemListener(new RadioListener());
group=new ButtonGroup();
group.add(radio1);
group.add(radio2);
container.add(gendarLabel);
container.add(radio1);
container.add(radio2);

birthLabel=new JLabel("出生年月");
birthBox1=new JComboBox(birthYear);
birthBox2=new JComboBox(birthMonth);
birthBox3=new JComboBox(birthDay);
birthBox1.addItemListener(new ComboListener());
birthBox2.addItemListener(new ComboListener());
birthBox3.addItemListener(new ComboListener());
container.add(birthLabel);
container.add(birthBox1);
container.add(birthBox2);
container.add(birthBox3);

confirmBtn=new JButton("确定");
cancelBtn=new JButton("取消");
confirmBtn.addActionListener(new NewActionListener());
cancelBtn.addActionListener(new NewActionListener());
container.add(new JLabel(" "));//NEW一个空LABEL将BUTTON撑下来
container.add(confirmBtn);
container.add(cancelBtn);
setSize(480,140);
setVisible(true);
}

class RadioListener implements ItemListener{
public void itemStateChanged(ItemEvent e){
Object item=e.getItemSelectable();
if(item==radio1) gendar=radio1.getLabel();
else if(item==radio2) gendar=radio2.getLabel();
}
}

class ComboListener implements ItemListener{
public void itemStateChanged(ItemEvent e){

//birth=(String)e.getItem(); //e.getItem()只获取得当前改变的选项的值
}
}

class NewActionListener implements ActionListener{
public void actionPerformed(ActionEvent e){
if(e.getSource()==confirmBtn){
name=nameField.getText();
password=passwordField.getText();
pwdcheck=pwdcheckField.getText();

//检查两次输入密码是否一致
if(!password.equals(pwdcheck)){
JOptionPane.showMessageDialog(container,"你两次输入的密码不一致!");
return;
}

//通过JCombobox.getSelectedItem()直接获取所选项
birth=birthBox1.getSelectedItem().toString()+"年"+birthBox2.getSelectedItem().toString()+"月"+birthBox3.getSelectedItem().toString()+ "日";

information="姓名: "+name+"\n密码: "+password+"\n性别: "+gendar+"\n出生年月: "+birth;
JOptionPane.showMessageDialog(null,information);
}
else if(e.getSource()==nameField){
name+=nameField.getText();
}
else if(e.getSource()==passwordField){
password+=passwordField.getText();
}
else if(e.getSource()==pwdcheckField){
pwdcheck+=pwdcheckField.getText();
}
else if(e.getSource()==cancelBtn){
System.exit(0);
}
}
}

public static void main(String args[]){
RegisterExample1 re=new RegisterExample1();
re.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}

晕了
这一点点你都不会自己想啊
要多多练习啊
呵呵
已经搞好了
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式