简体   繁体   English

覆盖JTextField中的paint方法以绘制文本

[英]Overriding the paint method in JTextField to draw text

I am wishing to draw a number onto a JTextField by overwriting the paint method. 我希望通过覆盖paint方法在JTextField上绘制一个数字。 So that when the user edits the text field the number doesn't disappear. 这样,当用户编辑文本字段时,数字不会消失。 However, at the moment, the number isn't appearing at all, I have tried: 但是,目前,这个数字根本没有出现,我尝试过:

public void paintComponent(Graphics g) {  
     super.paintComponent(g);
     if(number != 0){
            g.setColor(Color.RED);
            g.drawString(String.valueOf(number),0,0);
     }  
}  

Any ideas, is this even possible? 有什么想法,这可能吗?

Try to play with Y position in the g.drawString(String.valueOf(number),0,0); 尝试在g.drawString(String.valueOf(number),0,0); Y位置播放g.drawString(String.valueOf(number),0,0); call. 呼叫。 Eg use getHeight()/2 例如使用getHeight()/2

..when the user edits the text field the number doesn't disappear. ..当用户编辑文本字段时,数字不会消失。

As pointed out by @mKorbel, there is no need to override a JTextField in order to get red numbers, simply configure it using the public methods. 正如@mKorbel指出的那样,无需重写JTextField即可获取红色数字,只需使用公共方法对其进行配置即可。 OTOH.. OTOH ..

g.drawString(String.valueOf(number),0,0);

If this is really all about numbers, perhaps the best approach is to use a JSpinner with a SpinnerNumberModel , and set a custom SpinnerUI . 如果这真的与数字有关,那么最好的方法可能是将JSpinnerSpinnerNumberModel一起使用,并设置自定义SpinnerUI

maybe there no reason override paintComponent() for JTextField , instead of use 也许没有理由重写JTextField的 paintComponent()而不是使用

  • JTextField.setBackground()

  • JTextField.setForeground()

  • JTextField.setFont()

  • JTextField.setHorizontalAlignment(javax.swing.SwingConstants.LEFT)

  • some hacks are possible by put there Html colored or special formatted text 通过将Html彩色或特殊格式的文本放到此处,可能会引起一些黑客攻击

EDIT 编辑

maybe this question is about 也许这个问题是关于

  • filtering KeyEvents in the Document / DocumentListener Document / DocumentListener过滤KeyEvents

or 要么

  • JFormattedTextField with Number Formatter 具有Number Formatter JFormattedTextField

Why don't you just add a small JLabel to the front of the JTextField ? 您为什么不只在JTextField的前面添加一个小的JLabel The JLabel could contain the number, and because it isn't editable it will always be there no matter what the user changes in the JTextField . JLabel可以包含数字,并且因为它不可编辑,所以无论用户在JTextField什么更改,它都将始终存在。 You could also format the JLabel to make it red by calling setForeground(Color.RED); 您还可以通过调用setForeground(Color.RED);JLabel格式化为红色setForeground(Color.RED); . This might be a much simpler solution? 这可能是一个更简单的解决方案?

For example, instead of doing this... 例如,不要这样做...

JPanel panel = new JPanel(new BorderLayout());
JTextField textfield = new JTextField("Hello");
panel.add(textfield,BorderLayout.CENTER);

You might do something like this... 你可能会做这样的事情...

JPanel panel = new JPanel(new BorderLayout());

JTextField textfield = new JTextField("Hello");
panel.add(textfield,BorderLayout.CENTER);

JLabel label = new JLabel("1.");
label.setForeground(Color.RED);
panel.add(label,BorderLayout.WEST);

Which adds a red JLabel to the left of the JTextField , and because you're using BorderLayout for the JPanel then it automatically makes the JLabel the smallest it can possibly be. 这会在JTextField的左侧添加一个红色的JLabel ,并且因为您正在为JPanel使用BorderLayout ,所以它会自动使JLabel尽可能的小。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM