[英]Java Swing: Display validation rule to user on JPanel
I have a JPanel which contains a number of JTextField(s) that have a length limit of a few characters (implemented by a custom PlainDocument).我有一个 JPanel,其中包含许多长度限制为几个字符的 JTextField(由自定义 PlainDocument 实现)。
I want to display a text in red font at the top left of the panel saying, "**Fields allow a max of 20 characters.".我想在面板的左上角显示一个红色字体的文本,上面写着“**字段最多允许 20 个字符。”。
Two options that I am considering so far is adding a TitledBorder or a JLabel.到目前为止,我正在考虑的两个选项是添加 TitledBorder 或 JLabel。 What is the best way to do this?做这个的最好方式是什么?
Thank you.谢谢你。
You can add one more JLabel to the TextField, wich you can setVisible, when needed.您可以在 TextField 中再添加一个 JLabel,以便在需要时设置 Visible。
JTextField t = new JTextField();
t.setBounds(0, 0, 200, 75);
frame.add(t);
JLabel l = new JLabel();
l.setText("only 20 Character");
l.setForeground(Color.red);
l.setBounds(0, 0, 100, 25);
t.add(l);
Thats how I would do it.我就是这样做的。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.