源程序如下:
我的目的是要从我创建的文本文件里面获取值,然后输出到界面,但是我现在出现一个这个问题,就是我能够在界面上输出,在文本文件里不能输出性别里的"女"字,这个是怎么回事啊,各位帮帮忙呢.先谢谢哈!!!
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.io.*;
public class lab3 implements ActionListener,ItemListener
{
JFrame f=null;
JTextField name,num,age;
JRadioButton r1,r2;
int op=0;
String sex1;
JLabel L1,L2,L3,L4,L5,L6,L7,L8,L9,L10,L11,L12,L13,L14;
String name1,number,age1;
public lab3(){
f = new JFrame("学生信息输入表");
Container contentPane = f.getContentPane();
JPanel p1 = new JPanel();
p1.setLayout(new GridLayout(3, 2));
L1 = new JLabel("姓名:");
name = new JTextField();
L2 = new JLabel("学号:");
num = new JTextField();
L4 = new JLabel("年龄:");
age = new JTextField();
p1.add(L1);
p1.add(name);
p1.add(L2);
p1.add(num);
p1.add(L4);
p1.add(age);
contentPane.add(p1, BorderLayout.NORTH);
JPanel p3 = new JPanel();
p3.setLayout(new GridLayout(1, 2));
L3 = new JLabel("性别:");
r1=new JRadioButton("男");
r2=new JRadioButton("女");
p3.add(L3);
p3.add(r1);
p3.add(r2);
ButtonGroup bg=new ButtonGroup();
bg.add(r1);
bg.add(r2);
r1.addItemListener(this);
r2.addItemListener(this);
contentPane.add(p3, BorderLayout.WEST);
JPanel p4 = new JPanel();
p4.setLayout(new GridLayout(3, 2));
L9 = new JLabel("");
L10 = new JLabel("");
L11 = new JLabel("");
L12 = new JLabel("");
L13 = new JLabel("");
L14 = new JLabel("");
p4.add(L9);
p4.add(L10);
p4.add(L11);
p4.add(L12);
p4.add(L13);
p4.add(L14);
contentPane.add(p4, BorderLayout.EAST);
JPanel p2 = new JPanel();
p2.setLayout(new GridLayout(1, 2));
JButton b1 = new JButton("确定");
JButton b2 = new JButton("重写");
JButton b3 = new JButton("关闭");
p2.add(b1);
p2.add(b2);
p2.add(b3);
b1.addActionListener(this);
b2.addActionListener(this);
b3.addActionListener(this);
contentPane.add(p2, BorderLayout.SOUTH);
f.setSize(340, 300);
//f.pack();//对组件进行排列
f.show();//显示
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public void itemStateChanged(ItemEvent e){//单选按钮被单击时触发
if(e.getSource()==r1)op=1;
if(e.getSource()==r2)op=2;
}
public void actionPerformed(ActionEvent e){//确定,取消按钮被单击时触发
String cmd = e.getActionCommand();
if (cmd.equals("确定"))
{
String name1 = name.getText();
if( name.getText()==null)name1 =null;
String number = num.getText();
if( num.getText()==null)number =null;
String age1 = age.getText();
if( age.getText()==null)age1 =null;
switch (op) {
case 1:
{sex1 = "男";
break;}
case 2:
{sex1 = "女";
break;}
}
L9.setText("你输入的信息为:");
L11.setText("姓名: "+name1);
L12.setText("学号: "+number);
L13.setText("年龄: "+age1);
L14.setText("性别: "+sex1);
//写文件
String filename = "F:\\students.txt";
try {
BufferedWriter out = new BufferedWriter(new FileWriter(filename));
out.write(name1);
out.newLine();
out.write(number);
out.newLine();
out.write(age1);
out.newLine();
out.write(sex1);
//out.newLine();
out.close();
}
catch (IOException iox) {
System.out.println("写文件有错误!" + filename);
}
/* //读文件
String filename2 ="F:\\students.txt";
String line;
try{
System.out.println("你输入的信息如下:");
BufferedReader in =new BufferedReader(new FileReader(filename2));
line=in.readLine();
while (line !=null)
{
System.out.println(line);
line=in.readLine();
}
in.close();
}
catch (IOException iox)
{
System.out.println("读文件有错误!"+filename2);
}
f.setVisible(false);
return;*/
}
if (cmd.equals("重写"))
{
name.setText("");
num.setText("");
age.setText("");
}
if (cmd.equals("关闭")) System.exit(0);
}
public static void main(String args[])
{
new lab3();
}
}