如何制作AWT Button()并使用ImageIcon()、Icon()?

发布于 2024-11-23 22:02:27 字数 368 浏览 0 评论 0原文

如何将 GIF 图像应用到我的 AWT Button

AWT:不起作用

    Icon warnIcon = new ImageIcon("/src/world.gif");
    Button button1 = new Button(warnIcon);

    Icon warnIcon = new ImageIcon("/src/world.gif");
    JButton button1 = new JButton(warnIcon);

How can I apply a GIF image to my AWT Button?

AWT: does not work

    Icon warnIcon = new ImageIcon("/src/world.gif");
    Button button1 = new Button(warnIcon);

    Icon warnIcon = new ImageIcon("/src/world.gif");
    JButton button1 = new JButton(warnIcon);

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

鸠书 2024-11-30 22:02:27

AWT 与 Swing 有点不同。

没有构造函数button(image),因此你的“不工作”。

查看了解如何扩展 AWT使用您自己的图像按钮来了解如何在 AWT 按钮上制作该图像。

AWT is a bit different from Swing.

There's no constructor button(image), hence your "not working".

Take a look at Learn how to extend the AWT with your own image buttons to see how to make that image on a AWT button.

毁梦 2024-11-30 22:02:27

回到您的三天历史记录,请尝试在您的本机操作系统上并使用 OpenJDK (也许确实是时候从 Oracle 下载一个稳定的 JDK,您只能在项目属性中进行切换):

  1. 通过 NetBeans 运行

  2. 编译为 JAR 文件或类

    导入java.awt.event.*;
    导入java.awt.Color;
    导入 java.awt.AlphaComposite;
    导入 javax.swing.*;
    导入 javax.swing.UIManager.LookAndFeelInfo;
    
    公共类按钮测试{
        公共静态无效主(字符串[] args){
            SwingUtilities.invokeLater(new Runnable() {
    
                @覆盖
                公共无效运行(){
                    new ButtonTest().createAndShowGUI();
                }
            });
        }
    
        私有 JFrame 框架;
        私有 JButton opaqueButton1;
        私有 JButton opaqueButton2;
        私有 SoftJButton softButton1;
        私有 SoftJButton softButton2;
    
        公共无效createAndShowGUI(){
            opaqueButton1 = new JButton("不透明按钮");
            opaqueButton2 = new JButton("不透明按钮");
            softButton1 = new SoftJButton("透明按钮");
            softButton2 = new SoftJButton("透明按钮");
            opaqueButton1.setBackground(Color.GREEN);
            softButton1.setBackground(Color.GREEN);
            框架=新的JFrame();
            Frame.getContentPane().setLayout(new java.awt.GridLayout(2, 2, 10, 10));
            框架.add(opaqueButton1);
            框架.add(softButton1);
            框架.add(opaqueButton2);
            框架.add(softButton2);
            框架.setSize(567, 350);
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            框架.setVisible(true);
            计时器 alphaChanger = new Timer(30, new ActionListener() {
                私有浮点数增量器=-.03f;
    
                @覆盖
                公共无效actionPerformed(ActionEvent e){
                    float newAlpha = softButton1.getAlpha() + 增量器;
                    if (newAlpha < 0) {
                        新阿尔法 = 0;
                        增量器=-增量器;
                    } else if (newAlpha > 1f) {
                        新阿尔法 = 1f;
                        增量器=-增量器;
                    }
                    softButton1.setAlpha(newAlpha);
                    softButton2.setAlpha(newAlpha);
                }
            });
            alphaChanger.start();
            计时器 uiChanger = 新计时器(3500,新 ActionListener(){
                私有 LookAndFeelInfo[] laf = UIManager.getInstalledLookAndFeels();
                私有 int 索引 = 1;
    
                @覆盖
                公共无效actionPerformed(ActionEvent e){
                    尝试 {
                        UIManager.setLookAndFeel(laf[index].getClassName());
                        SwingUtilities.updateComponentTreeUI(frame);
                    } catch(异常除外){
                        exc.printStackTrace();
                    }
                    索引 = (索引 + 1) % laf.length;
                }
            });
            uiChanger.start();
        }
    
        公共静态类 SoftJButton 扩展 JButton {
            私有静态最终 JButton lafDeterminer = new JButton();
            私有静态最终长serialVersionUID = 1L;
            私有布尔矩形LAF;
            私有浮点数 alpha = 1f;
    
            公共 SoftJButton() {
                这个(空,空);
            }
    
            公共 SoftJButton(字符串文本){
                这(文本,空);
            }
    
            公共 SoftJButton(字符串文本,图标图标){
                超级(文本,图标);
    
                设置不透明(假);
                setFocusPainted(假);
            }
    
            公共浮动 getAlpha() {
                返回阿尔法;
            }
    
            公共无效setAlpha(浮点阿尔法){
                this.alpha = 阿尔法;
                重绘();
            }
    
            @覆盖
            公共无效paintComponent(java.awt.Graphics g){
                java.awt.Graphics2D g2 = (java.awt.Graphics2D) g;
                g2.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, alpha));
                如果(矩形LAF && isBackgroundSet()){
                    颜色 c = getBackground();
                    g2.setColor(c);
                    g.fillRect(0, 0, getWidth(), getHeight());
                }
                super.paintComponent(g2);
            }
    
            @覆盖
            公共无效更新UI(){
                super.updateUI();
                lafDeterminer.updateUI();
                矩形LAF = lafDeterminer.isOpaque();
            }
        }
    }
    

