📄 jcalendardemo.java
字号:
}
// the help menu
JMenu helpMenu = new JMenu("Help");
helpMenu.setMnemonic('H');
JMenuItem aboutItem = helpMenu.add(new AboutAction(this));
aboutItem.setMnemonic('A');
aboutItem.setAccelerator(KeyStroke.getKeyStroke('A', java.awt.Event.CTRL_MASK));
menuBar.add(helpMenu);
return menuBar;
}
/**
* The applet is a PropertyChangeListener for "locale" and "calendar".
*
* @param evt
* Description of the Parameter
*/
public void propertyChange(PropertyChangeEvent evt) {
if (calendarPanel != null) {
if (evt.getPropertyName().equals("calendar")) {
// calendar = (Calendar) evt.getNewValue();
// DateFormat df = DateFormat.getDateInstance(DateFormat.LONG,
// jcalendar.getLocale());
// dateField.setText(df.format(calendar.getTime()));
}
}
}
/**
* Creates a JFrame with a JCalendarDemo inside and can be used for testing.
*
* @param s
* The command line arguments
*/
public static void main(String[] s) {
WindowListener l = new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
};
JFrame frame = new JFrame("JCalendar Demo");
frame.addWindowListener(l);
JCalendarDemo demo = new JCalendarDemo();
demo.init();
frame.getContentPane().add(demo);
frame.pack();
frame.setBounds(200, 200, (int) frame.getPreferredSize().getWidth() + 20, (int) frame
.getPreferredSize().getHeight() + 180);
frame.setVisible(true);
}
/**
* Installes a demo bean.
*
* @param bean
* the demo bean
*/
private void installBean(JComponent bean) {
try {
componentPanel.removeAll();
componentPanel.add(bean);
BeanInfo beanInfo = Introspector.getBeanInfo(bean.getClass(), bean.getClass()
.getSuperclass());
PropertyDescriptor[] propertyDescriptors = beanInfo.getPropertyDescriptors();
propertyPanel.removeAll();
GridBagLayout gridbag = new GridBagLayout();
GridBagConstraints c = new GridBagConstraints();
c.fill = GridBagConstraints.BOTH;
propertyPanel.setLayout(gridbag);
int count = 0;
String[] types = new String[] { "class java.util.Locale", "boolean", "int",
"class java.awt.Color", "class java.util.Date", "class java.lang.String" };
for (int t = 0; t < types.length; t++) {
for (int i = 0; i < propertyDescriptors.length; i++) {
if (propertyDescriptors[i].getWriteMethod() != null) {
String type = propertyDescriptors[i].getPropertyType().toString();
final PropertyDescriptor propertyDescriptor = propertyDescriptors[i];
final JComponent currentBean = bean;
final Method readMethod = propertyDescriptor.getReadMethod();
final Method writeMethod = propertyDescriptor.getWriteMethod();
if (type.equals(types[t])
&& (((readMethod != null) && (writeMethod != null)) || ("class java.util.Locale"
.equals(type)))) {
if ("boolean".equals(type)) {
boolean isSelected = false;
try {
Boolean booleanObj = ((Boolean) readMethod.invoke(bean, null));
isSelected = booleanObj.booleanValue();
} catch (Exception e) {
e.printStackTrace();
}
final JCheckBox checkBox = new JCheckBox("", isSelected);
checkBox.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent event) {
try {
if (checkBox.isSelected()) {
writeMethod.invoke(currentBean,
new Object[] { new Boolean(true) });
} else {
writeMethod.invoke(currentBean,
new Object[] { new Boolean(false) });
}
} catch (Exception e) {
e.printStackTrace();
}
}
});
addProperty(propertyDescriptors[i], checkBox, gridbag);
count += 1;
} else if ("int".equals(type)) {
JSpinField spinField = new JSpinField();
spinField.addPropertyChangeListener(new PropertyChangeListener() {
public void propertyChange(PropertyChangeEvent evt) {
try {
if (evt.getPropertyName().equals("value")) {
writeMethod.invoke(currentBean, new Object[] { evt
.getNewValue() });
}
} catch (Exception e) {
}
}
});
try {
Integer integerObj = ((Integer) readMethod.invoke(bean, null));
spinField.setValue(integerObj.intValue());
} catch (Exception e) {
e.printStackTrace();
}
addProperty(propertyDescriptors[i], spinField, gridbag);
count += 1;
} else if ("class java.lang.String".equals(type)) {
String string = "";
try {
string = ((String) readMethod.invoke(bean, null));
} catch (Exception e) {
e.printStackTrace();
}
JTextField textField = new JTextField(string);
ActionListener actionListener = new ActionListener() {
public void actionPerformed(ActionEvent e) {
try {
writeMethod.invoke(currentBean, new Object[] { e
.getActionCommand() });
} catch (Exception ex) {
}
}
};
textField.addActionListener(actionListener);
addProperty(propertyDescriptors[i], textField, gridbag);
count += 1;
} else if ("class java.util.Locale".equals(type)) {
JLocaleChooser localeChooser = new JLocaleChooser(bean);
localeChooser.setPreferredSize(new Dimension(200, localeChooser
.getPreferredSize().height));
addProperty(propertyDescriptors[i], localeChooser, gridbag);
count += 1;
} else if ("class java.util.Date".equals(type)) {
Date date = null;
try {
date = ((Date) readMethod.invoke(bean, null));
} catch (Exception e) {
e.printStackTrace();
}
JDateChooser dateChooser = new JDateChooser(date);
dateChooser.addPropertyChangeListener(new PropertyChangeListener() {
public void propertyChange(PropertyChangeEvent evt) {
try {
if (evt.getPropertyName().equals("date")) {
writeMethod.invoke(currentBean, new Object[] { evt
.getNewValue() });
}
} catch (Exception e) {
}
}
});
addProperty(propertyDescriptors[i], dateChooser, gridbag);
count += 1;
} else if ("class java.awt.Color".equals(type)) {
final JButton button = new JButton();
try {
final Color colorObj = ((Color) readMethod.invoke(bean, null));
button.setText("...");
button.setBackground(colorObj);
ActionListener actionListener = new ActionListener() {
public void actionPerformed(ActionEvent e) {
Color newColor = JColorChooser.showDialog(
JCalendarDemo.this, "Choose Color", colorObj);
button.setBackground(newColor);
try {
writeMethod.invoke(currentBean,
new Object[] { newColor });
} catch (Exception e1) {
e1.printStackTrace();
}
}
};
button.addActionListener(actionListener);
} catch (Exception e) {
e.printStackTrace();
}
addProperty(propertyDescriptors[i], button, gridbag);
count += 1;
}
}
}
}
}
URL iconURL = bean.getClass().getResource("images/" + bean.getName() + "Color16.gif");
ImageIcon icon = new ImageIcon(iconURL);
componentTitlePanel.setTitle(bean.getName(), icon);
bean.invalidate();
propertyPanel.invalidate();
componentPanel.invalidate();
componentPanel.repaint();
} catch (IntrospectionException e) {
e.printStackTrace();
}
}
private void addProperty(PropertyDescriptor propertyDescriptor, JComponent editor,
GridBagLayout grid) {
String text = propertyDescriptor.getDisplayName();
String newText = "";
for (int i = 0; i < text.length(); i++) {
char c = text.charAt(i);
if (((c >= 'A') && (c <= 'Z')) || (i == 0)) {
if (i == 0) {
c += ('A' - 'a');
}
newText += (" " + c);
} else {
newText += c;
}
}
JLabel label = new JLabel(newText + ": ", null, JLabel.RIGHT);
GridBagConstraints c = new GridBagConstraints();
c.weightx = 1.0;
c.fill = GridBagConstraints.BOTH;
grid.setConstraints(label, c);
propertyPanel.add(label);
c.gridwidth = GridBagConstraints.REMAINDER;
grid.setConstraints(editor, c);
propertyPanel.add(editor);
JPanel blankLine = new JPanel() {
private static final long serialVersionUID = 4514530330521503732L;
public Dimension getPreferredSize() {
return new Dimension(10, 2);
}
};
grid.setConstraints(blankLine, c);
propertyPanel.add(blankLine);
}
/**
* Action to show the About dialog
*
* @author toedter_k
*/
class AboutAction extends AbstractAction {
private static final long serialVersionUID = -5204865941545323214L;
private JCalendarDemo demo;
/**
* Constructor for the AboutAction object
*
* @param demo
* Description of the Parameter
*/
AboutAction(JCalendarDemo demo) {
super("About...");
this.demo = demo;
}
/**
* Description of the Method
*
* @param event
* Description of the Parameter
*/
public void actionPerformed(ActionEvent event) {
JOptionPane
.showMessageDialog(
demo,
"JCalendar Demo\nVersion 1.3.2\n\nKai Toedter\nkai@toedter.com\nwww.toedter.com",
"About...", JOptionPane.INFORMATION_MESSAGE);
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -