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

2024-12-20 10:42:05
推荐回答(1个)
回答1:

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);
}
}

晕了
这一点点你都不会自己想啊
要多多练习啊
呵呵
已经搞好了