Java Swing实现的定制TextField功能示例
本文实例讲述了Java Swing实现的定制TextField功能。分享给大家供大家参考,具体如下:
先来看看运行效果:
具体代码如下:
package themedemo; import java.awt.BasicStroke; import java.awt.BorderLayout; import java.awt.Color; import java.awt.Graphics2D; import java.awt.GridLayout; import java.awt.Insets; import java.awt.RenderingHints; import java.util.Map; import javax.swing.BorderFactory; import javax.swing.JComponent; import javax.swing.JFrame; import javax.swing.JPanel; import javax.swing.JTextField; import javax.swing.Painter; import javax.swing.SwingUtilities; import javax.swing.UIDefaults; import javax.swing.UIManager; import javax.swing.WindowConstants; public class TextFieldSkinDemo { public static void main(String[] args) { SwingUtilities.invokeLater(new Runnable() { public void run() { for (UIManager.LookAndFeelInfo laf : UIManager .getInstalledLookAndFeels()) { if ("Nimbus".equals(laf.getName())) { try { UIManager.setLookAndFeel(laf.getClassName()); } catch (Exception e) { e.printStackTrace(); } } } for (Map.Entry<Object, Object> entry : UIManager .getLookAndFeelDefaults().entrySet()) { if ((entry.getKey().toString()).startsWith("TextField")) { System.out.println(entry.getKey() + " = " + entry.getValue()); } } JFrame frame = new JFrame("www.jb51.net - TextField Skining Demo"); frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); frame.getContentPane().setLayout(new BorderLayout()); JPanel panel = new JPanel(new GridLayout(0, 1, 20, 20)); panel.setBorder(BorderFactory.createEmptyBorder(20, 20, 20, 20)); panel.setBackground(Color.darkGray); UIDefaults textFieldDefaults = new UIDefaults(); textFieldDefaults.put("TextField.contentMargins", new Insets(6, 6, 6, 6)); textFieldDefaults.put("TextField[Selected].textForeground", Color.WHITE); textFieldDefaults.put("TextField.backgroundPainter", new Painter<JComponent>() { public void paint(Graphics2D g, JComponent c, int w, int h) { g.setRenderingHint( RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); g.setStroke(new BasicStroke(2f)); g.setColor(Color.WHITE); g.fillRoundRect(0, 0, w - 2, h - 2, 8, 8); } }); textFieldDefaults.put("TextField.borderPainter", new Painter<JComponent>() { public void paint(Graphics2D g, JComponent c, int w, int h) { g.setRenderingHint( RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); g.setStroke(new BasicStroke(2f)); g.setColor(Color.LIGHT_GRAY); g.drawRoundRect(0, 0, w - 2, h - 2, 8, 8); } }); JTextField textField = new JTextField(15); panel.add(textField); textField.putClientProperty("Nimbus.Overrides", textFieldDefaults); textField.putClientProperty("Nimbus.Overrides.InheritDefaults", false); // Add a normal themed slider for comparison JTextField normalTextField = new JTextField(15); panel.add(normalTextField); frame.getContentPane().add(panel, BorderLayout.CENTER); frame.pack(); frame.setLocationRelativeTo(null); frame.setVisible(true); } }); } }
更多关于java算法相关内容感兴趣的读者可查看本站专题:《Java数据结构与算法教程》、《Java操作DOM节点技巧总结》、《Java文件与目录操作技巧汇总》和《Java缓存操作技巧汇总》
希望本文所述对大家java程序设计有所帮助。
您可能感兴趣的文章:
- Java Swing组件复选框JCheckBox用法示例
- Java编程使用箱式布局管理器示例【基于swing组件】
- Java编程swing组件JLabel详解以及使用示例
- Java Swing实现简单的体重指数(BMI)计算器功能示例
- Java swing实现的计算器功能完整实例
- Java swing框架实现的贪吃蛇游戏完整示例
- Java Swing组件编程之JTable表格用法实例详解
- Java Swing组件单选框JRadioButton用法示例
- Java Swing组件下拉菜单控件JComboBox用法示例
- Java Swing中的文本框(JTextField)与文本区(JTextArea)使用实例
- Java Swing中的下拉式菜单(menu)、弹出式菜单(JPopupMenu)、选项卡窗体(JTabbedPane)组件使用案例
- Java Swing组件定制CheckBox示例
赞 (0)