我很熟悉你可以手工输入文本到JTextField。此文本将显示在JTextField中,并且必须在将您自己的文本输入JTextField时手动删除。例如,请考虑此JTextField:
cruiseSel = new JTextField ("Selected Cruise:");
cruiseSel.setEditable(false);
centerP12.add(cruiseSel);
contentPane12.add(centerP12, BorderLayout.CENTER);
Frame12.setVisible(true);
运行上述内容后,JTextField将显示“Selected Cruise:”。然后必须手动删除此文本以清除文本字段。
有没有办法在JTextField中输入文本,所以一旦GUI打开,将显示文本,但是当选择JTextField输入手动文本时,文本会消失吗?
答案 0 :(得分:3)
您可以使用FocusListener,当JTextField获得焦点时,清空文本。
当然,您需要一个状态标记来指示它具有默认文本,而不是在您输入用户文本后执行此操作。在第一次点击FocusListener之后或之后,将其删除。
textField.addFocusListener(new FocusAdapter() {
public void focusGained(FocusEvent e) {
JTextField source = (JTextField)e.getComponent();
source.setText("");
source.removeFocusListener(this);
}
});
答案 1 :(得分:1)
您可以使用SwingX
阅读How to set Text like Placeholder in JTextfield in swing
我在这里提供了示例代码供您使用
import java.awt.EventQueue;
import java.awt.Font;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import javax.swing.JFrame;
import javax.swing.JTextField;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import org.jdesktop.swingx.prompt.PromptSupport;
public class PromptExample {
public static void main(String[] args) {
new PromptExample();
}
public PromptExample() {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
}
JTextField bunnies = new JTextField(10);
JTextField ponnies = new JTextField(10);
JTextField unicorns = new JTextField(10);
JTextField fairies = new JTextField(10);
PromptSupport.setPrompt("Bunnies", bunnies);
PromptSupport.setPrompt("Ponnies", ponnies);
PromptSupport.setPrompt("Unicorns", unicorns);
PromptSupport.setPrompt("Fairies", fairies);
PromptSupport.setFocusBehavior(PromptSupport.FocusBehavior.HIDE_PROMPT, bunnies);
PromptSupport.setFocusBehavior(PromptSupport.FocusBehavior.HIGHLIGHT_PROMPT, ponnies);
PromptSupport.setFocusBehavior(PromptSupport.FocusBehavior.SHOW_PROMPT, unicorns);
PromptSupport.setFontStyle(Font.BOLD, bunnies);
PromptSupport.setFontStyle(Font.ITALIC, ponnies);
PromptSupport.setFontStyle(Font.ITALIC | Font.BOLD, unicorns);
JFrame frame = new JFrame("Testing");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(new GridBagLayout());
GridBagConstraints gbc = new GridBagConstraints();
gbc.gridwidth = GridBagConstraints.REMAINDER;
frame.add(bunnies, gbc);
frame.add(ponnies, gbc);
frame.add(unicorns, gbc);
frame.add(fairies, gbc);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}
}
答案 2 :(得分:1)
查看Text Prompt。
它支持此功能以及一些用于自定义提示行为的功能。
答案 3 :(得分:1)
您正在寻找的是占位符。我刚才写过这门课:
import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Insets;
import java.awt.Rectangle;
import java.awt.RenderingHints;
import java.awt.event.FocusEvent;
import java.awt.event.FocusListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;
import javax.swing.text.BadLocationException;
import javax.swing.text.Document;
/**
* @author xehpuk
*/
public class PlaceholderTextField extends JTextField {
private static final long serialVersionUID = -5529071085971698388L;
/**
* The placeholder to be displayed if the text field is empty.
*/
private String placeholder;
/**
* Determines whether the placeholder should be displayed even on focus.
*/
private boolean paintingOnFocus;
/**
* The color the placeholder should be displayed in.
*/
private Color placeholderColor;
public String getPlaceholder() {
return placeholder;
}
public void setPlaceholder(final String placeholder) {
this.placeholder = placeholder;
repaint();
}
public boolean isPaintingOnFocus() {
return paintingOnFocus;
}
public void setPaintingOnFocus(final boolean paintingOnFocus) {
this.paintingOnFocus = paintingOnFocus;
repaint();
}
public Color getPlaceholderColor() {
return placeholderColor;
}
public void setPlaceholderColor(final Color placeholderColor) {
this.placeholderColor = placeholderColor;
repaint();
}
public PlaceholderTextField() {
super();
}
public PlaceholderTextField(final Document doc, final String text, final int columns) {
super(doc, text, columns);
}
public PlaceholderTextField(final int columns) {
super(columns);
}
public PlaceholderTextField(final String text, final int columns) {
super(text, columns);
}
public PlaceholderTextField(final String text) {
super(text);
}
{
addFocusListener(new RepaintFocusListener());
}
@Override
protected void paintComponent(final Graphics g) {
super.paintComponent(g);
if (getPlaceholder() != null && getText().isEmpty() && (isPaintingOnFocus() || !isFocusOwner())) {
try {
final Rectangle rect = getUI().modelToView(this, 0);
final Insets insets = getInsets();
g.setFont(getFont());
g.setColor(getPlaceholderColor() == null ? getForeground() : getPlaceholderColor());
((Graphics2D) g).setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
g.drawString(getPlaceholder(), rect.x, getHeight() - insets.top - insets.bottom - rect.y);
} catch (final BadLocationException e) {
throw new RuntimeException(e);
}
}
}
private class RepaintFocusListener implements FocusListener {
@Override
public void focusGained(final FocusEvent e) {
repaint();
}
@Override
public void focusLost(final FocusEvent e) {
repaint();
}
}
}
即使文本字段具有焦点,您也可以选择文本和颜色以及是否应该绘制颜色。
关键部分是覆盖paintComponent(Graphics)
。
答案 4 :(得分:-1)
要实现这样的目标,通常需要创建某种类型的event listener。在您的情况下,需要在鼠标事件上触发所需的操作 - 因此MouseAdapter
事件监听器看起来非常合适(乍一看)。使用MouseAdapter
抽象类,您需要扩展它并覆盖必要的方法(有关可用方法的完整列表,请参阅here)。
实现这一目标的最短途径是通过anonymous class声明,如下所示:
cruiseSel.addMouseListener(new MouseAdapter(){
@Override
public void mouseClicked(MouseEvent e){
cruiseSel.setText("");
}
});
(但是,如果您需要覆盖多个方法或触发逻辑足够复杂,那么最好创建一个单独的侦听器类。)
编辑:或者,正如@HovercraftFullOfEels在评论部分指出的那样,以同样的方式应用FocusAdapter
类(见here)可能更明智:
cruiseSel.addFocusListener(new FocusAdapter(){
@Override
public void focusGained(FocusEvent e){
cruiseSel.setText("");
}
});
第一个解决方案的问题在于它只关注在文本字段上侦听实际的MOUSE CLICKS,而后者则在其上侦听任何类型的焦点增益。因此,当使用TAB键在文本字段之间切换时,只有第二个解决方案才能正确执行。