相关疑难解决方法(0)

JButton Image Icon不显示.png文件

我一直在寻找解决方案,并阅读一些与此问题相关的类似帖子但其中没有一个对我有用.

我正在尝试在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)

java swing png embedded-resource imageicon

3
推荐指数
1
解决办法
7481
查看次数

标签 统计

embedded-resource ×1

imageicon ×1

java ×1

png ×1

swing ×1