与背景图象的透明文本领域

时间:2014-07-01 00:16:02

标签: java swing jtextfield

我想了解如何将图片设置为 JTextField ,如果透明,或者如何让我的JTextField看起来像这样:

JTextField's background image

以下是我如何让我的字段透明,但我无法设置其背景图片:

//Step 1: Remove the border line to make it look like a flat surface.  
field.setBorder(BorderFactory.createLineBorder(Color.white, 0));  

//Step 2: Set the background color to null to remove the background.  
field.setBackground(null);  

1 个答案:

答案 0 :(得分:2)

所有Swing组件都有透明概念,可通过使用opaque属性来控制。将背景设置为null帐篷,将字段的背景颜色设置为其默认的UI。

话虽如此,一些组件可以忽略这一点(部分或全部)。在这种情况下,我们可以作弊......

在下面的示例中,通过opaque属性将字段设置为透明,这很重要,因为RepaintManager不会在组件后面绘制区域,除非它们是透明的,并使用完全透明的背景色

Field

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.EventQueue;
import java.awt.GridBagLayout;
import javax.swing.JFrame;
import javax.swing.JTextField;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

public class TestTextField {

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

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

                JTextField field = new JTextField(20);
                field.setBackground(new Color(0, 0, 0, 0));
                field.setOpaque(false);

                JFrame frame = new JFrame("Testing");
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.getContentPane().setBackground(Color.RED);
                frame.setLayout(new GridBagLayout());
                frame.add(field);
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }

}

根据评论更新...

这是一个非常具体的示例,旨在直接回答所提出的问题。基本上,这样做会创建一个自定义边框,并使用Image来渲染"边框"

Border example

import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.image.BufferedImage;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JTextField;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import javax.swing.border.AbstractBorder;
import javax.swing.border.Border;
import javax.swing.border.MatteBorder;

public class TestTextField {

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

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

                JTextField field = new JTextField(20);
                try {
                    BufferedImage img = ImageIO.read(getClass().getResource("/FieldBorder.png"));
                    field.setBorder(new ImageBorder(img, 8, 6));
                } catch (IOException ex) {
                    ex.printStackTrace();
                }

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

    public class ImageBorder implements Border {

        private BufferedImage img;
        private int bottomMargin;
        private int leftMargin;

        public ImageBorder(BufferedImage img, int leftMargin, int bottomMargin) {
            this.img = img;
            this.bottomMargin = bottomMargin;
            this.leftMargin = leftMargin;
        }

        @Override
        public void paintBorder(Component c, Graphics g, int x, int y, int width, int height) {
            g.drawImage(img, x, y + height - img.getHeight(), c);
        }

        @Override
        public Insets getBorderInsets(Component c) {
            return new Insets(0, leftMargin, bottomMargin, 0);
        }

        @Override
        public boolean isBorderOpaque() {
            return false;
        }

    }

}

现在,也可以使用自定义Border中的自定义绘图来完成此操作,但这种方式更快;)

另一种选择......

只需使用JPanel并添加字段和JLabel将边框轮廓保持在一起,例如......

CompoundField

import java.awt.Color;
import java.awt.Component;
import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.image.BufferedImage;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import javax.swing.border.Border;

public class TestTextField {

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

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

                JTextField field = new JTextField(20);
                field.setBorder(null);

                JPanel fieldPane = new JPanel(new GridBagLayout());
                fieldPane.setBackground(Color.WHITE);
                GridBagConstraints gbc = new GridBagConstraints();
                gbc.gridwidth = GridBagConstraints.REMAINDER;
                gbc.anchor = GridBagConstraints.WEST;
                gbc.insets = new Insets(0, 8, 0, 0);
                fieldPane.add(field, gbc);

                try {
                    BufferedImage img = ImageIO.read(getClass().getResource("/FieldBorder.png"));
                    gbc.insets = new Insets(0, 0, 0, 0);
                    fieldPane.add(new JLabel(new ImageIcon(img)), gbc);
                } catch (IOException ex) {
                    ex.printStackTrace();
                }

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

}

但这将归结为需求和要求......

带背景的文字字段

Textfield with background

import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.GridBagLayout;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.imageio.ImageIO;
import javax.swing.JFrame;
import javax.swing.JTextField;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import javax.swing.text.Document;

public class TextFieldBackground {

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

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

                TextFieldWithBackground field = new TextFieldWithBackground(40);
                try {
                    field.setBackgroundImage(ImageIO.read(getClass().getResource("/clouds.jpg")));
                } catch (IOException ex) {
                    ex.printStackTrace();
                }

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

    public class TextFieldWithBackground extends JTextField {

        private BufferedImage bg;

        public TextFieldWithBackground() {
        }

        public TextFieldWithBackground(String text) {
            super(text);
        }

        public TextFieldWithBackground(int columns) {
            super(columns);
        }

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

        public TextFieldWithBackground(Document doc, String text, int columns) {
            super(doc, text, columns);
        }

        public void setBackgroundImage(BufferedImage bg) {
            this.bg = bg;
            setOpaque(bg == null);
            repaint();
        }

        @Override
        protected void paintComponent(Graphics g) {
            if (bg != null) {
                int x = 0;
                int y = (getHeight() - bg.getHeight()) / 2;
                while (x < getWidth()) {
                    g.drawImage(bg, x, y, this);
                    x += bg.getWidth();
                }
            }
            super.paintComponent(g);
        }

    }

}