将ImageIcons添加到JButtons之后,按钮不再起作用 [英] After adding ImageIcons to JButtons, buttons no longer work

查看:102
本文介绍了将ImageIcons添加到JButtons之后,按钮不再起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尽早完成了一个项目,因此在提交之前有剩余时间,我想尝试一下.整个程序运行良好,并且JButton的功能与编程的目的完全相同.

I finished a project early, so with the remaining time I had before submitting it, I wanted to experiment. The entire program works great, and the JButtons did exactly as they were programmed to do.

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

Something went awry after adding ImageIcon(s) to my JButtons. I'll show a few of the JButton initializations and show the original in a single comment above each block:

/**
 * 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而不是文本.

The only difference with the buttons is that there are now ImageIcons rather than text.

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

My problem lies in the following code. This is the first time I've ever used ImageIcons on Buttons, so I've always been accustomed to "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触发一个事件?感谢您提供的任何帮助.如果这不起作用,我将很高兴将其更改为仅包含纯文本的旧版本.

So, Swing gurus, how does one fire an event based on the ImageIcon of a JButton? Thanks for any help offered. If this doesn't work, I'll happily change it back to the old version with just plain text.

注意是,我知道图像并非全部采用相同的格式.他们不会成为最终的图像.我只是在测试,现在就抓住了我可以找到的任何格式.

NOTE Yes, I know the images are not all in the same format. They aren't going to be the final images. I'm just testing and grabbed whatever format I could find for now.

推荐答案

您没有为JButton设置任何文本,因此它不起作用,如果设置了Text,则文本将出现在图像上,因此您可以做一件事 1.设置setName方法并在其中添加文本. 2.在actionPerformed中,使用getName而不是getText.

You are not set any text to JButton so it's not working, If you set text Text, then it will appear on the image, So you can do one thing 1. Set the setName Method and add text to that. 2. in actionPerformed, use getName rather than 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);
            } 
......

这篇关于将ImageIcons添加到JButtons之后,按钮不再起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