如何在不可编辑的 JTextField 中显示图像?

发布于 2024-12-09 18:31:22 字数 313 浏览 6 评论 0原文

在我的 Java Swing 应用程序中,我想在不可编辑的 JTextField 中放置一个锁定图像,如下所示:

Locked JTextField

我创建了一个 JTextField,并在其上方插入了一个 JLabel,并为 JLabel 定义了锁定图标。如果 JTextField 是可编辑的,那么 JLabel 将显示正常,如上图所示,但如果 JTextField 不可编辑,则根本不会显示图像。

我该如何解决这个问题?

In my Java Swing application i want to put a lock image inside a JTextField that is not editable, to appear like this :

Locked JTextField

I have created a JTextField, and inserted a JLabel above it and defined the lock icon for the JLabel. If the JTextField is editable then the JLabel appears fine as the image above shows, but if the JTextField is not editable then the image does not appear at all.

How can i fix that ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

千と千尋 2024-12-16 18:31:22

您可以尝试在面板中添加标签(用于图标)和文本字段。从文本字段中删除边框并在面板周围添加公共边框。将背景设置为与文本字段的背景相同。

You can try to add both label (for icon) and the textfield in a panel. Remove border from the textfield and add a common border around the panel. Set background to be the same as the textfield's background.

岛徒 2024-12-16 18:31:22

为什么不使用 jTextPane ?

try {
    // Get the text pane's document
    JTextPane textPane = new JTextPane();
    StyledDocument doc = (StyledDocument)textPane.getDocument();

    // The image must first be wrapped in a style
    Style style = doc.addStyle("StyleName", null);
    StyleConstants.setIcon(style, new ImageIcon("imagefile"));

    // Insert the image at the end of the text
    doc.insertString(doc.getLength(), "ignored text", style);
} catch (BadLocationException e) {
}

Why dont you use a jTextPane ?

try {
    // Get the text pane's document
    JTextPane textPane = new JTextPane();
    StyledDocument doc = (StyledDocument)textPane.getDocument();

    // The image must first be wrapped in a style
    Style style = doc.addStyle("StyleName", null);
    StyleConstants.setIcon(style, new ImageIcon("imagefile"));

    // Insert the image at the end of the text
    doc.insertString(doc.getLength(), "ignored text", style);
} catch (BadLocationException e) {
}
木緿 2024-12-16 18:31:22

编写自己的扩展 JTextField 的类,并且在该类中,您必须覆盖 paintComponent(Graphics g)

1) 仔细使用 Icon 的位置,

因为

2)将您的 Custom JTextField 放入 resizibale Container,尝试 Icon 是否保持在右侧静止状态,以及调整大小对于 Custom JTextField< 是否正常工作/代码> 与里面的 Icon

3) 使用 IconsetEditable(true)setEditable(false) 创建构造函数

write own Class that extends JTextField and inside this class you have to overide paintComponent(Graphics g)

1) carefully with positions for Icon

because

2) put your Custom JTextField to the resizibale Container, try if Icon stay still on right side, if resize works correctly for Custom JTextField with Icon inside,

3) create constructor for setEditable(true) and setEditable(false) with Icon

夜血缘 2024-12-16 18:31:22

创建一个自定义 Border 让我们将其命名为 IconBorder。查看 MatteIcon 的源代码,然后将其自定义为仅绘制单个图像。然后,您可以使用如下代码将边框添加到文本字段:

Border border = new CompoundBorder(textField.getBorder(), new IconBorder(...));
textField.setBorder( border );

Create a custom Border lets call it IconBorder. Take a look at the source code for MatteIcon and then customize it to only paint a single image. Then you would add the Border to the text field with code like:

Border border = new CompoundBorder(textField.getBorder(), new IconBorder(...));
textField.setBorder( border );
木有鱼丸 2024-12-16 18:31:22

您使用的是 Java 7 吗?然后使用 JLayeredPane

Are you using Java 7? Then use a JLayeredPane.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文