Back to your three days history, please try to run the code below on your native OS and by using OpenJDK (maybe it is really time to download a stable JDK from Oracle, you can only switch that in the project properties):

  1. By running from NetBeans

  2. compiled to a JAR file or class

    import java.awt.event.*;
    import java.awt.Color;
    import java.awt.AlphaComposite;
    import javax.swing.*;
    import javax.swing.UIManager.LookAndFeelInfo;
    
    public class ButtonTest {
        public static void main(String[] args) {
            SwingUtilities.invokeLater(new Runnable() {
    
                @Override
                public void run() {
                    new ButtonTest().createAndShowGUI();
                }
            });
        }
    
        private JFrame frame;
        private JButton opaqueButton1;
        private JButton opaqueButton2;
        private SoftJButton softButton1;
        private SoftJButton softButton2;
    
        public void createAndShowGUI() {
            opaqueButton1 = new JButton("Opaque Button");
            opaqueButton2 = new JButton("Opaque Button");
            softButton1 = new SoftJButton("Transparent Button");
            softButton2 = new SoftJButton("Transparent Button");
            opaqueButton1.setBackground(Color.GREEN);
            softButton1.setBackground(Color.GREEN);
            frame = new JFrame();
            frame.getContentPane().setLayout(new java.awt.GridLayout(2, 2, 10, 10));
            frame.add(opaqueButton1);
            frame.add(softButton1);
            frame.add(opaqueButton2);
            frame.add(softButton2);
            frame.setSize(567, 350);
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            frame.setVisible(true);
            Timer alphaChanger = new Timer(30, new ActionListener() {
                private float incrementer = -.03f;
    
                @Override
                public void actionPerformed(ActionEvent e) {
                    float newAlpha = softButton1.getAlpha() + incrementer;
                    if (newAlpha < 0) {
                        newAlpha = 0;
                        incrementer = -incrementer;
                    } else if (newAlpha > 1f) {
                        newAlpha = 1f;
                        incrementer = -incrementer;
                    }
                    softButton1.setAlpha(newAlpha);
                    softButton2.setAlpha(newAlpha);
                }
            });
            alphaChanger.start();
            Timer uiChanger = new Timer(3500, new ActionListener() {
                private LookAndFeelInfo[] laf = UIManager.getInstalledLookAndFeels();
                private int index = 1;
    
                @Override
                public void actionPerformed(ActionEvent e) {
                    try {
                        UIManager.setLookAndFeel(laf[index].getClassName());
                        SwingUtilities.updateComponentTreeUI(frame);
                    } catch (Exception exc) {
                        exc.printStackTrace();
                    }
                    index = (index + 1) % laf.length;
                }
            });
            uiChanger.start();
        }
    
        public static class SoftJButton extends JButton {
            private static final JButton lafDeterminer = new JButton();
            private static final long serialVersionUID = 1L;
            private boolean rectangularLAF;
            private float alpha = 1f;
    
            public SoftJButton() {
                this(null, null);
            }
    
            public SoftJButton(String text) {
                this(text, null);
            }
    
            public SoftJButton(String text, Icon icon) {
                super(text, icon);
    
                setOpaque(false);
                setFocusPainted(false);
            }
    
            public float getAlpha() {
                return alpha;
            }
    
            public void setAlpha(float alpha) {
                this.alpha = alpha;
                repaint();
            }
    
            @Override
            public void paintComponent(java.awt.Graphics g) {
                java.awt.Graphics2D g2 = (java.awt.Graphics2D) g;
                g2.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, alpha));
                if (rectangularLAF && isBackgroundSet()) {
                    Color c = getBackground();
                    g2.setColor(c);
                    g.fillRect(0, 0, getWidth(), getHeight());
                }
                super.paintComponent(g2);
            }
    
            @Override
            public void updateUI() {
                super.updateUI();
                lafDeterminer.updateUI();
                rectangularLAF = lafDeterminer.isOpaque();
            }
        }
    }
    
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文