将 ImageIcons 添加到 JButtons 后,按钮不再起作用

After adding ImageIcons to JButtons, buttons no longer work

我提前完成了一个项目,所以我想利用提交前的剩余时间进行试验。整个程序运行良好,JButton 完全按照它们的编程方式执行。

将 ImageIcon 添加到我的 JButton 后出现问题。我将展示一些 JButton 初始化,并在每个块上方的单个注释中展示原始内容:

/**
 * create child components
 */
private void initComponents() {

    //normalSetupButton = new JButton("Normal Setup");
    ImageIcon normalButtonImage = new ImageIcon("src/Images/normalIcon.png");
    normalSetupButton = new JButton();
    normalSetupButton.setIcon(normalButtonImage);
    normalSetupButton.addActionListener(buttonHandler);
    normalSetupButton.setToolTipText("Set up simulation for normal execution");

    // queen test button
    //queenTestButton = new JButton("Queen Test");
    ImageIcon queenButtonImage = new ImageIcon("src/Images/yellowIcon.jpg");
    queenTestButton = new JButton();
    queenTestButton.setIcon(queenButtonImage);
    queenTestButton.addActionListener(buttonHandler);
    queenTestButton.setToolTipText("Set up to test Queen Lifespan or Food Levels");

    // scout test button
    //scoutTestButton = new JButton("Scout Test");
    ImageIcon scoutButtonImage = new ImageIcon("src/Images/blueIcon.png");
    scoutTestButton = new JButton();
    scoutTestButton.setIcon(scoutButtonImage);
    scoutTestButton.addActionListener(buttonHandler);
    scoutTestButton.setToolTipText("Set up simulation for testing the Scout ant");
}   

按钮的唯一区别是现在有图像图标而不是文本。

我的问题出在下面的代码上。这是我第一次在 Buttons 上使用 ImageIcons,所以我一直习惯 "button.getText().equals("Button String");"

public void actionPerformed(ActionEvent e) {
        // get the button that was pressed
        JButton b = (JButton) e.getSource();

        // fire appropriate event
        if (b.getText().equals("Normal Setup")) {
            // set up for normal simulation
            fireSimulationEvent(SimulationEvent.NORMAL_SETUP_EVENT);
        } else if (b.getText().equals("Queen Test")) {
            // set for testing the queen ant
            fireSimulationEvent(SimulationEvent.QUEEN_TEST_EVENT);
        } else if (b.getText().equals("Scout Test")) {
            // set for testing the scout ant
            fireSimulationEvent(SimulationEvent.SCOUT_TEST_EVENT);
        } else if (b.getText().equals("Forager Test")) {
            // set for testing the forager ant
            fireSimulationEvent(SimulationEvent.FORAGER_TEST_EVENT);
        } else if (b.getText().equals("Soldier Test")) {
            // set for testing the soldier ant
            fireSimulationEvent(SimulationEvent.SOLDIER_TEST_EVENT);
        } else if (b.getText().equals("Run")) {
            // run the simulation continuously
            fireSimulationEvent(SimulationEvent.RUN_EVENT);
        } else if (b.getText().equals("Step")) {
            // run the simulation one turn at a time
            fireSimulationEvent(SimulationEvent.STEP_EVENT);
        } else if (b.getText().equals("Stop")) {
            //stop everything
            fireSimulationEvent(SimulationEvent.STOP_EVENT);
        }
    }

Swing 大师们,如何根据 JButton 的 ImageIcon 触发事件?感谢您提供的任何帮助。如果这不起作用,我会很乐意将其改回纯文本的旧版本。

注意 是的,我知道图片的格式不尽相同。它们不会成为最终图像。我只是在测试并抓住了我现在能找到的任何格式。

你没有为 JButton 设置任何文本,所以它不起作用,如果你设置文本文本,那么它会出现在图像上,所以你可以做一件事 1. 设置 setName 方法并向其添加文本。 2. 在 actionPerformed 中使用 getName 而不是 getText.

例如:

ImageIcon normalButtonImage = new ImageIcon("src/Images/normalIcon.png");
    normalSetupButton = new JButton();
    normalSetupButton.setIcon(normalButtonImage);
    normalSetupButton.setName("Normal Setup");
    normalSetupButton.addActionListener(buttonHandler);
    normalSetupButton.setToolTipText("Set up simulation for normal execution");

