还是以前的那个代码,不过是事件响应的问题。
import java.awt.Checkbox;import java.awt.CheckboxGroup;
import java.awt.Color;
import java.awt.Container;
import java.awt.TextArea;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JRadioButton;
import javax.swing.JTextField;
public class Login1 extends JFrame implements ActionListener
{
String strKey;
boolean flag=true;
JLabel lYourInform,lLogin, lKey;
JTextField textLogin;
JPasswordField textKey;
Checkbox box1,box2;
CheckboxGroup boxgroup;
JRadioButton r1,r2;
JLabel lSex;
JTextField textName;
JLabel lBirthday;
JComboBox y,m;
// JLabel aihao;
TextArea areaInform;
JButton bEnter;
String year[] = { "1990","1991","1992","1993","1994","1995","1996","1997","1998","1999" };
String month[] = { "1","2","3","4","5","6","7","8","9","10","11","12" };
public Login1()
{
Container c= getContentPane();
c.setLayout(new BoxLayout(c,BoxLayout.Y_AXIS));
JPanel panel1 = new JPanel(); //panel1
lLogin = new JLabel("账号:");
lLogin.setForeground(Color.red);
textLogin = new JTextField(8);
panel1.add(lLogin);
panel1.add(textLogin);
c.add(panel1);
JPanel panel2 = new JPanel(); //panel2
lKey = new JLabel("密码:");
lKey.setForeground(Color.red);
textKey = new JPasswordField(8);
panel2.add(lKey);
textKey.setEchoChar('*');
panel2.add(textKey);
c.add(panel2);
JPanel panel3 = new JPanel(); //panel3
lSex = new JLabel("性别:");
lSex.setForeground(Color.green);
textName = new JTextField("",3);
r1 = new JRadioButton( "男" ,true);
r2 = new JRadioButton( "女" , false);
boxgroup = new CheckboxGroup();
box1 = new Checkbox("男",boxgroup,true);
box2 = new Checkbox("女",boxgroup,false);
panel3.add(lSex);
panel3.add(r1);
panel3.add(r2);
c.add(panel3);
JPanel panel4 = new JPanel(); //panel4
lBirthday = new JLabel("出生日期:");
lBirthday.setForeground(Color.black);
y = new JComboBox(year);
// y.setMaximumRowCount( 4 );
y.setForeground(Color.orange);
m = new JComboBox(month);
m.setForeground(Color.blue);
// y.addItemListener(this);
// m.addItemListener(this);
panel4.add(lBirthday);
panel4.add(y);
panel4.add(m);
c.add(panel4);
JPanel bPanel = new JPanel(); //按钮面板
bEnter = new JButton("确定");
bEnter.addActionListener(this);
bPanel.add(bEnter);
c.add(bPanel); //按钮面板结束
areaInform = new TextArea("",6,25); //文本框
add(areaInform);
setSize(400,300);
setLocation(450,200);
setVisible(true);
setBackground(Color.darkGray);
addWindowListener(new WindowAdapter()
{
public void windowClosing( WindowEvent e )
{
System.exit( 0 );
}
} );
}
public void actionPerformed(ActionEvent a)
{
areaInform.setText("");
String e;
if(r1.isSelected())
e = r1.getText();
else
e = r2.getText();
if(a.getSource()==bEnter)
{
if(flag==true)
{
flag = false;
if(textLogin.getText().isEmpty())
JOptionPane.showMessageDialog(null, "账号不能为空");
else if(textKey.getText().isEmpty())
JOptionPane.showMessageDialog(null, "密码不能为空");
else
areaInform.setText("账号为:"+textLogin.getText()+"\n"+"密码为:"+textKey.getText()+"\n"+"性别:"+e+"\n"+"出生日期:"+y.getSelectedItem()+"年\t"+m.getSelectedItem()+"月\n");
textLogin.setText(""); //初始化账号框
textKey.setText(""); //初始化密码框
}
}
}
public static void main(String[] arg)
{
Login1 swing =new Login1 ();
//swing.setBackground(Color.);
}
}
点确认后,只能响应第一次事件,如果还有的话,就不在响应了,文本框一片空白了。
@java小蚂蚁