gpt4 book ai didi

java - 如何正确从文件加载 JFrame?

转载 作者:行者123 更新时间:2023-12-01 15:30:36 25 4
gpt4 key购买 nike

我正在为我的计算机科学类(class)制作这个成绩保持程序,而我还没有开始工作的一件事是保存/加载 session 。正如我现在设置的那样,它会在您退出时自动保存,并在启动时自动加载(如果有文件要加载)。但是,我觉得我加载或保存它不正确;有一个主 JFrame 保存所有数据,这就是保存的一个对象。当它加载后,向您展示会更容易。

If it looks like this when I close it: :

pre-save snapshot

然后it'll look like this when I start it up again:

post-load snapshot

此外,当程序加载 JFrame 时,“Enter Student”按钮以及用于输入的 JTextField 上的 ActionListener 也会停止工作。该程序分为 3 个类文件:

成绩簿:

import javax.swing.*;
import java.awt.event.*;
import java.io.*;

public class GradeBook implements java.io.Serializable {
private static JFrame frame;
public static void main(String[] args) throws Exception{
try{
FileInputStream fis = new FileInputStream("t.tmp");
ObjectInputStream ois = new ObjectInputStream(fis);

frame = (JFrame) ois.readObject();

ois.close();
} catch(Throwable t) {
frame = new JFrame("Course Grades");
}

frame.addWindowListener(new closing());
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add(new CourseGrade());
frame.pack();
frame.setVisible(true);
}

结束:

    private static class closing extends WindowAdapter {
public void windowClosing(WindowEvent e) {
try {
FileOutputStream fos = new FileOutputStream("t.tmp");
ObjectOutputStream oos = new ObjectOutputStream(fos);

oos.writeObject(frame);

oos.close();
} catch(Throwable t){System.out.println(t.getMessage());}
}
}
}

类(class)成绩:

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.awt.FlowLayout;
import java.util.Random;
import javax.swing.table.*;
import java.io.*;
public class CourseGrade extends JPanel implements java.io.Serializable {
private JLabel entername;
private JTextField in;
private JTextArea list;
private JScrollPane scroll;
private String[] students = new String[1000];
private JButton enter;
private JButton[] studentbuttons = new JButton[1000];
private JButton[] delete=new JButton[1000];
private int numstudents;
private JFrame[] studentframes=new JFrame[1000];
private static JTable[] tables=new JTable[1000];
private static DefaultTableModel[] model=new DefaultTableModel[1000];
private static int[] numass=new int[1000];
public CourseGrade() {
String[][] data = {{"", "", "", ""}};
String[] headers = {"Assignment", "Date Assigned", "Score", "Percentage"};
for(int i=0; i<tables.length; i++){
model[i] = new DefaultTableModel(data, headers);
tables[i] = new JTable(model[i]){
public boolean isCellEditable(int rowIndex, int colIndex) {
return false;
}
};
}
numstudents=0;
in=new JTextField(13);
in.addActionListener(new enterListener());
list=new JTextArea();
scroll=new JScrollPane(list);
list.setEditable(false);
entername=new JLabel("Enter a student's name");
enter=new JButton("Enter Student");
enter.addActionListener(new enterListener());
setPreferredSize(new Dimension(260, 300));
setBackground(Color.green);

add(entername);
add(in);
add(enter);
add(scroll);

scroll.setPreferredSize(new Dimension(240, 200));
}

private class enterListener implements ActionListener {
public void actionPerformed(ActionEvent event) {
String x=in.getText();
String y="";
String z="";
in.setText("");
int a=numstudents+1;
if(x.length()>0) x=a+". " + x + "\n";
students[numstudents] = x;
if(x.length()>0)numstudents++;
for(int i=0; i<numstudents; i++){
x=students[i];
if(x.length()>13)x=x.substring(0,11)+"...\n";
y+=x;
}
studentbuttons[numstudents]=new JButton("Edit");
studentbuttons[numstudents].addActionListener(new grades());
delete[numstudents]=new JButton("Delete");
delete[numstudents].addActionListener(new del());
list.setText(y);
list.add(studentbuttons[numstudents]);
studentbuttons[numstudents].setBounds(100,(numstudents-1)*16,55,15);
list.add(delete[numstudents]);
delete[numstudents].setBounds(160,(numstudents-1)*16,70,15);
}
}

private class grades implements ActionListener {
public void actionPerformed(ActionEvent event) {
Object obj = event.getSource();
if(obj instanceof JButton){
JButton clicked = (JButton)obj;
int x=clicked.getY()/16;
String y=students[x];
for(int i=0; i<y.length(); i++){
String q=y.substring(i,i+1);
if(q.equals(" ")) {
y=y.substring(i+1);
i=y.length()+1;
}
}
studentframes[x]=new JFrame(y+"'s Grades");
studentframes[x].setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
try{studentframes[x].getContentPane().add(new GradePage(x, tables[x], numass[x]));}
catch(Exception e){}
studentframes[x].pack();
studentframes[x].setVisible(true);
}
}
}

private class del implements ActionListener {
public void actionPerformed(ActionEvent event) {
Object obj = event.getSource();
if(obj instanceof JButton){
JButton clicked = (JButton)obj;
int x=clicked.getY()/16;
String y="", z="";
studentbuttons[numstudents].setVisible(false);
delete[numstudents].setVisible(false);
numstudents--;
int q=3;
int w=0;
for(int i=x; i<=numstudents; i++){
if(i<10)q=1;
else if(i<100) q=2;
tables[i]=tables[i+1];
numass[i]=numass[i+1];
model[i]=model[i+1];
w=i+1;
try{if(!students[i+1].equals(null)){students[i]=w+students[i+1].substring(q);} else{students[i]=null;}}catch(Throwable t){}
}
for(int i=0; i<numstudents; i++){
y=students[i];
if(y.length()>13)y=y.substring(0,11)+"...\n";
z+=y;
}
list.setText(z);
}
}
}
public static void setTable(int numtable, JTable table){
tables[numtable]=table;
}
public static void newRow(int numtable){
model[numtable].addRow(new Object[]{"", "", "", ""});
}
public static void setNumEntries(int numtable, int num){
numass[numtable]=num;
}
}

第三堂课应该与此无关,所以我现在不会发布它。

我意识到代码可能写得不好,但我才高中计算机科学二年级,我们实际上还没有涵盖任何内容。这个程序甚至不应该是一个 GUI,这是我第一次听说输入或输出流,所以我真的对它们一无所知。我现在意识到让类实现 java.io.Serialized 可能是不必要的,但是当我尝试研究这个问题时,我遇到了有人在讨论某些对象如何无法保存,因为它们自然不会实现它。如果这是一些愚蠢的错误,我们深表歉意,并感谢您的宝贵时间。

最佳答案

您可以将数据存储在这样的记事本文件中

            try
{
FileWriter file = new FileWriter( "insert file name here" );
BufferedWriter buffer = new BufferedWriter( file ) ;
buffer.write( "insert file content here" ) ;
buffer.newLine();
buffer.close();
}
catch ( IOException e )
{
//Insert error message here
}

然后像这样阅读

    try
{
FileReader file = new FileReader( "insert file name here" ) ;

BufferedReader buffer = new BufferedReader( file );

String line = "" ;

while( ( line = buffer.readLine() ) != null )
{
System.out.println( line ) ;
}

buffer.close() ;
}
catch( IOException e )
{
//Insert error message here
}

希望有帮助

关于java - 如何正确从文件加载 JFrame?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9558405/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com