我一直在寻找解决方案,并阅读一些与此问题相关的类似帖子但其中没有一个对我有用.
我正在尝试在JButton上显示图像"b.png",当我滚动按钮时图标会发生变化.
package GUI_JButton;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
public class Gui extends JFrame {
private JButton reg;
private JButton custom;
public Gui() {
super("Title goes here");
setLayout(new FlowLayout());
reg = new JButton("reg button"); // create reg button
add(reg); // add reg button to JFrame
// initialize images
Icon b = new ImageIcon(getClass().getResource("images/imageA.png"));
Icon x = new ImageIcon(getClass().getResource("images/imageB.png"));
custom = new JButton("custom button", b); // create custom …Run Code Online (Sandbox Code Playgroud)