使用java和NetBeans GUI写入和读取ArrayList
我试图为一个大学项目编写代码,在这个项目中,我使用NetBeans创建了一个应用程序,其中我添加了关于莱德杯球员的信息。我的GUI上有所有变量(团队、姓名、出生日期、家庭、大学、教授、年份、比赛、冠军、队长、pga、冠军和实习生)的文本字段,当我让GUI正确保存和读取时,我将添加关于我所有球员的所有信息,然后我将删除文本字段和保存按钮 我希望整个页面能够做到的是,当用户单击美国队或欧洲队的单选按钮时,所有关于该队球员的信息都会出现。我想用标签来显示每个球员的信息 所以现在,我被困在保存和读取部分。当我为第一个玩家添加信息并保存它时,它就起作用了。但是如果我添加其他玩家信息,它将只读取第一个玩家信息。我不知道如何将它保存到下一个数组列表,而不是第一个数组列表对象的顶部 我希望我已经把我要做的事情说清楚了。我将非常感谢任何帮助,谢谢使用java和NetBeans GUI写入和读取ArrayList,java,user-interface,netbeans,fileoutputstream,objectoutputstream,Java,User Interface,Netbeans,Fileoutputstream,Objectoutputstream,我试图为一个大学项目编写代码,在这个项目中,我使用NetBeans创建了一个应用程序,其中我添加了关于莱德杯球员的信息。我的GUI上有所有变量(团队、姓名、出生日期、家庭、大学、教授、年份、比赛、冠军、队长、pga、冠军和实习生)的文本字段,当我让GUI正确保存和读取时,我将添加关于我所有球员的所有信息,然后我将删除文本字段和保存按钮 我希望整个页面能够做到的是,当用户单击美国队或欧洲队的单选按钮时,所有关于该队球员的信息都会出现。我想用标签来显示每个球员的信息 所以现在,我被困在保存和读取部分
//my instantiable class code
package rydercup;
import java.io.Serializable;
public class players implements Serializable {
private String team;
private String name;
private String dob;//date of birth and age
private String home;//hometown
private String college;
private String prof;//turned professional
private String years;//years played
private String matches;//total matches
private String won;//total points won
private String captain;//team usa captain years
private String pga;//PGA tour
private String champion;//champions tour victories
private String intern;//international victories
public players() {
team = new String();
name = new String();
dob = new String();
home = new String();
college = new String();
prof = new String();
years = new String();
matches = new String();
won = new String();
captain = new String();
pga = new String();
champion = new String();
intern = new String();
}
public void setTeam(String team) {
this.team = team;
}
public void setName(String name) {
this.name = name;
}
public void setDob(String dob) {
this.dob = dob;
}
public void setHome(String home) {
this.home = home;
}
public void setCollege(String college) {
this.college = college;
}
public void setProf(String prof) {
this.prof = prof;
}
public void setYears(String years) {
this.years = years;
}
public void setMatches(String matches) {
this.matches = matches;
}
public void setWon(String won) {
this.won = won;
}
public void setCaptain(String captain) {
this.captain = captain;
}
public void setPga(String pga) {
this.pga = pga;
}
public void setChampion(String champion) {
this.champion = champion;
}
public void setIntern(String intern) {
this.intern = intern;
}
public String getName() {
return name;
}
public String getDob() {
return dob;
}
public String getTeam() {
return team;
}
public String getHome() {
return home;
}
public String getCollege() {
return college;
}
public String getProf() {
return prof;
}
public String getYears() {
return years;
}
public String getMatches() {
return matches;
}
public String getWon() {
return won;
}
public String getCaptain() {
return captain;
}
public String getPga() {
return pga;
}
public String getChampion() {
return champion;
}
public String getIntern() {
return intern;
}
}
//my GUI code
package rydercup;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.ArrayList;
import javax.swing.JOptionPane;
public class RyderPlayer extends javax.swing.JFrame {
//variables
private ArrayList <players> usaList;
private String team, name, dob, home, college, prof, years, matches, won;
private String captain, pga, champion, intern;
private int count;
public RyderPlayer() {
initComponents();
usaList = new ArrayList <> ();
team = new String();
name = new String();
dob = new String();
home = new String();
college = new String();
prof = new String();
years = new String();
matches = new String();
won = new String();
captain = new String();
pga = new String();
champion = new String();
intern = new String();
//initialise count to 0
count = 0;
}
//generate code ....
private void saveBtnActionPerformed(java.awt.event.ActionEvent evt) {
File outFile;
FileOutputStream fStream;
ObjectOutputStream oStream;
try{
outFile = new File("usa.data");
fStream = new FileOutputStream(outFile);
oStream = new ObjectOutputStream(fStream);
oStream.writeObject(usaList);
JOptionPane.showMessageDialog(null,"File written successfully");
oStream.close();
}
catch(IOException e){
System.out.println(e);
}
}
private void readBtnActionPerformed(java.awt.event.ActionEvent evt) {
File inFile;
FileInputStream fStream;
ObjectInputStream oStream;
try{
inFile = new File("usa.data");
fStream = new FileInputStream(inFile);
oStream = new ObjectInputStream(fStream);
ArrayList <players> xList;
xList = (ArrayList<players>)oStream.readObject();
for(players x:xList){
JOptionPane.showMessageDialog(null, "Team: "+x.getTeam() +
"\n Name: "+x.getName() +
"\n Date of birth: "+x.getDob() +
"\n Home: "+x.getHome() +
"\n College: "+x.getCollege() +
"\n Turned Professional:"+x.getProf() +
"\n Years played: "+x.getYears() +
"\n Total matches: "+x.getMatches() +
"\n Total points won: "+x.getWon() +
"\n USA captain years: "+x.getCaptain() +
"\n PGA tour: "+x.getPga() +
"\n Champion tour: "+x.getChampion() +
"\n International: "+x.getIntern());
}
oStream.close();
}
catch(IOException e){
System.out.println(e);
}
catch(ClassNotFoundException ex){
System.out.println(ex);
}
}
//generate code ....
}
//我的可实例化类代码
包装莱德杯;
导入java.io.Serializable;
公共类播放器实现了可序列化{
私人弦乐队;
私有字符串名称;
私有字符串dob;//出生日期和年龄
私有字符串home;//home
私立弦乐学院;
私人字符串prof;//已转为专业
私有字符串年份;//已播放的年份
私有字符串匹配;//总匹配数
私有字符串赢得;//赢得总分
私人弦乐队长;//美国队队长年
私有字符串pga;//pga-tour
私人弦乐冠军;//冠军巡回赛胜利
私人字符串实习生;//国际胜利
公共玩家(){
team=新字符串();
name=新字符串();
dob=新字符串();
home=新字符串();
college=新字符串();
prof=新字符串();
年份=新字符串();
匹配=新字符串();
won=新字符串();
船长=新字符串();
pga=新字符串();
champion=新字符串();
intern=新字符串();
}
公开组(字符串组){
这个团队=团队;
}
公共void集合名(字符串名){
this.name=名称;
}
公共void setDob(字符串dob){
this.dob=dob;
}
公共void setHome(字符串home){
this.home=家;
}
公立学院(弦学院){
这个学院=学院;
}
公共void setProf(字符串prof){
这个prof=prof;
}
公共年数(字符串年){
本年=年;
}
公共void集合匹配(字符串匹配){
this.matches=匹配项;
}
公共无效设置元(字符串元){
this.won=won;
}
公共空间队长(弦长){
这个。船长=船长;
}
公共无效设置pga(字符串pga){
这是pga=pga;
}
公开组冠军(字符串冠军){
这个。冠军=冠军;
}
公共void setIntern(字符串intern){
this.intern=实习生;
}
公共字符串getName(){
返回名称;
}
公共字符串getDob(){
返回dob;
}
公共字符串getTeam(){
返回队;
}
公共字符串getHome(){
回家;
}
公共学院(){
返回学院;
}
公共字符串getProf(){
返回教授;
}
公共字符串getYears(){
回归年;
}
公共字符串getMatches(){
返回比赛;
}
公共字符串getWon(){
赢回;
}
公共字符串getCaptain(){
返回船长;
}
公共字符串getPga(){
返回pga;
}
公共字符串getChampion(){
返回冠军;
}
公共字符串getIntern(){
返回实习生;
}
}
//我的GUI代码
包装莱德杯;
导入java.io.File;
导入java.io.FileInputStream;
导入java.io.FileOutputStream;
导入java.io.IOException;
导入java.io.ObjectInputStream;
导入java.io.ObjectOutputStream;
导入java.util.ArrayList;
导入javax.swing.JOptionPane;
公共类RyderPlayer扩展了javax.swing.JFrame{
//变数
私人ArrayList usaList;
私人弦乐队,姓名,出生日期,家庭,学院,教授,年份,比赛,获胜;
私人弦乐队长、pga、冠军、实习生;
私人整数计数;
公共RyderPlayer(){
初始化组件();
usaList=newarraylist();
team=新字符串();
name=新字符串();
dob=新字符串();
home=新字符串();
college=新字符串();
prof=新字符串();
年份=新字符串();
匹配=新字符串();
won=新字符串();
船长=新字符串();
pga=新字符串();
champion=新字符串();
intern=新字符串();
//将计数初始化为0
计数=0;
}
//生成代码。。。。
私有void saveBtnActionPerformed(java.awt.event.ActionEvent evt){
文件输出文件;
FileOutputStream流;
ObjectOutputStream;
试一试{
outFile=新文件(“usa.data”);
fStream=新文件输出流(输出文件);
oStream=新对象输出流(fStream);
oStream.writeObject(usaList);
showMessageDialog(null,“文件写入成功”);
oStream.close();
}
捕获(IOE异常){
系统输出打印ln(e);
}
}
私有void readBtnActionPerformed(java.awt.event.ActionEvent evt){
文件填充;
文件输入流;
目标输出流oStream;
试一试{
infle=新文件(“usa.data”);