我怎样才能让这个 JButton 可见?当我有逐行扫描背景 JWindow() 时?

发布于 2024-11-24 06:59:45 字数 3482 浏览 1 评论 0原文

如何使 JButton 可见?

1) 当没有打开渐进式背景时:JButton 显示

在此处输入图像描述

2) 当没有打开渐进式背景时,按下 JButton 仍不显示闪烁:

在此处输入图像描述

3) 当渐进式背景打开时,JButton 不可见,并且在按下此按钮,我看到闪烁,并且出现 JButton() 并再次隐藏 auto。 <<问题就在这里,那么我该如何解决它呢?

在此处输入图像描述

import java.awt.Color;
import java.awt.AlphaComposite;
import java.awt.BorderLayout;
import java.awt.Graphics;
import javax.swing.*;

import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;

public class ButtonTest extends JWindow implements MouseListener, MouseMotionListener {

    private static final long serialVersionUID = 1L;
    private JFrame frame = new JFrame();
    private SoftJButton softButton1;

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {

            @Override
            public void run() {
                ButtonTest j = new ButtonTest();
                j.createAndShowGUI();
            }
        });
    }

    public void createAndShowGUI() {
        softButton1 = new SoftJButton("Transparent Button");
        softButton1.setBackground(Color.GREEN);
        softButton1.setAlpha(0.5f);
        softButton1.setDoubleBuffered(true);
        this.setLayout(new BorderLayout());
        this.setBounds(100, 30, 200, 100);
        this.setBackground(new Color(0, 0, 0, 255));
        this.setVisible(true);
        add(softButton1);
    }

    @Override
    public void paint(Graphics g) {
        super.paint(g);
    }

    public void mouseDragged(MouseEvent e) {
    }

    public void mousePressed(MouseEvent e) {
    }

    public void mouseMoved(MouseEvent e) {
    }

    public void mouseClicked(MouseEvent e) {
    }

    public void mouseReleased(MouseEvent e) {
    }

    public void mouseEntered(MouseEvent e) {
    }

    public void mouseExited(MouseEvent e) {
    }

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

How can I make the JButton visible?

1) When no progressive background is turned on: JButton is showing

enter image description here

2) When no progressive background is turned on, JButton is pressed still showing no flicker:

enter image description here

3) When progressive background is turned on, JButton is invisible and on pressing in this I see flicker and JButton() appears and again hides auto. << Problem is here, so how can I fix it?

enter image description here

import java.awt.Color;
import java.awt.AlphaComposite;
import java.awt.BorderLayout;
import java.awt.Graphics;
import javax.swing.*;

import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;

public class ButtonTest extends JWindow implements MouseListener, MouseMotionListener {

    private static final long serialVersionUID = 1L;
    private JFrame frame = new JFrame();
    private SoftJButton softButton1;

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {

            @Override
            public void run() {
                ButtonTest j = new ButtonTest();
                j.createAndShowGUI();
            }
        });
    }

    public void createAndShowGUI() {
        softButton1 = new SoftJButton("Transparent Button");
        softButton1.setBackground(Color.GREEN);
        softButton1.setAlpha(0.5f);
        softButton1.setDoubleBuffered(true);
        this.setLayout(new BorderLayout());
        this.setBounds(100, 30, 200, 100);
        this.setBackground(new Color(0, 0, 0, 255));
        this.setVisible(true);
        add(softButton1);
    }

    @Override
    public void paint(Graphics g) {
        super.paint(g);
    }

    public void mouseDragged(MouseEvent e) {
    }

    public void mousePressed(MouseEvent e) {
    }

    public void mouseMoved(MouseEvent e) {
    }

    public void mouseClicked(MouseEvent e) {
    }

    public void mouseReleased(MouseEvent e) {
    }

    public void mouseEntered(MouseEvent e) {
    }

    public void mouseExited(MouseEvent e) {
    }

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

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

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

发布评论

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

