将ImageIcons添加到JButtons后,按钮不再有效

时间:2016-05-06 05:49:47

标签: java swing jbutton imageicon

Image Icons on JButtons

我提前完成了一个项目,所以在提交之前剩下的时间,我想进行实验。整个程序运行良好,JButton完全按照他们的编程方式完成。

将ImageIcon添加到我的JButtons后出现问题。我将展示一些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");
}   

按钮的唯一差异在于现在有ImageIcons而不是文本。

我的问题在于以下代码。这是我第一次在按钮上使用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触发事件?感谢您提供的任何帮助。如果这不起作用,我很乐意用纯文本将其改回旧版本。

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

4 个答案:

答案 0 :(得分:3)

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

change_third_set_colour

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

答案 1 :(得分:2)

您没有为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);
            } 
......

答案 2 :(得分:1)

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

你可以......

只需使用Ctr + N

source即可
ActionEvent

如果你有对原始按钮的引用

,这可能没问题

你可以......

为每个按钮分配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 (...) { //... } } }; API并为每个按钮制作单独的,自包含的操作......

Action

然后你可以简单地使用

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方法。通过这种方式,您可以100%了解调用actionPerformed方法时应该/需要做的事情,毫无疑问。

详细了解How to Use Actions了解更多详情

答案 3 :(得分:0)

@ Sandeep.K

我仍然会接受你的回答,因为我喜欢对"正常设置"的短暂呼叫。在看到你的答案之前,我走了很远的路。两者都有效,但你的更好。

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