我的目的是这样的,当我输入用户信息之后,点"确定",就生成一个文本文件,然后我再从文本文件里面把数据给读出来,显示在另一个用户界面上,我现在做的这个源程序,当我运行的时候,两个用户界面都运行出来了.我是个菜鸟,哪位大虾给指点指点哈.先谢谢噢!!!
我的源程序如下:
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.io.*;
public class lab3 implements ActionListener,ItemListener
{
JFrame f=null;
JFrame f1=null;
JTextField name,num,age;
JRadioButton r1,r2;
int op=0;
String sex1;
JLabel L1,L2,L3,L4,L5,L6,L7,L8;
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.CENTER);
JPanel p2 = new JPanel();
p2.setLayout(new GridLayout(1, 2));
JButton b1 = new JButton("确定");
JButton b2 = new JButton("取消");
p2.add(b1);
p2.add(b2);
b1.addActionListener(this);
b2.addActionListener(this);
contentPane.add(p2, BorderLayout.SOUTH);
f.setSize(250, 200);
//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();
//int number=Integer.parseInt(num.getText());
//int age1=Integer.parseInt(age.getText());
String number = num.getText();
String age1 = age.getText();
switch (op) {
case 1:
sex1 = "男";
break;
case 2:
sex1 = "女";
break;
}
//写文件
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(sex1);
out.newLine();
out.write(age1);
//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("取消")) System.exit(0);
}
}
//public static void main(String args[])
// {
// new lab3();
// }
}
class lab4 extends lab3//implements ActionListener//,ItemListener
{
JFrame f1=null;
//JTextField name,num,age;
JLabel L1,L2,L3,L4,L5,L6,L7,L8;
public lab4()
{
f1 = new JFrame("学生信息输出表");
Container contentPane = f1.getContentPane();
JPanel p1 = new JPanel();
p1.setLayout(new GridLayout(4, 2));
L1 = new JLabel("姓名:");
L5 = new JLabel("",SwingConstants.LEFT);
L2 = new JLabel("学号:");
L6 = new JLabel("",SwingConstants.LEFT);
L4 = new JLabel("年龄:");
L7 = new JLabel("",SwingConstants.LEFT);
L3 = new JLabel("性别:");
L8 = new JLabel("",SwingConstants.LEFT);
p1.add(L1);
p1.add(L5);
p1.add(L2);
p1.add(L6);
p1.add(L4);
p1.add(L7);
p1.add(L3);
p1.add(L8);
contentPane.add(p1, BorderLayout.NORTH);
L5.setText("" + name1);
L6.setText("" + number);
L7.setText("" + age1);
L8.setText("" + sex1);
/*JPanel p3 = new JPanel();
p3.setLayout(new GridLayout(1, 2));
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.CENTER);*/
JPanel p2 = new JPanel();
p2.setLayout(new GridLayout(1, 1));
JButton b1 = new JButton("确定");
//JButton b2 = new JButton("取消");
p2.add(b1);
//p2.add(b2);
b1.addActionListener(this);
//b2.addActionListener(this);
contentPane.add(p2, BorderLayout.SOUTH);
f1.setSize(250, 200);
//f.pack();//对组件进行排列
f1.show();//显示
f1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public void actionPerformed(ActionEvent e){ //确定,取消按钮被单击时触发
String cmd = e.getActionCommand();
if (cmd.equals("确定"))
System.exit(0);
else System.out.println("操作错误!");
}
public static void main(String args[])
{
new lab4();
}
}