评论(2

も让我眼熟你 2024-12-01 06:59:45

目前尚不清楚您的视频源如何工作,但由于 混合重量级和轻量级组件。尽管 awt 组件不透明,但您始终可以在 Frame 上绘制自己的半透明文本并执行手动命中测试,如下所示。您还可以检查您的视频源 API 是否支持 Double缓冲和翻页

在此处输入图像描述

import java.awt.Color;
import java.awt.AlphaComposite;
import java.awt.Font;
import java.awt.FontMetrics;
import java.awt.Frame;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Insets;
import java.awt.Rectangle;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.awt.geom.Rectangle2D;

/** @see http://stackoverflow.com/questions/6725618 */
public class AlphaFrame extends Frame {

    private static final Font font = new Font("Dialog", Font.BOLD, 24);
    private float alpha = 1f;
    private Rectangle rect = new Rectangle();

    public static void main(String[] args) {
        AlphaFrame af = new AlphaFrame();
        af.setTitle("Translucent Button");
        af.setAlpha(0.5f);
        af.setForeground(Color.green);
        af.setBackground(Color.black);
        af.setVisible(true);
    }

    public AlphaFrame() {
        this.setSize(320, 240);
        this.setLocationRelativeTo(null);
        this.setBackground(Color.white);
        this.addWindowListener(new WindowAdapter() {

            @Override
            public void windowClosing(WindowEvent e) {
                System.exit(0);
            }
        });
        this.addMouseListener(new MouseAdapter() {

            @Override
            public void mousePressed(MouseEvent e) {
                if (rect.contains(e.getPoint())) {
                    System.out.println("Pressed.");
                }
            }
        });
        repaint();
    }

    @Override
    public void paint(Graphics g) {
        super.paint(g);
        Graphics2D g2 = (Graphics2D) g;
        g2.setComposite(AlphaComposite.getInstance(
            AlphaComposite.SRC_OVER, alpha));
        g2.setColor(getForeground());
        g2.setFont(font);
        FontMetrics fm = g2.getFontMetrics();
        String s = getTitle();
        Insets i = getInsets();
        int dx = i.left + 10;
        int dy = i.top + fm.getHeight() + 10;
        Rectangle2D bounds = fm.getStringBounds(s, g);
        rect.x = dx + (int) bounds.getX() - 1;
        rect.y = dy + (int) bounds.getY() - 1;
        rect.width = (int) bounds.getWidth() + 1;
        rect.height = (int) bounds.getHeight() + 1;
        System.out.println(i + " \n" + bounds + "\n" + rect);
        g2.drawRect(rect.x, rect.y, rect.width, rect.height);
        g2.drawString(s, dx, dy);
    }

    public float getAlpha() {
        return alpha;
    }

    public void setAlpha(float alpha) {
        this.alpha = alpha;
    }
}

It's not clear how your video source works, but it appears to be incompatible with Swing due to Mixing Heavyweight and Lightweight Components. Although awt components aren't transparent, you can always draw your own translucent text on the Frame and do manual hit testing, as suggested below. You might also check to see if your video source API supports Double Buffering and Page Flipping.

enter image description here

import java.awt.Color;
import java.awt.AlphaComposite;
import java.awt.Font;
import java.awt.FontMetrics;
import java.awt.Frame;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Insets;
import java.awt.Rectangle;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.awt.geom.Rectangle2D;

/** @see http://stackoverflow.com/questions/6725618 */
public class AlphaFrame extends Frame {

    private static final Font font = new Font("Dialog", Font.BOLD, 24);
    private float alpha = 1f;
    private Rectangle rect = new Rectangle();

    public static void main(String[] args) {
        AlphaFrame af = new AlphaFrame();
        af.setTitle("Translucent Button");
        af.setAlpha(0.5f);
        af.setForeground(Color.green);
        af.setBackground(Color.black);
        af.setVisible(true);
    }

    public AlphaFrame() {
        this.setSize(320, 240);
        this.setLocationRelativeTo(null);
        this.setBackground(Color.white);
        this.addWindowListener(new WindowAdapter() {

            @Override
            public void windowClosing(WindowEvent e) {
                System.exit(0);
            }
        });
        this.addMouseListener(new MouseAdapter() {

            @Override
            public void mousePressed(MouseEvent e) {
                if (rect.contains(e.getPoint())) {
                    System.out.println("Pressed.");
                }
            }
        });
        repaint();
    }

    @Override
    public void paint(Graphics g) {
        super.paint(g);
        Graphics2D g2 = (Graphics2D) g;
        g2.setComposite(AlphaComposite.getInstance(
            AlphaComposite.SRC_OVER, alpha));
        g2.setColor(getForeground());
        g2.setFont(font);
        FontMetrics fm = g2.getFontMetrics();
        String s = getTitle();
        Insets i = getInsets();
        int dx = i.left + 10;
        int dy = i.top + fm.getHeight() + 10;
        Rectangle2D bounds = fm.getStringBounds(s, g);
        rect.x = dx + (int) bounds.getX() - 1;
        rect.y = dy + (int) bounds.getY() - 1;
        rect.width = (int) bounds.getWidth() + 1;
        rect.height = (int) bounds.getHeight() + 1;
        System.out.println(i + " \n" + bounds + "\n" + rect);
        g2.drawRect(rect.x, rect.y, rect.width, rect.height);
        g2.drawString(s, dx, dy);
    }

    public float getAlpha() {
        return alpha;
    }

    public void setAlpha(float alpha) {
        this.alpha = alpha;
    }
}
何止钟意 2024-12-01 06:59:45
import java.awt.event.*;
import java.awt.Color;
import java.awt.AlphaComposite;
import java.awt.Dimension;
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 SoftJButton softButton1;

    public void createAndShowGUI() {
        softButton1 = new SoftJButton("Transparent Button");
        softButton1.setPreferredSize(new Dimension(800, 600));
        frame = new JFrame();
        frame.add(softButton1);
        frame.setLocation(150, 150);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.pack();
        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);
            }
        });
        alphaChanger.start();
        Timer uiChanger = new Timer(5500, 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();
        }
    }
}
import java.awt.event.*;
import java.awt.Color;
import java.awt.AlphaComposite;
import java.awt.Dimension;
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 SoftJButton softButton1;

    public void createAndShowGUI() {
        softButton1 = new SoftJButton("Transparent Button");
        softButton1.setPreferredSize(new Dimension(800, 600));
        frame = new JFrame();
        frame.add(softButton1);
        frame.setLocation(150, 150);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.pack();
        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);
            }
        });
        alphaChanger.start();
        Timer uiChanger = new Timer(5500, 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 和您的相关数据。
原文