jtable窗口不显示arraylist内容

lndjwyie  于 2021-07-11  发布在  Java
关注(0)|答案(1)|浏览(369)

我试着做一个程序,你可以启动一个摆动框架按钮,并从中选择一个按钮做各种事情。其中之一就是显示一个表,其中包含 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);

}
xghobddn

xghobddn1#

你应该通过现有的 PremierLeagueManager 进入 GUInterface 建造师。
在主要方法中:

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

这个 GUInterface 构造函数必须接受 PremierLeagueManager 作为参数。在guinterface课程中:

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类的开始如下所示:

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

相关问题