本文介绍了我怎样才能让这个JButton的可见?当我有逐行扫描背景的JWindow()?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何才能将JButton可见?

1)当没有渐进背景接通:JButton的是显示

2)如果没有进步的背景下被打开,一个JButton是pressed仍呈现无频闪:

3)当渐进背景被接通,JButton的是在此我看到闪烁和的JButton()出现,并再次隐藏汽车隐形和pressing。 <<问题是在这里,所以我怎么能解决这个问题?

 进口java.awt.Color中;
进口java.awt.AlphaComposite中;
进口java.awt.BorderLayout中;
进口java.awt.Graphics;
进口的javax.swing *。进口java.awt.event.MouseEvent中;
进口java.awt.event.MouseListener;
进口java.awt.event.MouseMotionListener;公共类ButtonTest扩展的JWindow实现的MouseListener,{的MouseMotionListener    私有静态最后的serialVersionUID长1L =;
    私人的JFrame框架=新的JFrame();
    私人SoftJButton softButton1;    公共静态无效的主要(字串[] args){
        SwingUtilities.invokeLater(Runnable的新(){            @覆盖
            公共无效的run(){
                ButtonTest J =新ButtonTest();
                j.createAndShowGUI();
            }
        });
    }    公共无效createAndShowGUI(){
        softButton1 =新SoftJButton(透明按钮);
        softButton1.setBackground(Color.GREEN);
        softButton1.setAlpha(0.5F);
        softButton1.setDoubleBuffered(真);
        this.setLayout(新的BorderLayout());
        this.setBounds(100,30,200,100);
        this.setBackground(新色彩(0,0,0,255));
        this.setVisible(真);
        加(softButton1);
    }    @覆盖
    公共无效漆(图形G){
        super.paint(G);
    }    公共无效的mouseDragged(的MouseEvent E){
    }    公共无效鼠标pressed(的MouseEvent E){
    }    公共无效的mouseMoved(的MouseEvent E){
    }    公共无效的mouseClicked(的MouseEvent E){
    }    公共无效的mouseReleased(的MouseEvent E){
    }    公共无效的mouseEntered(的MouseEvent E){
    }    公共无效的mouseExited(的MouseEvent E){
    }    公共静态类SoftJButton扩展的JButton {        私有静态最后一个JButton lafDeterminer =的新的JButton();
        私有静态最后的serialVersionUID长1L =;
        私人布尔rectangularLAF;
        私人浮动阿尔法= 1F;        公共SoftJButton(){
            这个(NULL,NULL);
        }        公共SoftJButton(字符串文本){
            此(文本,NULL);
        }        公共SoftJButton(字符串文本,图标图标){
            超(文字,图标);
            setOpaque(假);
            setFocusPainted(假);
        }        公众持股量getAlpha(){
            阿尔法回报;
        }        公共无效setAlpha(浮动阿尔法){
            this.alpha =阿尔法;
            重绘();
        }        @覆盖
        公共无效的paintComponent(java.awt.Graphics G){
            java.awt.Graphics2D中的G2 =(java.awt.Graphics2D中)克;
            g2.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER,阿尔法));
            如果(rectangularLAF&放大器;&放大器; isBackgroundSet()){
                颜色C =的getBackground();
                g2.setColor(C);
                g.fillRect(0,0,的getWidth(),的getHeight());
            }
            super.paintComponent方法(G2);
        }        @覆盖
        方法public void updateUI(){
            super.updateUI();
            lafDeterminer.updateUI();
            rectangularLAF = lafDeterminer.isOpaque();
        }
    }
}


解决方案

您的视频源是如何工作的,但它似乎是与Swing不兼容因的。虽然 AWT 组件不是透明的,你总是可以借鉴自己半透明的文字和做手工命中测试,如下建议。你也可以检查,看看你的视频源API支持。

 进口java.awt.Color中;
进口java.awt.AlphaComposite中;
进口java.awt.Font中;
进口java.awt.FontMetrics中;
进口java.awt.Frame中;
进口java.awt.Graphics;
进口java.awt.Graphics2D中;
进口java.awt.Insets中;
进口java.awt.Rectangle中;
进口java.awt.event.MouseAdapter;
进口java.awt.event.MouseEvent中;
进口java.awt.event.WindowAdapter中;
进口java.awt.event.WindowEvent中;
进口java.awt.geom.Rectangle2D中;/ ** @see http://stackoverflow.com/questions/6725618 * /
公共类AlphaFrame扩展框架{    私有静态最后字体的字体=新的字体(对话,Font.BOLD,24);
    私人浮动阿尔法= 1F;
    私人矩形RECT =新的Rectangle();    公共静态无效的主要(字串[] args){
        AlphaFrame AF =新AlphaFrame();
        af.setTitle(半透明按钮);
        af.setAlpha(0.5F);
        af.setForeground(Color.green);
        af.setBackground(Color.black);
        af.setVisible(真);
    }    公共AlphaFrame(){
        this.setSize(320,240);
        this.setLocationRelativeTo(NULL);
        this.setBackground(Color.white);
        this.addWindowListener(新WindowAdapter的(){            @覆盖
            公共无效的windowClosing(WindowEvent五){
                System.exit(0);
            }
        });
        this.addMouseListener(新MouseAdapter(){            @覆盖
            公共无效鼠标pressed(的MouseEvent E){
                如果(rect.contains(e.getPoint())){
                    的System.out.println(pressed。);
                }
            }
        });
        重绘();
    }    @覆盖
    公共无效漆(图形G){
        super.paint(G);
        Graphics2D的G2 =(Graphics2D的)克;
        g2.setComposite(AlphaComposite.getInstance(
            AlphaComposite.SRC_OVER,阿尔法));
        g2.setColor(getForeground());
        g2.setFont(字体);
        FontMetrics对象FM = g2.getFontMetrics();
        字符串s =的getTitle();
        插图I =的getInsets();
        INT DX = i.left + 10;
        INT DY = i.top + fm.getHeight()+ 10;
        矩形边界= 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(我+\\ n+界限+\\ n+ RECT);
        g2.drawRect(rect.x,rect.y,rect.width,rect.height);
        g2.drawString(S,DX,DY);
    }    公众持股量getAlpha(){
        阿尔法回报;
    }    公共无效setAlpha(浮动阿尔法){
        this.alpha =阿尔法;
    }
}

How can I make the JButton visible?

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

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

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?

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();
        }
    }
}
解决方案

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.

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

这篇关于我怎样才能让这个JButton的可见?当我有逐行扫描背景的JWindow()?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-23 12:16