private static void createAndShowGUI() {
JFrame frame = new JFrame("Demo");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(350, 400);
try {
JTextPane textPane = new JTextPane();
StyledDocument doc = (StyledDocument) textPane.getDocument();
Style style = doc.addStyle("StyleName", null);
// Italic
StyleConstants.setItalic(style, false);
// Bold
StyleConstants.setBold(style, true);
doc.insertString(doc.getLength(), "Some Text\n", style);
// Font Family
StyleConstants.setFontFamily(style, "tahoma");
// Font Size
StyleConstants.setFontSize(style, 30);
doc.insertString(doc.getLength(), "Some Text\n", style);
// Background Color
StyleConstants.setBackground(style, Color.blue);
// Insert an image icon
StyleConstants.setIcon(style, new ImageIcon("Adium.png"));
// Foreground Color
StyleConstants.setForeground(style, Color.white);
// Append to document.
doc.insertString(doc.getLength(), "Some Text", style);
frame.getContentPane().add(textPane);
} catch (BadLocationException e) {
}
frame.setVisible(true);
}
单行输入用JTextField
多行输入用JTextArea
密码输入用JPasswordField
简单网页载入用JEditorPanel, 添加hyperlinklistener
格式化的用JTextPane
控制只能输入数字等可以JFormattedTextField
NumberFormat
解析日期可用SimpleDateFormat