ActionListener在单击按钮时不执行任何操作

时间:2018-11-27 23:47:35

标签: java swing jtable actionlistener

public class ReadGameDetailsWindow extends JFrame implements ActionListener {

    private GameFactory theFactory = GameFactory.getInstance();
    private HashMap<String, Game> theListOfGames = theFactory.listOfGames();

    JTable table;

    // Delete game panel
            JPanel deletePanel = new JPanel(new GridLayout(2,2));
            JLabel theDeleteGameName = new JLabel("Game Name:");
            JTextField theDeleteGameNameField = new JTextField();
            JButton theDeleteCancelButton = new JButton("Clear");
            JButton theDeleteOKButton = new JButton("Delete");

    String[] columns = {"Name", "Developer", "Genre", "Rating"};
    Object[][] tableData = new Object[theListOfGames.keySet().size()][4];

    public void fillDetails() {
        int index = 0;
        for (String key : theListOfGames.keySet())
        {
            Game game = theListOfGames.get(key);
            tableData[index][0] = game.getName();
            tableData[index][1] = game.getDeveloper();
            tableData[index][2] = game.getGenre();
            tableData[index][3] = game.getOutOfTen() + "/10";
            index++;
        }
    }

    public ReadGameDetailsWindow() {
        fillDetails();

        deletePanel.add(theDeleteGameName);
        deletePanel.add(theDeleteGameNameField);
        deletePanel.add(theDeleteCancelButton);
        deletePanel.add(theDeleteOKButton);

        TitledBorder deleteBorder = BorderFactory.createTitledBorder("Delete Section");

        table = new JTable(tableData, columns);
        table.setPreferredScrollableViewportSize(new Dimension(500, 50));
        JScrollPane scrollPane = new JScrollPane(table);

        JPanel controls = new JPanel();
        controls.setLayout(new GridLayout(1,3));

        controls.add(deletePanel);

        setLayout(new GridLayout(2,1));

        getContentPane().add(scrollPane);
        getContentPane().add(controls);
        setVisible(true);
        setSize(800,800);


    }

    @Override
    public void actionPerformed(ActionEvent e) {
        if (e.getSource().equals(theDeleteOKButton)) {
            try {
                System.out.println("Delete Button Pressed");
                String aGameName = theDeleteGameNameField.getText();
                theFactory.deleteGame(aGameName);
                fillDetails();
                table.repaint();
                JOptionPane.showMessageDialog(new JFrame(), aGameName + " has been deleted!");
            } catch (Exception aException) {
                aException.printStackTrace();
            }
        }
    }
}

因此,实际上,当我单击theDeleteOKButton时没有任何反应。我正在尝试从我的HashMap中删除一条记录,该记录是显示信息的地方。

我尝试在主构造函数内部和外部创建和实例化按钮,并尝试分别添加actionListeners,但似乎一点反应都没有。

我是否需要移动按钮,或者我是否缺少其他选择? 抱歉,我是Swing的新手,只是想使其正常工作。

1 个答案:

答案 0 :(得分:1)

您需要在按钮上添加动作监听器。做到这一点:

theDeleteCancelButton.addActionListener(this); //detects button press action for theDeleteCancelButton
theDeleteOKButton.addActionListener(this); //detects button press action for theDeleteOKButton