gpt4 book ai didi

java - 单击 j/ToggleButton 然后设置图标/图像

转载 作者:行者123 更新时间:2023-12-02 15:30:35 27 4
gpt4 key购买 nike

我正在制作一个有点像扫雷器的 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() 但这会将我的图像设置为程序的主图标。

基本上我需要的是:单击一个按钮并仅设置一次图像/图标。

最佳答案

首先创建您自己的按钮,一个您可以控制选定状态的按钮...

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

}

}

关于java - 单击 j/ToggleButton 然后设置图标/图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27141032/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com