单击 JTextField 中的图标并清除其内容

2024-01-03

我正在尝试创建一个带有图像和提示的 JTextField。文本字段的功能是一个搜索字段,用于搜索一些书籍。现在,我想走得更远一点。我想赋予图像一个功能。例如,如果我单击图像,文本字段中的文本应该被清除。

为了实现此实现,我创建了一个新类并使用 JTextField 对其进行了扩展。

这是代码:

public class JSearchTextField extends JTextField implements FocusListener {

/**
 * 
 */
private static final long serialVersionUID = 1L;
private String textWhenNotFocused;
private Icon icon;
private Insets dummyInsets;
private JTextField dummy;

public JSearchTextField() {
    super();

    Border border = UIManager.getBorder("TextField.border");
    dummy = new JTextField("Suchen...");
    this.dummyInsets = border.getBorderInsets(dummy);

    icon = new ImageIcon(JSearchTextField.class.getResource("/images/clearsearch.png"));
    this.addFocusListener(this);

}

public JSearchTextField(String textWhenNotFocused) {
    this();
    this.textWhenNotFocused = textWhenNotFocused;
}

public void setIcon(ImageIcon newIcon){
    this.icon = newIcon;
}

public String getTextWhenNotFocused() {
    return this.textWhenNotFocused;
}

public void setTextWhenNotFocused(String newText) {
    this.textWhenNotFocused = newText;
}

public void paintComponent(Graphics g){
    super.paintComponent(g);

    int textX = 2;

    if(!this.hasFocus() && this.getText().equals("")) {
        int height = this.getHeight();
        Font prev = this.getFont();
        Font italic = prev.deriveFont(Font.ITALIC);
        Color prevColor = g.getColor();
        g.setFont(italic);
        g.setColor(UIManager.getColor("textInactiveText"));
        int h = g.getFontMetrics().getHeight();
        int textBottom = (height - h) / 2 + h - 4;
        int x = this.getInsets().left;
        Graphics2D g2d = (Graphics2D) g;
        RenderingHints hints = g2d.getRenderingHints();
        g2d.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, 
                             RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
        g2d.drawString(textWhenNotFocused, x, textBottom);
        g2d.setRenderingHints(hints);
        g.setFont(prev);
        g.setColor(prevColor);
    } else {
        int iconWidth = icon.getIconWidth();
        int iconHeight = icon.getIconHeight();
        int x = dummy.getWidth() + dummyInsets.right;
        textX = x - 420;
        int y = (this.getHeight() - iconHeight)/2;
        icon.paintIcon(this, g, x, y);
    }

    setMargin(new Insets(2, textX, 2, 2));

}
@Override
public void focusGained(FocusEvent arg0) {
    this.repaint();
}

@Override
public void focusLost(FocusEvent arg0) {
    this.repaint();
}

}

这就是我创建字段的地方;

txtSearchBooks = new JSearchTextField("Buch suchen...");

现在回到我的问题。您知道如何为图像提供自动清除文本的功能吗?我尝试实现 MouseListener 并将“txtSearchBooks”的文本设置为 null,但没有成功。

我希望我没有走错方向。

抱歉发了这么长的帖子,但我真的很感激能得到一些建议。


A JTextField is a JComponent,这意味着它也是其他组件的容器。您可以使用add(Component c)方法向其添加其他组件。但是一个JTextField除非您提供一个,否则不会显示其添加的组件LayoutManager到它。然后它的行为就像正常的一样JPanel.

我做了一个小例子,告诉你如何管理你需要的东西。标签显示在右侧,单击它将清除该字段。您也可以使用按钮来代替标签。

请注意,您不需要创建Image像我一样从头开始对象,您可以从文件加载它。我以这种方式创建它,以便该示例不依赖于其他文件。

public class TextFieldWithLabel {
    public static void main(String[] args) 
    {
        JFrame frame = new JFrame();
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        final JTextField textField = new JTextField("Search...");
        textField.setLayout(new BorderLayout());

        //creating dummy image...
        Image image = new BufferedImage(25, 25, BufferedImage.TYPE_INT_RGB);
        Graphics graphics = image.getGraphics();
        graphics.setColor(Color.WHITE);
        graphics.fillRect(0, 0, 25, 25);
        graphics.setColor(Color.RED);
        graphics.fillRect(2, 11, 21, 3);
        graphics.fillRect(11, 2, 3, 21);

        JLabel label = new JLabel(new ImageIcon(image));
        textField.add(label, BorderLayout.EAST);
        label.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                textField.setText("");
            }
        });
        frame.add(textField);
        frame.pack();
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
    }
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

单击 JTextField 中的图标并清除其内容 的相关文章

随机推荐