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

标签 java linux swing user-interface awt

如何使 JButton 可见?

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

enter image description here

2)当没有开启渐进背景时,按下JButton仍然没有闪烁:

enter image description here

3) 当渐进式背景打开时,JButton 是不可见的,按下时我看到闪烁,JButton() 出现并再次隐藏自动。 << 问题来了,我该如何解决?

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

最佳答案

不清楚您的视频源是如何工作的,但由于 Mixing Heavyweight and Lightweight Components,它似乎与 Swing 不兼容。 .虽然 awt 组件不是透明的,但您始终可以在 Frame 上绘制自己的半透明文本并进行手动 HitTest ,如下所示。您还可以检查您的视频源 API 是否支持 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;
    }
}

关于java - 我怎样才能使这个 JButton 可见?当我有逐行扫描背景 JWindow() 时?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6725618/

相关文章:

java - 动态更新 swing JXMapKit 中的标记

java - Android,volley : When using volley, 既没有调用 Errorlistener,也没有调用 Responselistener

java - 通过知道名称选择 JLabel

java - 在 Android 中使用 CustomMultiPartEntity 取消上传文件

python kill script.py 名称不是 PID

linux - 在 bash 中多次调用 getopts

c - 如何禁止从 Linux 内核模块访问键盘和鼠标?

java - Java 中的操作停止工作

java - 物理设备测试和 google-app-engine 后端

java - 使用 Java 8 流 API 创建字符串映射和排序列表