用图像替换JTextField? [英] Replace JTextField with an Image?

查看:139
本文介绍了用图像替换JTextField?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在寻找用图像&替换JTextField的方法。什么都没有出现。是否可以在图像上添加jtextfield或用图像替换?

I've been searching on way's to replace a JTextField with an Image & nothing comes up. Is it possible to add a jtextfield onto an image or replace on with an image?

我正在使用JPanel&我想把JtextField放在下面的图片中:

I'm using a JPanel & I am trying to put the JtextField inside the image below:

推荐答案

有几种方法可以实现......

There are several ways this might be achieved...

最简单的可能是使用 JLabel ,将它的布局管理器设置为 BorderLayout 并简单地将文本字段添加到它...

The simplest might be to use a JLabel, set it's layout manager to something BorderLayout and simply add the text field to it...

JTextField field = new JTextField();
field.setOpaque(false);
JLabel label = new JLabel();
label.setIcon(...);
label.setLayout(new BorderLayout());
label.add(field);

例如......

这个将生成......

This will generate...

如果您希望文本字段是透明的,可以添加 field.setBackground(new Color(0,0,0,0)) ; ,生成......

If you prefer the text field to be transparent, you could add field.setBackground(new Color(0, 0, 0, 0));, which generates...

如果您不想要边框,请添加 field.setBorder(null); ,生成......

And if you don't want the border, add field.setBorder(null);, which generates...

使用其他示例更新...

或者你可能更喜欢自给自足的课程......

Or perhaps you'd prefer a self contained class...

这有点棘手,因为 paintComponent 不仅描绘了背景,但文本,你需要一些方法来插入文本下的背景。为此,我实际上并没有调用 super.paintComponent ,而是我们作弊,并调用 getUI()。paint 直接。这确保了字段的文本被绘制,但允许我们在它之前绘制我们自己的背景...

This is a little tricky, because paintComponent not only paints the background, but the text, you need some way to insert the background under the text. To this end, I don't actually call super.paintComponent, but instead, we cheat, and call getUI().paint directly. This ensures that the text of the field is painted, but allows us to paint our own background before it...

import java.awt.Color;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.JFrame;
import javax.swing.JTextField;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import javax.swing.border.EmptyBorder;

public class BackgroundTextField {

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

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

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

    public class TextFieldBackground extends JTextField {

        private BufferedImage img;

        public TextFieldBackground() {
            setColumns(10);
            setOpaque(false);
            setBorder(new EmptyBorder(2, 2, 2, 2));
            setBackground(new Color(0, 0, 0, 0));
            try {
                img = ImageIO.read(getClass().getResource("/y9yBe.jpg"));
            } catch (IOException ex) {
                ex.printStackTrace();
            }
        }

        @Override
        public Dimension getPreferredSize() {
            return img == null ? super.getPreferredSize() : new Dimension(img.getWidth(), img.getHeight());
        }

        @Override
        protected void paintComponent(Graphics g) {
            Graphics2D g2d = (Graphics2D) g.create();
            g2d.drawImage(img, 0, 0, this);
            getUI().paint(g2d, this);
            g2d.dispose();
        }

    }

}

这有限制,字段永远不应该大于图像,因为图像不会重复,但我相信你可以使用 for-loop 将一堆图像拼接在一起......

There are limitations to this, the field should never be bigger then the image, as the image is not repeated, but I'm sure you could use a for-loop to stitch a bunch of images together...

这篇关于用图像替换JTextField?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