【发布时间】:2022-01-10 08:28:45
【问题描述】:
作为一项任务,我需要制作一个 Swing GUI。在第一部分中,我创建了一个对象 (Formula1Driver) 的 ArrayList (f1Driver)。目前需要的是对所述数组列表进行排序并在 GUI 中将其显示为JTable。我已经制作了一个JButton,当按下它时它应该会排序和显示。
但是,一旦按下它只会显示表格标题而不是数组列表中的信息,我应该怎么做?我已经包含了 GUI 类和下面的主类。
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Formula1ChampionshipManager formula1ChampionshipManager =new Formula1ChampionshipManager();
F1ChampionshipGUI guiCode = new F1ChampionshipGUI();
formula1ChampionshipManager.loadPreviousData();
String menu ="";
Scanner input = new Scanner(System.in);
while (true) {
System.out.println("Enter required menu value to continue"+ //Allow user to enter the values and use the program
"\n1 or AND:Add a new driver."+
"\n2 or RMD:Remove a driver."+
"\n3 or CDT:Change driver of a team."+
"\n4 or DDS:Display the statistics of a driver."+
"\n5 or DDT:Display the Formula1 driver table."+
"\n6 or ARD:Add the details of completed race."+
"\n7 or SCD:Save the current data."+
"\n8 or LPD:Load the previous data."+
"\n9 or GUI:Access the user interface."+
"\n0 or EXT:Exit and terminate the program.");
menu = input.nextLine();
if ((menu.equals("1")) || (menu.equalsIgnoreCase("AND"))) { //calls each of the methods.
formula1ChampionshipManager.addDriver();
System.out.println("New driver has been added.");
} else if ((menu.equals("2")) || (menu.equalsIgnoreCase("RMD"))) {
formula1ChampionshipManager.removeDriver();
System.out.println("The driver has been removed.");
} else if ((menu.equals("3")) || (menu.equalsIgnoreCase("CTD"))) {
formula1ChampionshipManager.changeDriver();
System.out.println("The driver of the team has been changed.");
} else if ((menu.equals("4")) || (menu.equalsIgnoreCase("DDS"))) {
formula1ChampionshipManager.displayDriverStats();
System.out.println("The driver's statistics has been displayed.");
} else if ((menu.equals("5")) || (menu.equalsIgnoreCase("DDT"))) {
formula1ChampionshipManager.displayDriverTable();
System.out.println("The Formula1 driver table has been displayed.");
} else if ((menu.equals("6")) || (menu.equalsIgnoreCase("ARD"))) {
formula1ChampionshipManager.addRaceDetails();
System.out.println("The details of the completed race has been added..");
} else if ((menu.equals("7")) || (menu.equalsIgnoreCase("SCD"))) {
formula1ChampionshipManager.saveCurrentData();
System.out.println("All current data has been saved to file.");
} else if ((menu.equals("8")) || (menu.equalsIgnoreCase("LPD"))) {
formula1ChampionshipManager.loadPreviousData();
System.out.println("All previous data has been loaded from file.");
}else if ((menu.equals("9")) || (menu.equalsIgnoreCase("GUI"))) {
guiCode.gui();
System.out.println("The user interface has been opened.");
} else if ((menu.equals("0")) || (menu.equalsIgnoreCase("EXT"))) {
System.out.println("Manager program has been terminated.");
break;
}
}
}
}
import javax.swing.*;
import javax.swing.table.DefaultTableModel;
import javax.swing.text.JTextComponent;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.Vector;
public class F1ChampionshipGUI extends Formula1ChampionshipManager {
DefaultTableModel tModel;
JTable table;
public void gui() {
JFrame frame = new JFrame("F1 Championship Manager Interface");
JPanel panel1 = new JPanel();
panel1.setBounds(0,0,550,500);
JLabel descendPointTable = new JLabel("Display the drivers in descending order of points:");
descendPointTable.setBounds(50, 50, 400, 30);
JButton ascendBtn = new JButton("Display");
ascendBtn.setBounds(400, 50, 80, 30);
frame.add(descendPointTable);
frame.add(ascendBtn);
frame.setSize(550, 500);
frame.setLayout(null);
frame.setVisible(true);
ascendBtn.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
for (int i = 0; i < f1Driver.size() - 1; i++) {
for (int j = 0; j < f1Driver.size() - 1 - i; j++) {
if ((f1Driver.get(j).compareTo(f1Driver.get(j + 1))) < 0) {
Formula1Driver tempDriver = f1Driver.get(j + 1);
f1Driver.set(j + 1, f1Driver.get(j));
f1Driver.set(j, tempDriver);
}
}
}
// JTable table=new JTable();
// table.setModel(new javax.swing.table.DefaultTableModel(new Object[][]{},new String[]{"Name","Team","Location","1st positions","2nd positions","3rd positions","Races participated","Current points"}));
//// addToTable();
//
// public static void addToTable(ArrayList<Formula1Driver> ,JTable table){
JFrame fDescend = new JFrame();
tModel = new DefaultTableModel(0, 0);
table = new JTable(tModel);
fDescend.setSize(1500, 400);
fDescend.setVisible(true);
String[] statFields = {"Name", "Team", "Location", "First positions", "Second positions", "Third positions", "Races participated", "Points"};
// int driversCount = 0;
// for (int a=0;a<f1Driver.size();a++){
// driversCount++;
// }
tModel.setColumnIdentifiers(statFields);
table.setModel(tModel);
table.setBounds(0, 500, 1200, 500);
table.setOpaque(true);
for (int b = 0; b < f1Driver.size(); b++) {
Object[] stats = new Object[]{
f1Driver.get(b).getDriverName(),
f1Driver.get(b).getDriverTeam(),
f1Driver.get(b).getDriverLocation(),
f1Driver.get(b).getFirstPositions(),
f1Driver.get(b).getSecondPositions(),
f1Driver.get(b).getThirdPositions(),
f1Driver.get(b).getRacesParticipated(),
f1Driver.get(b).getPoints(),
};
tModel.addRow(stats);
}
// for(int x=0;x<f1Driver.size();x++) {
// Vector<Object> data = new Vector<Object>();
//
//
// data.addElement(f1Driver.get(x).getDriverName());
// data.addElement(f1Driver.get(x).getDriverTeam());
// data.addElement(f1Driver.get(x).getDriverLocation());
// tModel.addRow(data);
// }
//
JScrollPane scrollPane = new JScrollPane(table);
scrollPane.setBounds(0, 500, 1200, 500);
panel1.add(scrollPane);
fDescend.add(panel1);
}
});
}
}
【问题讨论】:
-
我要改变的东西太多了。从哪儿开始?您的 GUI 不应该只显示 JTable,它还应该允许用户执行您显示到控制台的菜单中的所有操作,因为您不应该在同一个应用程序中同时使用控制台和 GUI 进行用户交互。通常你不应该使用空的layout。如果您需要帮助调试代码,那么我认为您应该发布minimal reproducible example。
-
因此,从概念上讲,您的代码可以工作,问题出在数据源上。您的 GUI 是从
Formula1ChampionshipManager扩展而来的,我假设它充当某种数据源,问题是您创建了此数据源的两个实例,因此 GUI 可能不知道其他数据源具有什么信息。所以,是的,Abra 的评论实际上是正确的 - 放弃控制台输入并通过 GUI 构建所有管理 -
或者,从
F1ChampionshipGUI中删除extends Formula1ChampionshipManager,然后将它需要的数据直接传递给它
标签: java swing arraylist jtable