合作伙伴和我正在制作一个由三个GUI框架组成的程序(是的,用于家庭作业);第一个是带有JButtons的简单菜单,第二个是注册屏幕,为用户提供选项(例如Passport Type,Passport Number等),以便用Jbutton填充" save"信息到.txt文件,第三个是"加载"通过在GUI框架的文本区域内打印其内容来说.txt文件。
我们的问题是,我们似乎无法从.txt文件中获取要保存和加载的信息。
我们非常确定文件没有被创建,因此,没有任何内容被写入。但是,当它运行时我们没有收到任何错误,所以我们在错误的地方感到困惑。如果有人可以看一下并给我们一些指示,那将会有很大的帮助。
代码如下:
public class test
{
public static void main(String[] args) throws IOException
{
// File file = new File("enroll.txt"); used to create the file
final JFrame mainMenu = new JFrame("Error");
final int WINDOW_WIDTH = 600;
final int WINDOW_HEIGHT = 400;
FlowLayout layout = new FlowLayout();
mainMenu.setSize(WINDOW_WIDTH, WINDOW_HEIGHT);
mainMenu.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
mainMenu.setLocationRelativeTo(null);
mainMenu.setLayout(layout);
Button load = new Button("Load Enrollment");
Button enroll = new Button ("New Enrollment");
mainMenu.add(load);
mainMenu.add(enroll);
mainMenu.setVisible(true);
final JFrame loadEn = new JFrame("Load");
loadEn.setVisible(false);
loadEn.setSize(WINDOW_WIDTH, WINDOW_HEIGHT);
loadEn.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
loadEn.setLocationRelativeTo(null);
loadEn.setLayout(layout);
JLabel file = new JLabel("File:");
file.setVisible(true);
final JTextField fileEnter = new JTextField(10);
fileEnter.setEditable(true);
fileEnter.setVisible(true);
Button run = new Button("Run");
Button menu = new Button("Main Menu");
final JTextArea print = new JTextArea(500, 300);
loadEn.add(file);
loadEn.add(fileEnter);
loadEn.add(run);
loadEn.add(menu);
loadEn.add(print);
final JFrame newEnroll = new JFrame("New Enrollment");
newEnroll.setVisible(false);
newEnroll.setSize(WINDOW_WIDTH, WINDOW_HEIGHT);
newEnroll.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
newEnroll.setLocationRelativeTo(null);
newEnroll.setLayout(layout);
JLabel passType = new JLabel("Passport Type");
JLabel passNo = new JLabel("Passport Number");
JLabel firstName = new JLabel("First Name");
JLabel lastName = new JLabel("Last Name");
JLabel country = new JLabel("Country");
JLabel placeOB = new JLabel("Place of Birth");
JLabel visaType = new JLabel("Visa Type");
JLabel visaNo = new JLabel("Visa Number");
JLabel remarks = new JLabel("Remarks");
Button save = new Button("Save");
final JTextField _passNo = new JTextField(10);
final JTextField _firstName = new JTextField(10);
final JTextField _lastName = new JTextField(10);
final JTextField _placeOB = new JTextField(10);
final JTextField _visaNo = new JTextField(10);
final JTextField _remarks = new JTextField(10);
final JTextField _passType = new JTextField(10);
final JTextField _country = new JTextField(10);
final JTextField _visaType = new JTextField(10);
newEnroll.add(passType);
newEnroll.add(_passType);
newEnroll.add(visaType);
newEnroll.add(_visaType);
newEnroll.add(passNo);
newEnroll.add(_passNo);
newEnroll.add(visaNo);
newEnroll.add(_visaNo);
newEnroll.add(firstName);
newEnroll.add(_firstName);
newEnroll.add(remarks);
newEnroll.add(_remarks);
newEnroll.add(lastName);
newEnroll.add(_lastName);
newEnroll.add(country);
newEnroll.add(_country);
newEnroll.add(placeOB);
newEnroll.add(_placeOB);
newEnroll.add(menu);
newEnroll.add(save);
load.addActionListener(new ActionListener ()
{
@Override
public void actionPerformed(ActionEvent e)
{
loadEn.setVisible(true);
mainMenu.setVisible(false);
newEnroll.setVisible(false);
} });
menu.addActionListener(new ActionListener ()
{
@Override
public void actionPerformed(ActionEvent e1)
{
loadEn.setVisible(false);
mainMenu.setVisible(true);
newEnroll.setVisible(false);
} });
enroll.addActionListener(new ActionListener ()
{
@Override
public void actionPerformed(ActionEvent e2)
{
loadEn.setVisible(false);
mainMenu.setVisible(false);
newEnroll.setVisible(true);
} });
save.addActionListener(new ActionListener()
{
@Override
public void actionPerformed(ActionEvent e4)
{
PrintWriter fw = null ;
try
{
fw = new PrintWriter("enroll.txt", "UTF-8");
BufferedWriter bw = new BufferedWriter(fw);
bw.write(_passNo.getText());
bw.newLine();
bw.write(_firstName.getText());
bw.newLine();
bw.write(_lastName.getText());
bw.newLine();
bw.write(_placeOB.getText());
bw.newLine();
bw.write(_visaNo.getText());
bw.newLine();
bw.write(_remarks.getText());
bw.newLine();
bw.write(_passType.getText());
bw.newLine();
bw.write(_country.getText());
bw.newLine();
bw.write(_visaType.getText());
} catch (IOException e1)
{
e1.printStackTrace();
}
finally
{
fw.close();
}
}
});
run.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e3)
{
FileReader fr = null;
try
{
fr = new FileReader("enroll.txt");
} catch (FileNotFoundException e2)
{
e2.printStackTrace();
}
try
{
fr = new FileReader("enroll.txt");
} catch (FileNotFoundException e1)
{
e1.printStackTrace();
}
BufferedReader br = new BufferedReader(fr);
try
{
print.read(br, null);
} catch (IOException e1)
{
e1.printStackTrace();
}
catch(Exception e)
{
System.out.println(e);
} finally
{
try
{
br.close();
} catch (IOException e)
{
e.printStackTrace();
}
try
{
fr.close();
} catch (IOException e)
{
e.printStackTrace();
}
}
}
});
}
}
答案 0 :(得分:0)
进行两项更改
首先 - 写作
在save.addActionListener
中,您使用fw.close();
关闭Printwriter。您的BufferedWriter bw = new BufferedWriter(fw);
使用了Printerwriter。
您必须致电bw.close()
而不是fw.close()
二读 -
您的布局选择并不是最好的。你的JTextArea很大。您的文件已加载,但您无法看到它。如果您使用final JTextArea print = new JTextArea(30, 30);
代替final JTextArea print = new JTextArea(500, 500);
,则每件事都可以。请记住:JTextArea中的参数是行和列 - 而不是像素。
希望有所帮助