java placeholder_java – 如何在JTextfield中设置像Placeholder一样的文本

我用来覆盖文本字段绘制方法,直到我最终得到更多的自定义文本字段然后我真的想…

然后我发现this prompt API易于使用,不需要你扩展任何组件.它还有一个很好的“伙伴”API

这已经被包含在SwingLabs,SwingX library中,这使得它甚至可以使用……

例如(这使用SwingX-1.6.4)

import java.awt.BorderLayout;

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);

}

});

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值