3

你如何使 JTextField 半透明?我需要大约 50% 的 alpha 作为文本字段的背景。

我试过textField.setOpaque(false)textField.setBackground(new Color(0,0,0,128)),但它最终完全透明。我也尝试过没有将 opaque 设置为 false 并且每次键入时 textField 变得不那么透明。

4

2 回答 2

5

在油漆链的某个时刻,您需要控制。

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

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

在此处输入图像描述

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

    }
}
于 2013-05-01T02:49:59.253 回答
4

I also tried it without setting opaque to false and the textField became less transparent every time you type.

See Background With Transparency for an explanation why this happens and a solution.

于 2013-05-01T02:38:10.923 回答