【问题标题】:How to make AWT Button() and use ImageIcon(), Icon()?如何制作 AWT Button() 并使用 ImageIcon()、Icon()?
【发布时间】:2011-10-06 19:57:02
【问题描述】:

如何将 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);

【问题讨论】:

  • 我仍然说 Swing 在处理透明度时会比 AWT 工作得更好,但是您仍然没有发布适当的 SSCCE 来向我们展示您在做什么,所以您希望我们如何提供帮助。相反,您会得到所有这些“AWT hacks”,从长远来看,这些都是更多的工作。
  • @camickr:我在主窗口中有每秒帧数,这会导致 JButton 变得不可见。我尝试了所有但没有工作java.sun.com/products/jfc/tsc/articles/threads/threads1.html
  • @89899.3K 看起来你在使用 EDT 时遇到了一些问题,真的不了解你的频率和每速率像素,稳定的 Swing 容器对此免疫,你真的尝试了什么,因为这些描述离静态Container很远,???您是否尝试过一些动画或...... ???,我会尝试找出一些必须适用于每个 Native OS 的示例......
  • @89899.3k,你的SSCCE帖子的哪一部分你不明白??????您从未发布任何带有图像或动画的代码。您对问题的口头描述令人困惑。这是你关于这个话题的第四个问题!!!您仍然没有答案,因为您还没有发布我们可以实际用来查看您的问题的代码。如果您需要任何帮助,请发布您的 SSCC!!!

标签: java swing user-interface awt


【解决方案1】:

AWT 与 Swing 有点不同。

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

查看 Learn how to extend the AWT with your own image buttons 了解如何在 AWT 按钮上制作该图像。

【讨论】:

  • 这个链接有用吗?例如:geom.uiuc.edu/java/Kali/ImageButton.java
  • 在另一个答案中,这不是写给 mKorbel 的吗?我不确定它是否有效,我通常处理摆动,而不是 AWT
【解决方案2】:

回到你三天的历史,请尝试在你的本地操作系统上运行下面的代码并使用OpenJDK(也许真的是时候从Oracle下载一个稳定的JDK了,你只能在项目属性中切换它):

  1. 通过从 NetBeans 运行

  2. 编译成 JAR 文件或类

    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();
            }
        }
    }
    

【讨论】:

  • 仍然无法正常工作。一切都像机关枪一样闪烁。例如:gist.github.com/1086865
  • 它的闪烁就像每秒黑色相机预览,然后是 Swing 按钮预览,再次是黑色相机预览,然后再次是 Swing 按钮预览。每秒钟。
  • @89899.3K 同意巨大/大 JButton 在 LCD 显示器上非常闪烁,寻找 Java 的 2D 设置和硬件加速,我的知识到此结束,这真的不是我的 Java 杯
  • 当我点击按钮显示并返回,显示并返回例如:gist.github.com/1086884
  • 这是唯一的原因,我正在寻找 Awt 按钮,因为在我之前的示例中,您可以看到我使用了 Window()、JPanel()、Button()。 JPanel() 允许我透明,Button() 允许我跳过这个闪烁问题,Window() 允许我将预处理背景放在 JWindow() 没有的地方。
猜你喜欢
  • 2011-12-31
  • 2014-01-16
  • 1970-01-01
  • 1970-01-01
  • 2022-01-25
  • 2019-06-15
  • 2018-03-13
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多