半透明文本字段

时间:2013-05-01 02:35:09

标签: java swing transparency

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

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

2 个答案:

答案 0 :(得分:5)

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

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

这还有一些其他问题。文本字段必须是透明的(opaque == 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 :(得分:4)

  

我也试过没有将opaque设置为false,每次键入时textField变得不那么透明。

请参阅Background With Transparency,了解为何会出现这种情况并提供解决方案。