【发布时间】:2021-03-01 21:55:32
【问题描述】:
我正在尝试制作一个程序,您可以在其中启动带有按钮的摆动框架,然后从中选择一个按钮来执行各种操作。其中之一是在我的ArrayList 中显示一个包含数据的表格。按下按钮时,表格会打开,但即使我将内容添加到数组列表中,内容也是空的。如果我以我在 switch 语句中注释掉的方式在 case “G”下调用表,我会从数组列表中获取数据并且表可以工作。
我不确定是什么导致了我无法打开表格并显示信息的按钮。
public static void main(String[] args) {
/* Display user menu, select from menu */
String menuSelector;
Scanner selectfromMenu = new Scanner(System.in);
PremierLeagueManager PLM = new PremierLeagueManager();
PLM.menu();
/* dowhile to allow multiple functionality through menu */
do {
menuSelector = PLM.userInput.nextLine().toUpperCase();
switch(menuSelector){
case "A":
PLM.addClub();
break;
case "V":
PLM.displayClubs(PLM.clubs);
break;
case "D":
PLM.deleteClub(PLM.clubs);
break;
case "S":
PLM.addMatchStatistic(PLM.clubs);
break;
case "F":
PLM.displayStatistics(PLM.clubs);
break;
case "G":
GUInterface GUI = new GUInterface();
//PLM.displayTable(PLM.clubs);
break;
case "Q":
System.exit(0);
break;
default:
break;
}
} while(menuSelector != "q");
}
public class GUInterface {
JFrame frame;
JPanel jPanel = new JPanel();
Container c;
GUInterface()
{
frame = new JFrame("PremierLeageGUI");
JButton leagueTable = new JButton("Premier League Table");
JButton sortGoals = new JButton("Sort by goals scored, descending order");
JButton sortWins = new JButton("Sort by wins scored, descending order");
c = frame.getContentPane();
frame.setLayout(new FlowLayout());
c.add(leagueTable);
c.add(sortGoals);
c.add(sortWins);
leagueTable.addActionListener(new ActionEvents(frame, leagueTable));
frame.setSize(400,400);
frame.setVisible(true);
frame.getContentPane().setBackground(Color.pink);
frame.addWindowListener(new WindowListener());
}
}
public class ActionEvents implements ActionListener {
JFrame frame;
JButton showtable;
PremierLeagueManager p = new PremierLeagueManager();
ActionEvents(JFrame f, JButton b)
{
frame = f;
showtable = b;
}
@Override
public void actionPerformed(ActionEvent e) {
if(e.getSource() == showtable)
{
p.displayTable(p.clubs);
}
}
}
//displayTable method inside PremierLeagueManager class
protected ArrayList<FootballClub> clubs = new ArrayList<FootballClub>();
public void displayTable(ArrayList<FootballClub> footballClubs)
{
String[] columnNames = {"Club name", "goals", "points", "wins"};
DefaultTableModel model = new DefaultTableModel(columnNames, 0);
for(int i = 0; i < footballClubs.size(); i++)
{
String name = footballClubs.get(i).getClubName();
int goals = footballClubs.get(i).getGoals();
int points = footballClubs.get(i).getPoints();
int wins = footballClubs.get(i).getWins();
Object[] row = {name, goals, points, wins};
model.addRow(row);
System.out.println(name);
}
final JTable teamTable = new JTable(model);
teamTable.setFillsViewportHeight(true);
JPanel panel = new JPanel();
JScrollPane scrollPane = new JScrollPane(teamTable);
panel.add(scrollPane);
JFrame frame = new JFrame("Tableview");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
teamTable.setOpaque(true);
frame.setContentPane(panel);
frame.pack();
frame.setSize(500, 500);
frame.setVisible(true);
}
【问题讨论】:
-
我们无法调试您的代码。您可以单步执行代码并按照逻辑确定不显示表格的原因。我会首先从设计一个合适的 UI 开始。 1) 不要使用命令行界面和图形界面。 2)不要显示多帧。一个应用程序应该只有一个 JFrame。
-
所以我会设计初始框架,使其具有带有项目的菜单栏或在带按钮的框架的 BorderLayout.LINE_START 上具有面板。当用户单击菜单项或按钮时,与该按钮对应的面板将显示在框架的 BorderLayout.CENTER 中。
-
在您的主要方法中,您创建一个包含所有数据的
PremierLeagueManager PLM = new PremierLeagueManager();。在ActionEvents类中,您创建一个不包含数据的新PremierLeagueManager p = new PremierLeagueManager();。您应该传递现有的PremierLeageManager,以便 GUI 可以访问存储在那里的数据。 -
我会扩展 PremierLeagueManager 类吗?并像这样传递它
标签: java swing arraylist jtable