【问题标题】:Issue with JButtonJButton 的问题
【发布时间】:2011-11-15 02:30:08
【问题描述】:

我有一些 JButton 的代码:

 solutionButton = new JButton("Solution");
 solutionButton.setBorderPainted( false);
 solutionButton.setContentAreaFilled( false );
 solutionButton.setFocusPainted( false);
 solutionButton.setFont( new Font("Arial",Font.BOLD,16));
 solutionButton.setForeground( new Color(80,21,25));
 solutionButton.setRolloverIcon( 
         new ImageIcon(getClass().getResource( "images/game.png")));
  solutionButton.setRolloverEnabled( true );
 add(solutionButton);

如果我只是简单地设置图标,它就可以正常工作,我会看到图标。如果我执行上述操作并尝试设置鼠标悬停图标,当我将鼠标悬停在按钮上时,我看不到任何图标。

我做错了什么?

谢谢

【问题讨论】:

标签: java swing jbutton


【解决方案1】:

指向月球!

import java.awt.Image;
import javax.swing.*;
import javax.imageio.ImageIO;
import java.net.URL;

class TestRolloverIcon {

    public static void main(String[] args) throws Exception {
        URL url1 = new URL("http://pscode.org/media/citymorn1.jpg");
        URL url2 = new URL("http://pscode.org/media/citymorn2.jpg");
        final Image image1 = ImageIO.read(url1);
        final Image image2 = ImageIO.read(url2);
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                JButton button = new JButton("Point at the Moon!");
                button.setIcon(new ImageIcon(image1));
                button.setRolloverIcon(new ImageIcon(image2));

                JOptionPane.showMessageDialog(null, button);
            }
        });
    }
}

有没有办法在不先设置图标的情况下做到这一点,我只想要一个翻转图标而不是一个图标。

指向空间(有很多)!

import java.awt.image.BufferedImage;
import javax.swing.*;
import javax.imageio.ImageIO;
import java.net.URL;

class TestRolloverIcon {

    public static void main(String[] args) throws Exception {
        URL url2 = new URL("http://pscode.org/media/citymorn2.jpg");
        final BufferedImage image2 = ImageIO.read(url2);
        final BufferedImage image1 = new BufferedImage(
            image2.getWidth(),image2.getHeight(),BufferedImage.TYPE_INT_ARGB);
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                JButton button = new JButton("Point at space (there's a lot of it)!");
                button.setIcon(new ImageIcon(image1));
                button.setRolloverIcon(new ImageIcon(image2));

                JOptionPane.showMessageDialog(null, button);
            }
        });
    }
}

【讨论】:

  • 有没有办法在不先设置图标的情况下做到这一点,我只想要一个翻转图标而不是图标。
猜你喜欢
  • 1970-01-01
  • 2015-07-28
  • 1970-01-01
  • 1970-01-01
  • 2013-04-03
  • 1970-01-01
  • 2023-03-13
  • 1970-01-01
  • 2012-12-13
相关资源
最近更新 更多