查看: 1201|回复: 0

[Java学习] Java Swing实现的定制TextField功能示例

发表于 2018-3-2 08:04:34

本文实例讲述了Java Swing实现的定制TextField功能。分享给大家供大家参考,具体如下:

先来看看运行效果:

具体代码如下:

  1. package themedemo;
  2. import java.awt.BasicStroke;
  3. import java.awt.BorderLayout;
  4. import java.awt.Color;
  5. import java.awt.Graphics2D;
  6. import java.awt.GridLayout;
  7. import java.awt.Insets;
  8. import java.awt.RenderingHints;
  9. import java.util.Map;
  10. import javax.swing.BorderFactory;
  11. import javax.swing.JComponent;
  12. import javax.swing.JFrame;
  13. import javax.swing.JPanel;
  14. import javax.swing.JTextField;
  15. import javax.swing.Painter;
  16. import javax.swing.SwingUtilities;
  17. import javax.swing.UIDefaults;
  18. import javax.swing.UIManager;
  19. import javax.swing.WindowConstants;
  20. public class TextFieldSkinDemo {
  21. public static void main(String[] args) {
  22. SwingUtilities.invokeLater(new Runnable() {
  23. public void run() {
  24. for (UIManager.LookAndFeelInfo laf : UIManager
  25. .getInstalledLookAndFeels()) {
  26. if ("Nimbus".equals(laf.getName())) {
  27. try {
  28. UIManager.setLookAndFeel(laf.getClassName());
  29. } catch (Exception e) {
  30. e.printStackTrace();
  31. }
  32. }
  33. }
  34. for (Map.Entry<Object, Object> entry : UIManager
  35. .getLookAndFeelDefaults().entrySet()) {
  36. if ((entry.getKey().toString()).startsWith("TextField")) {
  37. System.out.println(entry.getKey() + " = "
  38. + entry.getValue());
  39. }
  40. }
  41. JFrame frame = new JFrame("www.jb51.net - TextField Skining Demo");
  42. frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
  43. frame.getContentPane().setLayout(new BorderLayout());
  44. JPanel panel = new JPanel(new GridLayout(0, 1, 20, 20));
  45. panel.setBorder(BorderFactory.createEmptyBorder(20, 20, 20, 20));
  46. panel.setBackground(Color.darkGray);
  47. UIDefaults textFieldDefaults = new UIDefaults();
  48. textFieldDefaults.put("TextField.contentMargins", new Insets(6,
  49. 6, 6, 6));
  50. textFieldDefaults.put("TextField[Selected].textForeground",
  51. Color.WHITE);
  52. textFieldDefaults.put("TextField.backgroundPainter",
  53. new Painter<JComponent>() {
  54. public void paint(Graphics2D g, JComponent c,
  55. int w, int h) {
  56. g.setRenderingHint(
  57. RenderingHints.KEY_ANTIALIASING,
  58. RenderingHints.VALUE_ANTIALIAS_ON);
  59. g.setStroke(new BasicStroke(2f));
  60. g.setColor(Color.WHITE);
  61. g.fillRoundRect(0, 0, w - 2, h - 2, 8, 8);
  62. }
  63. });
  64. textFieldDefaults.put("TextField.borderPainter",
  65. new Painter<JComponent>() {
  66. public void paint(Graphics2D g, JComponent c,
  67. int w, int h) {
  68. g.setRenderingHint(
  69. RenderingHints.KEY_ANTIALIASING,
  70. RenderingHints.VALUE_ANTIALIAS_ON);
  71. g.setStroke(new BasicStroke(2f));
  72. g.setColor(Color.LIGHT_GRAY);
  73. g.drawRoundRect(0, 0, w - 2, h - 2, 8, 8);
  74. }
  75. });
  76. JTextField textField = new JTextField(15);
  77. panel.add(textField);
  78. textField.putClientProperty("Nimbus.Overrides",
  79. textFieldDefaults);
  80. textField.putClientProperty("Nimbus.Overrides.InheritDefaults",
  81. false);
  82. // Add a normal themed slider for comparison
  83. JTextField normalTextField = new JTextField(15);
  84. panel.add(normalTextField);
  85. frame.getContentPane().add(panel, BorderLayout.CENTER);
  86. frame.pack();
  87. frame.setLocationRelativeTo(null);
  88. frame.setVisible(true);
  89. }
  90. });
  91. }
  92. }
复制代码

更多关于java算法相关内容感兴趣的读者可查看本站专题:《Java数据结构与算法教程》、《Java操作DOM节点技巧总结》、《Java文件与目录操作技巧汇总》和《Java缓存操作技巧汇总》

希望本文所述对大家java程序设计有所帮助。



回复

使用道具 举报