和一位合作伙伴正在开发一个由三个GUI框架组成的程序(是的,用于家庭作业); 第一个是带有JButtons的简单菜单,第二个是注册屏幕,它为用户提供了选项(例如护照类型,护照号码等),以便用户填写Jbutton以将信息"保存"到.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();
}
}
}
});
}
}
进行两项更改
首先 - 写作
save.addActionListener
你用fw.close();
关闭你的版画家。打印机由您的BufferedWriter bw = new BufferedWriter(fw);
使用。你必须打电话给bw.close()
而不是fw.close()
第二 - 阅读
布局的选择不是最好的。你的JTextArea很大。您的文件已加载,但您看不到它。如果您使用final JTextArea print = new JTextArea(30, 30);
而不是final JTextArea print = new JTextArea(500, 500);
则一切正常。请记住:JTextArea 中的参数是行和列 - 而不是像素。
希望有帮助