【问题标题】:Image Icon not showing on JButton图像图标未显示在 JButton 上
【发布时间】:2013-12-11 20:36:40
【问题描述】:

我目前正在用 java 制作游戏,并且有一个带有图像图标的 JButton。唯一的问题是图像没有显示,甚至在调试窗口中也没有抛出错误。

我已经打包了我的程序(见截图 - https://db.tt/N9CwHJdf)。我使用的代码写在下面,如果有人能解决这个问题,我将不胜感激。谢谢。

//Button Image
ImageIcon diceIcon = new ImageIcon("Client/images/DiceIcon.png");

//Create Button
JButton rollDice = new JButton("Roll Dice", diceIcon);
rollDice.setForeground(Color.darkGray);
rollDice.setFocusPainted(false);
rollDice.setPreferredSize(new Dimension(284,50));
rollDice.setBorder(BorderFactory.createLineBorder(Color.orange));
rollDice.setBackground(Color.orange);
rollDice.setToolTipText("Click to roll dice and continue playing");
rollDice.addActionListener(this);

【问题讨论】:

  • DiceIcon = new ImageIcon("Client/images/DiceIcon.png");尝试给出完整的图像路径
  • 我添加了以下行:DiceIcon = new ImageIcon("src/Client/images/DiceIcon.png");这仍然行不通:(
  • 尝试从项目中的开始文件获取路径,看看是否可行...在路径方面,图像很棘手

标签: java user-interface jbutton packaging imageicon


【解决方案1】:

你可以像这样加载你的 ImageIcon:

ImageIcon diceIcon = new ImageIcon(getClass().getResource("/images/DiceIcon.png"));

阅读How to Use Icons 上的 Java 教程了解更多信息。

【讨论】:

  • 我也有同样的问题。但在我的情况下,它必须看起来像这样(复制代码):ImageIcon diceIcon = new ImageIcon(getClass().getResource("/images/DiceIcon.png")); static JButton mybutton = new JButton("mybutton", diceIcon); public static void main(String[] args) { add(mybutton); } 但是它会在 getClass() 下划线并显示错误,因为它是静态的。任何想法如何解决这个问题?
  • @Tom Lenc:试试这个:import javax.swing.*;公共类 Foo 扩展 JFrame { private ImageIcon diceIcon = new ImageIcon(getClass().getResource("/images/DiceIcon.png"));私人 JButton mybutton = new JButton("mybutton", diceIcon); public Foo(){ add(mybutton);盒(); } public static void main(String[] args) { new Foo().setVisible(true); } }
  • 那行不通。我必须在 main 方法之后的特定部分之后添加该按钮,这意味着 mybutton 必须是静态的,这意味着 ImageIcon 也必须是静态的,但随后我会抛出错误。很难解释..(我不是美国人)
  • 这个问题是你不能在静态上下文中使用 getClass(),因为它是一个非静态方法。如果您跳过 getClass() 和 getResource() 并编写:private static ImageIcon diceIcon = new ImageIcon("/images/DiceIcon.png");,则可以绕过此限制,但是当您尝试添加时,编译器会得到您(我的按钮);因为 add 也不是静态的。
【解决方案2】:

在尝试在JButton 上渲染之前,您可能应该使用ImageIcon.getImageLoadStatus() 确保图像加载没有错误。

【讨论】:

    【解决方案3】:

    javas.swing.Action 是 Java GUI API 中的一种 ugly duckling 类:尽管它的提议很有趣,但使用不多,正确使用的示例也很少。

    在按钮中使用 Action 时,您可以定义几个通常必须直接添加到按钮实例的属性,例如图标、工具提示、字幕等

    一个恼人的问题是 Action 会用警告覆盖您的按钮设置!因此,如果您使用 Action,请避免设置额外的属性。

    您可以定义自己的Action工厂类或扩展AbstractAction并使用putValue(String key, Object value)来设置您的按钮属性,如下面的代码sn-p所示。

    请确保在使用 putValues() 设置您自己的值时使用 Action 类中定义的键。

    **
    * Facility class for AbstractAction
    * Remember that Action also extends ActionListener interface
    *
    */
    class MyAction extends AbstractAction {     
    /**
     * Ctor.
     * @param name - The Action.NAME or the button caption
     * @param actionCMD - the Action.ACTION_COMMAND_KEY value
     * @param shortDescription - the tool tip, Action.SHORT_DESCRIPTION
     * @param icon - the default icon, or  Action.SMALL_ICON paramenter.
     */
    public MyAction(String name, String actionCMD, String shortDescription, Icon icon) {                   
         putValue(Action.NAME, name);  
         putValue(Action.SHORT_DESCRIPTION, shortDescription);  
         putValue(Action.ACTION_COMMAND_KEY, actionCMD);  
         if(icon != null)
             putValue(Action.SMALL_ICON, icon);
     }  
    
     /**
      * The action to be performed
      */
     public void actionPerformed(ActionEvent e) {  
        // put your action code here
     }  
    } // end class
     
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-06-01
      • 2012-08-13
      • 1970-01-01
      • 2013-07-19
      • 1970-01-01
      • 1970-01-01
      • 2019-04-03
      相关资源
      最近更新 更多