我如何在自定义JTextField中绘制光标?

问题描述 投票:0回答:1

我创建了一个四舍五入的JTextField,它覆盖了paintComponent(g: Graphics)方法。形状和文本可以正确绘制,但是我还没有实现任何显示光标的东西。

我该如何实现?

编辑:super.paintComponent(...)不是解决方案。如果我使用它,则可以看到其他已绘制组件的边缘。

到目前为止,这是代码(没有描述任何呈现光标的内容!]

@Override
protected void paintComponent(Graphics g) {

    //TODO entfernen
    //super.paintComponent(g);

    if(g instanceof Graphics2D) {
        Graphics2D graphics = (Graphics2D) g;
        graphics.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);

        //Draw button background
        graphics.setColor(getBackground());
        graphics.fillRoundRect(0, 0, getWidth()-1, getHeight()-1, arcRadius, arcRadius);

        this.paintText(graphics);

    }
}

protected final void paintText(@NotNull Graphics2D g) {
    //Draw font
    g.setColor(getForeground());
    if (this.getFont() != null && this.getText() != null) {
        FontMetrics fm = getFontMetrics(getFont());
        g.setColor(this.getForeground());
        g.drawString(this.getText(), ((this.getWidth() / 2) - (fm.stringWidth(this.getText()) / 2)),
                ((this.getHeight() / 2) + fm.getMaxDescent()));
    }
}

编辑2:当我调用super.paintComponent(...)时,结果如下:

super component visible

如您所见,超级组件可见。这就是为什么,我不调用super方法。

有人对Carets有经验吗?非常确定这是正确的方法...

java jtextfield
1个回答
0
投票

调用super.paintComponent(Graphics g)绝对没错!

与OP不同,我将提供一个最小的,可运行的示例,该示例显示带有圆角的自定义JTextField。

首先,这是我必须创建的GUI。

Sample GUI

您可以看到,黄色的自定义JTextField具有圆角。

这是我所做的最小的,可运行的示例。

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.FontMetrics;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;

public class CustomJTextField implements Runnable {

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new CustomJTextField());
    }

    @Override
    public void run() {
        JFrame frame = new JFrame("Custom JTextField");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        JPanel panel = new JPanel();
        panel.setLayout(new BorderLayout());
        panel.setPreferredSize(new Dimension(400, 160));

        Font font = panel.getFont().deriveFont(32F);

        JLabel label = new JLabel("Something");
        label.setHorizontalAlignment(JLabel.CENTER);
        label.setFont(font);
        panel.add(label, BorderLayout.BEFORE_FIRST_LINE);

        QTextField sampleField = new QTextField(panel, 10);
        sampleField.setBackground(Color.YELLOW);
        sampleField.setFont(font);
        panel.add(sampleField, BorderLayout.CENTER);

        frame.add(panel);
        frame.pack();
        frame.setLocationByPlatform(true);
        frame.setVisible(true);
    }

    public class QTextField extends JTextField {

        private static final long serialVersionUID = 1L;

        private int arcRadius = 40;

        private Container container;

        public QTextField(Container container, int length) {
            super(length);
            this.container = container;
        }

        @Override
        protected void paintComponent(Graphics g) {
            super.paintComponent(g);

            Graphics2D graphics = (Graphics2D) g;
            graphics.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);

            // Draw button background
            graphics.setColor(container.getBackground());
            graphics.fillRoundRect(0, 0, getWidth(), getHeight(), arcRadius, arcRadius);
            graphics.setColor(getBackground());
            graphics.fillRoundRect(20, 20, getWidth() - 40, getHeight() - 40, arcRadius, arcRadius);

            this.paintText(graphics);

        }

        private void paintText(Graphics2D g) {
            // Draw font
            g.setColor(getForeground());
            if (this.getFont() != null && this.getText() != null) {
                FontMetrics fm = getFontMetrics(getFont());
                g.setColor(this.getForeground());
                g.drawString(this.getText(), ((this.getWidth() / 2) - (fm.stringWidth(this.getText()) / 2)),
                        ((this.getHeight() / 2) + fm.getMaxDescent()));
            }
        }
    }

}
© www.soinside.com 2019 - 2024. All rights reserved.