[英]Java: Add Place Holder on JTextField
Is there a way or method in which we can add placeholder in j text field. 有没有一种方法或方法可以在j文本字段中添加占位符。 I want to add placeholder "Enter Your Number" in field but how can I do this.
我想在字段中添加占位符“输入您的号码”,但我该怎么做呢。 I check all methods but didn't working.
我检查所有方法但没有工作。
Code: 码:
public class Loop extends JFrame{
private JTextField t1;
public L(){
getContentPane().setLayout(null);
t1=new JTextField();
t1.setBounds(27,50,47,28);
getContentPane().add(t1);
setSize(400,400);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLocationRelativeTo(null);
setVisible(true);
}
}
Main: 主要:
public class Main {
public static void main(String[] args) {
L object = new L();
}
}
Here is an example of which you can you inspire 这是一个你可以激发灵感的例子
package TinyOS;
import java.awt.*;
import javax.swing.*;
import javax.swing.text.Document;
@SuppressWarnings("serial")
public class PlaceholderTextField extends JTextField {
public static void main(final String[] args) {
final PlaceholderTextField tf = new PlaceholderTextField ("");
tf.setColumns(20);
tf.setPlaceholder("Here is a placeHolder!");
final Font f = tf.getFont();
tf.setFont(new Font(f.getName(), f.getStyle(), 30));
JOptionPane.showMessageDialog(null, tf);
}
private String placeholder;
public PlaceholderTextField () {
}
public PlaceholderTextField (
final Document pDoc,
final String pText,
final int pColumns)
{
super(pDoc, pText, pColumns);
}
public PlaceholderTextField (final int pColumns) {
super(pColumns);
}
}
I hope that can help you 我希望能帮助你
This code should work, it listen on first click and removes the text 此代码应该有效,它会在第一次单击时收听并删除文本
public class Loop extends JFrame{
private JTextField t1;
private boolean clicked = false;
public L(){
getContentPane().setLayout(null);
t1=new JTextField();
t1.setText("Enter Your Number");
t1.addMouseListener(new MouseAdapter(){
@Override
public void mousePressed(MouseEvent e){
if(!clicked){
clicked=true;
t1.setText("");
}
}
}
t1.setBounds(27,50,47,28);
getContentPane().add(t1);
setSize(400,400);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLocationRelativeTo(null);
setVisible(true);
}
}
Maybe better solution exists 也许存在更好的解决方
Note - not tested 注意 - 未经测试
clicked
works)
clicked
工作方式)
mousePressed(MouseEvent)
at the first time, the clicked
variable is false
, by declaration:
mousePressed(MouseEvent)
时, clicked
变量为false
,通过声明:
private boolean clicked = false;
So the if
body is executed (because !clicked = !false = true ) 所以
if
主体被执行(因为!clicked =!false = true )
in the if
body, the clicked
variable is set to true, so if
condition will be then false: (because !clicked = !true = false ) 在
if
主体中, clicked
变量设置为true,因此if
condition将为false :(因为!clicked =!true = false )
This solves the problem of running code just once. 这解决了仅运行一次代码的问题。
Check out Text Prompt for a flexible solution. 查看文本提示以获得灵活的解决方案。
You can control when prompt is displayed (always, focus gained or focus lost). 您可以控制何时显示提示(始终,焦点获得或焦点丢失)。 You can also customize the style of the text.
您还可以自定义文本样式。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.