在执行的操作中:

    public void actionPerformed(ActionEvent e) {
            // get the button that was pressed
            JButton b = (JButton) e.getSource();

            // fire appropriate event
            if (b.getName().equals("Normal Setup")) {
                // set up for normal simulation
                fireSimulationEvent(SimulationEvent.NORMAL_SETUP_EVENT);
            } 
......

@Sandeep.K

我仍然会接受你的回答,因为我喜欢更短的 "Normal Setup" 电话。在看到您的答案之前,我走了很长一段路。两者都有效,但你的更好。

public void actionPerformed(ActionEvent e) {
                // get the button that was pressed
                JButton b = (JButton) e.getSource();

                // fire appropriate event
                if(b.getToolTipText().equals("Set up simulation for normal execution")) {
                    fireSimulationEvent(SimulationEvent.NORMAL_SETUP_EVENT);
                }
                else if(b.getToolTipText().equals("Set up to test Queen Lifespan or Food Levels")) {
                    fireSimulationEvent(SimulationEvent.QUEEN_TEST_EVENT);
                }
                else if (b.getToolTipText().equals("Set up simulation for testing the Forager ant (Scouts are included)")) {
                    // set for testing the forager ant
                    fireSimulationEvent(SimulationEvent.FORAGER_TEST_EVENT);
                } else if (b.getToolTipText().equals("Set up simulation for testing the Soldier ant (Scouts are included")) {
                    // set for testing the soldier ant
                    fireSimulationEvent(SimulationEvent.SOLDIER_TEST_EVENT);
                } else if (b.getToolTipText().equals("Run the simulation continuously")) {
                    // run the simulation continuously
                    fireSimulationEvent(SimulationEvent.RUN_EVENT);
                } else if (b.getToolTipText().equals("Step through the simulation one turn at a time")) {
                    // run the simulation one turn at a time
                    fireSimulationEvent(SimulationEvent.STEP_EVENT);
                } else if (b.getToolTipText().equals("Stop or Pause the simulation")) {
                    //stop everything
                    fireSimulationEvent(SimulationEvent.STOP_EVENT);
                }

我认为修改代码的最佳方法是使用按钮的客户端属性。

private static final String EVENT_TYPE = "event_type";

// button creation
normalSetupButton = new JButton();
// set appropriate event for this button
normalSetupButton.putClientProperty(EVENT_TYPE, SimulationEvent.NORMAL_SETUP_EVENT);
// other init button routine

//next button
queenTestButton = new JButton();
queenTestButton.putClientProperty(EVENT_TYPE, SimulationEvent.QUEEN_TEST_EVENT);
// other init button routine

// same way for other buttons 


public void actionPerformed(ActionEvent e) {
    // get the button that was pressed
    JButton b = (JButton) e.getSource();
    SimulationEvent evt = (SimulationEvent) b.getClientProperty(EVENT_TYPE);
    fireSimulationEvent(evt);
}

这看起来比 "if-else if" 级联更好 ;)

您可以通过多种方式实现这一目标

你可以...

只需使用 ActionEvent

source
Action voting = new AbstractAction(){
    @Override
    public void actionPerformed(ActionEvent e){
        if (e.getSource() == vote_up) {
            //...
        } else if (...) {
            //...
        }
    }
};

如果你有对原始按钮的引用,这可能没问题

你可以...

为每个按钮分配一个 actionCommand

JButton vote_up = new JButton(upvote);
vote_up.setActionCommand("vote.up");
JButton vote_down = new JButton(downvote);
vote_down .setActionCommand("vote.down");
//...
Action voting = new AbstractAction(){
    @Override
    public void actionPerformed(ActionEvent e){
        if ("vote.up".equals(e.getActionCommand())) {
            //...
        } else if (...) {
            //...
        }
    }
};

你可以...

充分利用 Action API 并为每个按钮制作独立的自包含操作...

public class VoteUpAction extends AbstractAction {

    public VoteUpAction() {
        putValue(SMALL_ICON, new ImageIcon(getClass().getResource("vote_up.png")));
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        // Specific action for up vote
    }

}

那么你可以简单地使用

JButton vote_up = new JButton(new VoteUpAction());
//...

这将根据 Action 的属性配置按钮,并在触发按钮时触发它的 actionPerformed 方法。这样,当 actionPerformed 方法被调用时,您 100% 知道 should/need 要做什么,毫无疑问。

仔细查看 How to Use Actions 了解更多详情