半透明文本框

3
您如何将JTextField设置为半透明?我需要文本框的背景有50%的透明度。我已经尝试过textField.setOpaque(false)然后textField.setBackground(new Color(0,0,0,128)),但结果完全透明。我还尝试了不设置不透明度的方法,但每次输入时文本框都会变得越来越不透明。

这是为游戏中的抬头显示器而设计的,因此用户需要能够在一定程度上透过它看到东西,但他们也需要能够确定它的位置(仅有边框会让它看起来像一个盒子)。 SSCCE 马上就来... - jedyobidan
2个回答

6

在绘图链的某个阶段,您需要控制一下。

最简单的方法是覆盖文本字段的paint方法,并调整图形组合以提供所需的透明度。

这会带来一些额外的问题。文本字段必须是透明的(不透明 == false),否则您将产生重绘伪像。您还需要手动填充字段的背景,因为将字段设置为透明会停止其自己绘制背景...

enter image description here

import java.awt.AlphaComposite;
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.GridBagLayout;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

public class TestTransparentTextField {

    public static void main(String[] args) {
        new TestTransparentTextField();
    }

    public TestTransparentTextField() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                }

                JFrame frame = new JFrame("Testing");
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.setLayout(new BorderLayout());
                frame.add(new TestPane());
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }

    public class TestPane extends JPanel {

        private BufferedImage background;

        public TestPane() {
            setLayout(new GridBagLayout());
            add(new TransparentTextField("Look ma, no opacity!", 20));
            try {
                background = ImageIO.read(new File("/get/your/own/image"));
            } catch (IOException ex) {
                ex.printStackTrace();
            }
        }

        @Override
        public Dimension getPreferredSize() {
            return new Dimension(200, 200);
        }

        @Override
        protected void paintComponent(Graphics g) {
            super.paintComponent(g);
            if (background != null) {
                Graphics2D g2d = (Graphics2D) g.create();
                int x = (getWidth() - background.getWidth()) / 2;
                int y = (getHeight()- background.getHeight()) / 2;
                g2d.drawImage(background, x, y, this);
                g2d.dispose();
            }
        }
    }

    public class TransparentTextField extends JTextField {

        public TransparentTextField(String text) {
            super(text);
            init();
        }

        public TransparentTextField(int columns) {
            super(columns);
            init();
        }

        public TransparentTextField(String text, int columns) {
            super(text, columns);
            init();
        }

        protected void init() {
            setOpaque(false);
        }

        @Override
        public void paint(Graphics g) {
            Graphics2D g2d = (Graphics2D) g.create();
            g2d.setComposite(AlphaComposite.SrcOver.derive(0.5f));
            super.paint(g2d);
            g2d.dispose();
        }

        @Override
        protected void paintComponent(Graphics g) {
            Graphics2D g2d = (Graphics2D) g.create();
            g2d.setColor(getBackground());
            g2d.fillRect(0, 0, getWidth(), getHeight());
            super.paintComponent(g2d);
            g2d.dispose();
        }

    }
}

+1,这种方法(覆盖paint方法)使得背景和文本都变成了透明的。 - camickr

4
我也尝试了不将 opaque 设置为 false,但是每次输入后 textField 的透明度都会降低。

请参见带有透明度的背景,了解原因及解决方案。


我理解为什么会发生这种情况,但是有没有解决方法? - jedyobidan
你为什么要提问呢,如果你连整个答案都不愿意读并跟着链接走的话? - camickr
我之前看过那个页面,但我不太理解Alpha Container。每当我向GUI添加一个半透明组件时,我是不是就需要构建一个新的AlphaContainer? 对了,我忘记阅读你回答中的“以及解决方案”部分了;对此我感到抱歉。 - jedyobidan

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接