JTable window 不显示 ArrayList 内容

JTable window not displaying ArrayList content

我正在尝试制作一个程序,您可以在其中启动带有按钮的摆动框架,然后从中选择一个按钮来执行各种操作。其中之一是显示一个 table,其中包含我的 ArrayList 中的数据。按下按钮时,table 打开,但内容为空,即使我将内容添加到数组列表中也是如此。如果我以我在 switch 语句中注释掉的方式调用 table

我不确定是什么原因导致我无法获得打开 table 和显示信息的按钮。

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);

}

您应该将现有的 PremierLeagueManager 传递给 GUInterface 构造函数。

在主要方法中:

    case "G":
        GUInterface GUI = new GUInterface(PLM);
        break;

GUInterface 构造函数必须接受 PremierLeagueManager 作为参数。在 GU 界面中 class:

public class GUInterface {
    // some lines left out
    GUInterface(PremierLeagueManager plm)
    {
        // some lines left out
        leagueTable.addActionListener(new ActionEvents(frame, leagueTable, plm));
        // some lines left out
    }

ActionEvent 的开始 class 看起来像这样:

public class ActionEvents implements ActionListener {

    JFrame frame;
    JButton showtable;
    PremierLeagueManager p;
    ActionEvents(JFrame f, JButton b, PremierLeagueManager plm)
    {
        frame = f;
        showtable = b;
        p = plm;
    }
    // some lines left out