分类: Java
2008-04-17 15:50:04
第22章 文本组件
Swing提供两种完全不同的文本组件类型:简单文本控件和风格文本组件。简单文本控件一次只能显示一种字体和一种颜色,而风格文本组件则可以显示多种字体和颜色。前者有单行文本域(JTextField)、口令域(JPassowrdField)、后者有编辑器窗格(JEditorPan)和文本窗格(JTextPane)。
22.1 JTextField
例22-1 单行文本域的排列方式和列数
public class Test extends JApplet {
private JPanel textFieldPanel = new JPanel();
private JTextField textField =
new JTextField("initial content");
public void init() {
Container contentPane = getContentPane();
textFieldPanel.add(textField);
contentPane.setLayout(new BorderLayout(0,20));
contentPane.add(new ControlPanel(), BorderLayout.NORTH);
contentPane.add(textFieldPanel, BorderLayout.CENTER);
}
class ControlPanel extends JPanel {
private JComboBox alignments = new JComboBox();
private JComboBox columns = new JComboBox();
public ControlPanel() {
columns.addItem(new Integer(0));
columns.addItem(new Integer(5));
columns.addItem(new Integer(10));
columns.addItem(new Integer(15));
alignments.addItem("LEFT");
alignments.addItem("CENTER");
alignments.addItem("RIGHT");
add(new JLabel("Horizontal Alignment:"));
add(alignments);
add(Box.createHorizontalStrut(10));
add(new JLabel("Columns:"));
add(columns);
columns.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
Integer c =
(Integer)columns.getSelectedItem();
textField.setColumns(c.intValue());
// the following call to revalidate()
// should not be necessary
revalidate();
textField.setScrollOffset(0);
}
});
alignments.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
int index = alignments.getSelectedIndex();
if(index == 0)
textField.setHorizontalAlignment(
JTextField.LEFT);
else if(index == 1)
textField.setHorizontalAlignment(
JTextField.CENTER);
else if(index == 2)
textField.setHorizontalAlignment(
JTextField.RIGHT);
}
});
}
}
}
22.1.1 水平可视性和滚动偏移
例22-2 单行文本域的滚动偏移
public class Test extends JApplet {
private JTextField textField = new JTextField(
"12345678901234567890123456789012345678901234567890", 10);
public void init() {
Container contentPane = getContentPane();
JPanel textFieldPanel = new JPanel();
textFieldPanel.add(textField);
contentPane.add(new ControlPanel(), BorderLayout.NORTH);
contentPane.add(textFieldPanel, BorderLayout.CENTER);
}
class ControlPanel extends JPanel {
private JLabel display = new JLabel(" ");
private JSlider slider = new JSlider(
textField.getHorizontalVisibility());
private JComboBox columns = new JComboBox();
public ControlPanel() {
columns.addItem(new Integer(0));
columns.addItem(new Integer(5));
columns.addItem(new Integer(10));
columns.addItem(new Integer(15));
columns.setSelectedIndex(2);
add(new JLabel("Scroll Offset:"));
add(slider);
add(display);
add(Box.createHorizontalStrut(10));
add(new JLabel("Columns:"));
add(columns);
slider.addChangeListener(new ChangeListener() {
public void stateChanged(ChangeEvent e) {
textField.setScrollOffset(slider.getValue());
Integer i =
new Integer(textField.getScrollOffset());
BoundedRangeModel m =
textField.getHorizontalVisibility();
display.setText(i.toString());
showStatus("Visibility - min: " +
m.getMinimum() +
", max: " + m.getMaximum() +
", extent: " + m.getExtent() +
", value: " + m.getValue() +
", isAdj: " +
m.getValueIsAdjusting());
}
});
columns.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
Integer c =
(Integer)columns.getSelectedItem();
textField.setColumns(c.intValue());
// the following call to revalidate()
// should not be necessary
revalidate();
textField.setScrollOffset(0);
}
});
}
}
}
22.1.2 布局单行文本域
例22-3 用GridBagLayout来布局单行文本域
public class PurchaseApplet extends JApplet {
public void init() {
getContentPane().add(new ButtonPurchaseForm(),
BorderLayout.CENTER);
}
}
class ButtonPurchaseForm extends JPanel {
JSeparator sep = new JSeparator();
JLabel title = new JLabel("Order Form");
JLabel name = new JLabel("Name:");
JLabel address = new JLabel("Address:");
JLabel payment = new JLabel("Purchase Method:");
JLabel phone = new JLabel("Phone:");
JLabel city = new JLabel("City:");
JLabel state = new JLabel("State:");
JTextField nameField = new JTextField(25);
JTextField addressField = new JTextField(25);
JTextField cityField = new JTextField(15);
JTextField stateField = new JTextField(2);
JComboBox paymentChoice = new JComboBox();
JButton paymentButton = new JButton("Purchase");
JButton cancelButton = new JButton("Cancel");
public ButtonPurchaseForm() {
GridBagLayout gbl = new GridBagLayout();
GridBagConstraints gbc = new GridBagConstraints();
setLayout(gbl);
paymentChoice.addItem("Visa");
paymentChoice.addItem("MasterCard");
paymentChoice.addItem("COD");
title.setFont(new Font("Times-Roman",
Font.BOLD + Font.ITALIC,
16));
gbc.anchor = GridBagConstraints.NORTHWEST;
gbc.gridwidth = GridBagConstraints.REMAINDER;
add(title, gbc);
gbc.anchor = GridBagConstraints.NORTH;
gbc.fill = GridBagConstraints.HORIZONTAL;
gbc.insets = new Insets(0,0,10,0);
add(sep, gbc);
gbc.anchor = GridBagConstraints.WEST;
gbc.gridwidth = 1;
gbc.insets = new Insets(0,0,0,0);
add(name, gbc);
add(Box.createHorizontalStrut(10));
gbc.gridwidth = GridBagConstraints.REMAINDER;
add(nameField, gbc);
gbc.gridwidth = 1;
add(address, gbc);
add(Box.createHorizontalStrut(10));
gbc.gridwidth = GridBagConstraints.REMAINDER;
add(addressField, gbc);
gbc.gridwidth = 1;
add(city, gbc);
add(Box.createHorizontalStrut(10));
add(cityField, gbc);
add(Box.createHorizontalStrut(10));
add(state, gbc);
add(Box.createHorizontalStrut(5));
gbc.gridwidth = GridBagConstraints.REMAINDER;
gbc.fill = GridBagConstraints.NONE;
add(stateField, gbc);
gbc.gridwidth = 1;
add(payment, gbc);
gbc.insets = new Insets(5,0,5,0);
add(Box.createHorizontalStrut(10));
gbc.gridwidth = GridBagConstraints.REMAINDER;
gbc.fill = GridBagConstraints.NONE;
add(paymentChoice, gbc);
ButtonPanel buttonPanel = new ButtonPanel();
buttonPanel.add(paymentButton);
buttonPanel.add(cancelButton);
gbc.anchor = GridBagConstraints.SOUTH;
gbc.insets = new Insets(15,0,0,0);
gbc.fill = GridBagConstraints.HORIZONTAL;
gbc.gridwidth = 7;
add(buttonPanel, gbc);
}
class ButtonPanel extends JPanel {
JPanel buttonPanel = new JPanel();
JSeparator separator = new JSeparator();
public ButtonPanel() {
buttonPanel.setLayout(
new FlowLayout(FlowLayout.CENTER));
setLayout(new BorderLayout(0,5));
add(separator, "North");
add(buttonPanel, "Center");
}
public void add(JButton button) {
buttonPanel.add(button);
}
}
}
22.1.3 使单行文本域有效
例22-4 实现一个定制文档
public class Test extends JApplet {
JTextField tf = new JTextField(DateDocument.initString);
public Test() {
Container contentPane = getContentPane();
JLabel label = new JLabel("Date:");
Font font = new Font("Dialog", Font.PLAIN, 24);
tf.setFont(font);
label.setFont(font);
tf.setDocument(new DateDocument(tf));
contentPane.setLayout(new FlowLayout(FlowLayout.CENTER,
10,10));
contentPane.add(label);
contentPane.add(tf);
}
}
class DateDocument extends PlainDocument {
public static String initString = "XX/XX/XXXX"; // Y10K!
private static int sep1 = 2, sep2 = 5;
private JTextComponent textComponent;
private int newOffset;
public DateDocument(JTextComponent tc) {
textComponent = tc;
try {
insertString(0, initString, null);
}
catch(Exception ex) { ex.printStackTrace(); }
}
public void insertString(int offset, String s,
AttributeSet attributeSet)
throws BadLocationException {
if(s.equals(initString)) {
super.insertString(offset, s, attributeSet);
}
else {
try {
Integer.parseInt(s);
}
catch(Exception ex) {
return; // only allow integer values
}
newOffset = offset;
if(atSeparator(offset)) {
newOffset++;
textComponent.setCaretPosition(newOffset);
}
super.remove(newOffset, 1);
super.insertString(newOffset, s, attributeSet);
}
}
public void remove(int offset, int length)
throws BadLocationException {
if(atSeparator(offset))
textComponent.setCaretPosition(offset-1);
else
textComponent.setCaretPosition(offset);
}
private boolean atSeparator(int offset) {
return offset == sep1 || offset == sep2;
}
}
22.1.4 JTextField组件总结
22.1.5 JTextField属性
例22-5 单行文本域和缺省按钮
public class Test extends JFrame {
private JTextField field = new JTextField(10);
private JButton b = new JButton("Default Button");
private JCheckBox cb = new JCheckBox(
"Text field fires action event");
public Test() {
Container contentPane = getContentPane();
SwingUtilities.getRootPane(this).setDefaultButton(b);
cb.setSelected(true);
contentPane.setLayout(new FlowLayout(
FlowLayout.CENTER,10,20));
contentPane.add(field);
contentPane.add(b);
contentPane.add(cb);
b.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
JOptionPane.showMessageDialog(Test.this,
"Button fired action event");
}
});
field.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
JOptionPane.showMessageDialog(Test.this,
"Textfield fired action event");
}
});
cb.addActionListener(new ActionListener() {
private Keymap km;
private KeyStroke ks;
private Action action;
public void actionPerformed(ActionEvent e) {
if(cb.isSelected()) {
km.addActionForKeyStroke(ks, action);
}
else {
if(ks == null) {
km = field.getKeymap();
ks = KeyStroke.getKeyStroke(
KeyEvent.VK_ENTER, 0);
action = km.getAction(ks);
}
km.removeKeyStrokeBinding(ks);
}
}
});
}
public static void main(String args[]) {
GJApp.launch(new Test(),
"Default Buttons",300,300,350,200);
}
}
class GJApp extends WindowAdapter {
static private JPanel statusArea = new JPanel();
static private JLabel status = new JLabel(" ");
static private ResourceBundle resources;
public static void launch(final JFrame f, String title,
final int x, final int y,
final int w, int h) {
launch(f,title,x,y,w,h,null);
}
public static void launch(final JFrame f, String title,
final int x, final int y,
final int w, int h,
String propertiesFilename) {
f.setTitle(title);
f.setBounds(x,y,w,h);
f.setVisible(true);
statusArea.setBorder(BorderFactory.createEtchedBorder());
statusArea.setLayout(new FlowLayout(FlowLayout.LEFT,0,0));
statusArea.add(status);
status.setHorizontalAlignment(JLabel.LEFT);
f.setDefaultCloseOperation(
WindowConstants.DISPOSE_ON_CLOSE);
if(propertiesFilename != null) {
resources = ResourceBundle.getBundle(
propertiesFilename, Locale.getDefault());
}
f.addWindowListener(new WindowAdapter() {
public void windowClosed(WindowEvent e) {
System.exit(0);
}
});
}
static public JPanel getStatusArea() {
return statusArea;
}
static public void showStatus(String s) {
status.setText(s);
}
static Object getResource(String key) {
if(resources != null) {
return resources.getString(key);
}
return null;
}
}
22.1.6 JTextField事件
22.1.7 JTextField类总结
22.1.8 AWT兼容
22.2 JPasswordField
例22-6 使用JPasswordField
public class Test extends JApplet {
private String pw = "dol42ce";
private JPasswordField passwordField = new JPasswordField(8);
public void init() {
Container contentPane = getContentPane();
JPanel panel = new JPanel();
panel.add(new JLabel("Password:"));
panel.add(passwordField);
passwordField.setEchoChar('?');
contentPane.add(panel, BorderLayout.CENTER);
passwordField.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String password = new String(
passwordField.getPassword());
if(pw.equals(password))
showStatus("Access Granted");
else
showStatus("Wrong password - security " +
"has been called");
}
});
}
}
22.2.1 JPasswordField组件总结
22.2.2 JPasswordField属性
22.2.3 JPasswordField类总结
22.3 JTextArea
例22-7 指定JTExtArea的换行行为
public class Test extends JApplet {
private JTextArea textArea = new JTextArea();
private Container contentPane = getContentPane();
public void init() {
readFile(textArea, "text");
contentPane.add(new ControlPanel(), BorderLayout.NORTH);
contentPane.add(new JScrollPane(textArea),
BorderLayout.CENTER);
}
private void readFile(JTextComponent textComponent,String s) {
try { (new DefaultEditorKit()).read(
new FileReader(s), textComponent.getDocument(), 0);
} catch(Exception ex) { ex.printStackTrace(); }
}
class ControlPanel extends JPanel {
JRadioButton radioButtons[] = new JRadioButton[] {
new JRadioButton("wrap off"),
new JRadioButton("wrap characters"),
new JRadioButton("wrap words"),
};
public ControlPanel() {
ButtonGroup group = new ButtonGroup();
Listener listener = new Listener();
for(int i=0; i < radioButtons.length; ++i) {
JRadioButton b = radioButtons[i];
b.addActionListener(listener);
group.add(b);
add(b);
if(i == 0)
b.setSelected(true); // "wrap off"
}
}
class Listener implements ActionListener {
public void actionPerformed(ActionEvent e) {
String action = e.getActionCommand();
textArea.setLineWrap(!action.equals("wrap off"));
textArea.setWrapStyleWord(
action.equals("wrap words"));
//textArea.repaint();
showStatus("rows: " + textArea.getRows() +
", columns: " + textArea.getColumns() +
", lines: " + textArea.getLineCount());
}
};
}
}
22.3.1 JTextArea组件总结
22.3.2 JTextArea属性
22.3.3 JTextArea类总结
22.3.4 AWT兼容
22.4 JEditorPane
例22-8 用一个编辑器窗格来显示HTML文件
public class Test extends JFrame {
private JEditorPane editorPane = new JEditorPane();
public Test() {
Container contentPane = getContentPane();
String url = "file:" + System.getProperty("user.dir") +
System.getProperty("file.separator") +
"java.util.Hashtable.html";
editorPane.setEditable(false);
try {
editorPane.setPage(url);
}
catch(Exception ex) { ex.printStackTrace(); }
contentPane.add(new ControlPanel(), BorderLayout.NORTH);
contentPane.add(new JScrollPane(editorPane),
BorderLayout.CENTER);
}
class ControlPanel extends JPanel {
private JCheckBox edit = new JCheckBox("Editable");
public ControlPanel() {
add(edit);
edit.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
editorPane.setEditable(edit.isSelected());
}
});
}
}
public static void main(String args[]) {
GJApp.launch(new Test(),
"JEditorPane",300,300,650,450);
}
}
class GJApp extends WindowAdapter {
static private JPanel statusArea = new JPanel();
static private JLabel status = new JLabel(" ");
static private ResourceBundle resources;
public static void launch(final JFrame f, String title,
final int x, final int y,
final int w, int h) {
launch(f,title,x,y,w,h,null);
}
public static void launch(final JFrame f, String title,
final int x, final int y,
final int w, int h,
String propertiesFilename) {
f.setTitle(title);
f.setBounds(x,y,w,h);
f.setVisible(true);
statusArea.setBorder(BorderFactory.createEtchedBorder());
statusArea.setLayout(new FlowLayout(FlowLayout.LEFT,0,0));
statusArea.add(status);
status.setHorizontalAlignment(JLabel.LEFT);
f.setDefaultCloseOperation(
WindowConstants.DISPOSE_ON_CLOSE);
if(propertiesFilename != null) {
resources = ResourceBundle.getBundle(
propertiesFilename, Locale.getDefault());
}
f.addWindowListener(new WindowAdapter() {
public void windowClosed(WindowEvent e) {
System.exit(0);
}
});
}
static public JPanel getStatusArea() {
return statusArea;
}
static public void showStatus(String s) {
status.setText(s);
}
static Object getResource(String key) {
if(resources != null) {
return resources.getString(key);
}
return null;
}
}
22.4.1 JEditorPane属性
22.4.2 JEditorPane事件
例22-9 用超链接监听器来装载URL
public class Test extends JFrame {
private JEditorPane editorPane = new JEditorPane();
public Test() {
Container contentPane = getContentPane();
String url = "file:" + System.getProperty("user.dir") +
System.getProperty("file.separator") +
"java.util.Hashtable.html";
try {
editorPane.setPage(url);
}
catch(IOException ex) { ex.printStackTrace(); }
contentPane.add(new JScrollPane(editorPane),
BorderLayout.CENTER);
editorPane.setEditable(false);
editorPane.addHyperlinkListener(new HyperlinkListener() {
public void hyperlinkUpdate(HyperlinkEvent e) {
try {
editorPane.setPage(e.getURL());
}
catch(IOException ex) { ex.printStackTrace(); }
}
});
}
public static void main(String args[]) {
GJApp.launch(new Test(),
"JEditorPane",300,300,450,300);
}
}
class GJApp extends WindowAdapter {
static private JPanel statusArea = new JPanel();
static private JLabel status = new JLabel(" ");
static private ResourceBundle resources;
public static void launch(final JFrame f, String title,
final int x, final int y,
final int w, int h) {
launch(f,title,x,y,w,h,null);
}
public static void launch(final JFrame f, String title,
final int x, final int y,
final int w, int h,
String propertiesFilename) {
f.setTitle(title);
f.setBounds(x,y,w,h);
f.setVisible(true);
statusArea.setBorder(BorderFactory.createEtchedBorder());
statusArea.setLayout(new FlowLayout(FlowLayout.LEFT,0,0));
statusArea.add(status);
status.setHorizontalAlignment(JLabel.LEFT);
f.setDefaultCloseOperation(
WindowConstants.DISPOSE_ON_CLOSE);
if(propertiesFilename != null) {
resources = ResourceBundle.getBundle(
propertiesFilename, Locale.getDefault());
}
f.addWindowListener(new WindowAdapter() {
public void windowClosed(WindowEvent e) {
System.exit(0);
}
});
}
static public JPanel getStatusArea() {
return statusArea;
}
static public void showStatus(String s) {
status.setText(s);
}
static Object getResource(String key) {
if(resources != null) {
return resources.getString(key);
}
return null;
}
}
22.4.3 JEditorPane类总结
22.5 JTextPane
22.5.1 嵌入图标和组件
例22-10 文本窗格里的组件和图标
public class Test extends JFrame {
private JFileChooser chooser = new JFileChooser();
private JTextPane textPane = new JTextPane();
public Test() {
Container contentPane = getContentPane();
JMenuBar menuBar = new JMenuBar();
JMenu insertMenu = new JMenu("Insert");
JMenuItem imageItem = new JMenuItem("image"),
chooserItem = new JMenuItem("color chooser");
insertMenu.add(imageItem);
insertMenu.add(chooserItem);
menuBar.add(insertMenu);
setJMenuBar(menuBar);
textPane.setFont(new Font("Serif", Font.ITALIC, 24));
contentPane.add(textPane, BorderLayout.CENTER);
chooserItem.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
JColorChooser chooser = new JColorChooser();
chooser.setMaximumSize(
chooser.getPreferredSize());
textPane.insertComponent(chooser);
}
});
imageItem.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
int option =
chooser.showDialog(Test.this,"Pick An Image");
if(option == JFileChooser.APPROVE_OPTION) {
File file = chooser.getSelectedFile();
if(file != null) {
textPane.insertIcon(new ImageIcon(
file.getPath()));
}
}
}
});
}
public static void main(String args[]) {
GJApp.launch(new Test(),
"Using JTextPane",300,300,450,300);
}
}
class GJApp extends WindowAdapter {
static private JPanel statusArea = new JPanel();
static private JLabel status = new JLabel(" ");
public static void launch(final JFrame f, String title,
final int x, final int y,
final int w, int h) {
f.setTitle(title);
f.setBounds(x,y,w,h);
f.setVisible(true);
statusArea.setBorder(BorderFactory.createEtchedBorder());
statusArea.setLayout(new FlowLayout(FlowLayout.LEFT,0,0));
statusArea.add(status);
status.setHorizontalAlignment(JLabel.LEFT);
f.setDefaultCloseOperation(
WindowConstants.DISPOSE_ON_CLOSE);
f.addWindowListener(new WindowAdapter() {
public void windowClosed(WindowEvent e) {
System.exit(0);
}
});
}
static public JPanel getStatusArea() {
return statusArea;
}
static public void updateStatus(String s) {
status.setText(s);
}
}
22.5.2 用属性标记内容
例22-11 在文本窗格中设置字符属性
public class Test extends JFrame {
private JTextPane textPane = new JTextPane();
private JMenuBar menubar = new JMenuBar();
private JToolBar toolbar = new JToolBar();
private Hashtable actionTable = new Hashtable();
private String[] cutCopyPasteActionNames = new String[] {
DefaultEditorKit.cutAction, "Cut", "cut.gif",
DefaultEditorKit.copyAction, "Copy", "copy.gif",
DefaultEditorKit.pasteAction, "Paste", "paste.gif",
};
private String[] familyActionNames = new String[] {
"font-family-SansSerif", "SanSerif",
"font-family-Monospaced", "Monospaced",
"font-family-Serif", "Serif",
};
private String[] styleActionNames = new String[] {
"font-italic", "Italic", "italic.gif",
"font-bold", "Bold", "bold.gif",
"font-underline", "Underline", "underline.gif",
};
private String[] sizeActionNames = new String[] {
"font-size-8", "8", "font-size-10", "10",
"font-size-12", "12", "font-size-14", "14",
"font-size-16", "16", "font-size-18", "18",
"font-size-24", "24", "font-size-36", "36",
"font-size-48", "48",
};
public Test() {
Container contentPane = getContentPane();
JScrollPane scrollPane = new JScrollPane(textPane);
loadActionTable();
populate();
readFile();
setJMenuBar(menubar);
contentPane.add(toolbar, BorderLayout.NORTH);
contentPane.add(new JScrollPane(textPane),
BorderLayout.CENTER);
}
private void readFile() {
try {
textPane.getEditorKit().read(
new FileReader("text"), textPane.getDocument(), 0);
}
catch(Exception ex) { ex.printStackTrace(); }
}
private void populate() {
JMenu editMenu = new JMenu("Edit"),
fontMenu = new JMenu("Font"),
styleMenu = new JMenu("Style"),
sizeMenu = new JMenu("Size"),
familyMenu = new JMenu("Family");
for(int i=0; i < familyActionNames.length; ++i) {
Action action = getAction(familyActionNames[i]);
if(action != null) {
JMenuItem item = familyMenu.add(action);
item.setText(familyActionNames[++i]);
}
}
for(int i=0; i < sizeActionNames.length; ++i) {
Action action = getAction(sizeActionNames[i]);
if(action != null) {
JMenuItem item = sizeMenu.add(action);
item.setText(sizeActionNames[++i]);
}
}
for(int i=0; i < cutCopyPasteActionNames.length; ++i) {
Action action = getAction(cutCopyPasteActionNames[i]);
if(action != null) {
JButton button = toolbar.add(action);
JMenuItem item = editMenu.add(action);
item.setText(cutCopyPasteActionNames[++i]);
button.setText(null);
button.setIcon(new ImageIcon(
cutCopyPasteActionNames[++i]));
}
}
editMenu.addSeparator();
toolbar.addSeparator();
for(int i=0; i < styleActionNames.length; ++i) {
Action action = getAction(styleActionNames[i]);
if(action != null) {
JButton button = toolbar.add(action);
JMenuItem item = styleMenu.add(action);
item.setText(styleActionNames[++i]);
button.setText(null);
button.setIcon(
new ImageIcon(styleActionNames[++i]));
}
}
fontMenu.add(familyMenu);
fontMenu.add(styleMenu);
fontMenu.add(sizeMenu);
editMenu.add(fontMenu);
menubar.add(editMenu);
}
private void loadActionTable() {
Action[] actions = textPane.getActions();
for(int i=0; i < actions.length; ++i) {
actionTable.put(actions[i].getValue(Action.NAME),
actions[i]);
}
}
private Action getAction(String name) {
return (Action)actionTable.get(name);
}
public static void main(String args[]) {
GJApp.launch(new Test(),
"Using JTextPane",300,300,450,300);
}
}
class GJApp extends WindowAdapter {
static private JPanel statusArea = new JPanel();
static private JLabel status = new JLabel(" ");
static private ResourceBundle resources;
static {
resources = ResourceBundle.getBundle(
"GJApp", Locale.getDefault());
};
private GJApp() {}
public static void launch(final JFrame f, String title,
final int x, final int y,
final int w, int h) {
f.setTitle(title);
f.setBounds(x,y,w,h);
f.setVisible(true);
statusArea.setBorder(BorderFactory.createEtchedBorder());
statusArea.setLayout(new FlowLayout(FlowLayout.LEFT,0,0));
statusArea.add(status);
status.setHorizontalAlignment(JLabel.LEFT);
f.setDefaultCloseOperation(
WindowConstants.DISPOSE_ON_CLOSE);
f.addWindowListener(new WindowAdapter() {
public void windowClosed(WindowEvent e) {
System.exit(0);
}
});
}
static public JPanel getStatusArea() {
return statusArea;
}
static public void showStatus(String s) {
status.setText(s);
}
static Object getResource(String key) {
if(resources != null) {
return resources.getString(key);
}
return null;
}
}