单击j / ToggleButton,然后设置Icon / Image

时间:2014-11-26 03:52:59

标签: java swing click icons jtogglebutton

我正在制作像扫雷一样的4x4板。每个按钮都有一个炸弹或其他图像。

这是我的代码:

private void jButton1MouseClicked(java.awt.event.MouseEvent evt) {                                      
    this.jToggleButton1.setIcon(new javax.swing.ImageIcon("bombaa.png"));          
}         

private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {                                         
    this.jToggleButton1.setIcon(new javax.swing.ImageIcon("bombaa.png"));
}                    

也试过这种方式......

private void setIcon1(){
    setIconImage(Toolkit.getDefaultToolkit().getImage(getClass().getResource("bombaa.png")));
}

并在jButton1ActionPerformed和jButton1MouseClicked中调用setIcon()但这会将我的图像设置为程序的主图标。

基本上我需要的是:点击一个按钮,只设置一次图像/图标。

1 个答案:

答案 0 :(得分:5)

首先创建一个您可以控制所选状态的按钮...

public class StickyModel extends JToggleButton.ToggleButtonModel {

    public void reset() {
        super.setSelected(false);
    }

    @Override
    public void setSelected(boolean b) {
        if (!isSelected()) {
            super.setSelected(b);
        }
    }

}

这样可以防止按钮在设置完成后被“取消选中”(它还包含一个reset方法,可以让你“取消选择”该按钮。

使用“空白”或空“默认”图标创建按钮,并将selectedIcon属性设置为选择按钮时显示的内容...

JToggleButton btn = new JToggleButton();
btn.setModel(new StickyModel());
btn.setIcon(new ImageIcon(ImageIO.read(getClass().getResource("/Blank.png"))));
btn.setSelectedIcon(new ImageIcon(ImageIO.read(getClass().getResource("/Bomb.png"))));

因此,单击该按钮时,它将使用selectedIcon

Sticky Buttons

import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.GridBagLayout;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JToggleButton;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

public class Test {

    public static void main(String[] args) {
        new Test();
    }

    public Test() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                    ex.printStackTrace();
                }

                JFrame frame = new JFrame("Testing");
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.add(new TestPane());
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }

    public class TestPane extends JPanel {

        public TestPane() {
            setLayout(new GridBagLayout());
            try {
                add(createButton());
                add(createButton());
                add(createButton());
            } catch (IOException exp) {
                exp.printStackTrace();
            }
        }

        protected JToggleButton createButton() throws IOException {

            JToggleButton btn = new JToggleButton();
            btn.setModel(new StickyModel());
            btn.setIcon(new ImageIcon(ImageIO.read(getClass().getResource("/Blank.png"))));
            btn.setSelectedIcon(new ImageIcon(ImageIO.read(getClass().getResource("/Bomb.png"))));
            return btn;

        }

    }

    public class StickyModel extends JToggleButton.ToggleButtonModel {

        public void reset() {
            super.setSelected(false);
        }

        @Override
        public void setSelected(boolean b) {
            if (!isSelected()) {
                super.setSelected(b);
            }
        }

    }

}