JXMonthViewDemoPanel.java
上传用户:zhengdagz
上传日期:2014-03-06
资源大小:1956k
文件大小:5k
源码类别:

xml/soap/webservice

开发平台:

Java

  1. package org.jdesktop.demo.swingx;
  2. import java.awt.*;
  3. import java.awt.event.ActionEvent;
  4. import java.awt.event.ActionListener;
  5. import java.util.Calendar;
  6. import javax.swing.*;
  7. import org.jdesktop.demo.DemoPanel;
  8. import org.jdesktop.swingx.calendar.*;
  9. /**
  10.  * Show off the month view demo panel.
  11.  *
  12.  * @author Joshua Outwater
  13.  */
  14. public class JXMonthViewDemoPanel extends DemoPanel implements ActionListener {
  15. private JXMonthView monthView;
  16. private JComboBox dayOfWeekComboBox;
  17. private JColorChooser colorChooser;
  18. private JDialog colorDialog;
  19.     
  20.     public JXMonthViewDemoPanel() {
  21.         initComponents();
  22.     }
  23.     public String getHtmlDescription() {
  24.         return "Demonstrates the month view component.  By default the component " +
  25.             "will display as many months as possible in the area provided.  It " +
  26.             "supports various properties, some of which can be played with by " +
  27.             "using the control panel at the bottom of the demo.";
  28.     }
  29.     
  30.     public String getName() {
  31.         return "More Date Selection";
  32.     }
  33.     
  34.     public Container getContents() {
  35.         return this;
  36.     }
  37.     public void initComponents() {
  38.         monthView = new JXMonthView();
  39.         monthView.setFirstDayOfWeek(Calendar.MONDAY);
  40.         monthView.setSelectionMode(JXMonthView.NO_SELECTION);
  41.         monthView.setTodayBackground(Color.BLUE);
  42.         // Create controller panel
  43.         JPanel controlPanel = new JPanel(new GridLayout(2, 1));
  44.         JPanel panel = new JPanel(new FlowLayout(FlowLayout.LEADING));
  45.         
  46.         JButton button = new JButton("Toggle orientation");
  47.         button.addActionListener(new ActionListener() {
  48.             public void actionPerformed(ActionEvent ev) {
  49.                 monthView.setComponentOrientation(
  50.                     (monthView.getComponentOrientation() ==
  51.                         ComponentOrientation.RIGHT_TO_LEFT) ?
  52.                             ComponentOrientation.LEFT_TO_RIGHT :
  53.                             ComponentOrientation.RIGHT_TO_LEFT);
  54.                 monthView.repaint();
  55.             }
  56.         });
  57.         panel.add(button);
  58.         panel.add(Box.createHorizontalStrut(5));
  59.         
  60.         JLabel label = new JLabel("Selection Mode:");
  61.         panel.add(label);
  62.         JComboBox cBox = new JComboBox(new String[] { "None", "Single",
  63.             "Multiple", "Week" });
  64.         cBox.addActionListener(new ActionListener() {
  65.             public void actionPerformed(ActionEvent ev) {
  66.                 JComboBox c = (JComboBox)ev.getSource();
  67.                 int index = c.getSelectedIndex();
  68.                 monthView.setSelectedDateSpan(null);
  69.                 monthView.setSelectionMode(index);
  70.             }
  71.         });
  72.         panel.add(cBox);
  73.         panel.add(Box.createHorizontalStrut(5));
  74.         
  75.         label = new JLabel("Anti-aliased text:");
  76.         panel.add(label);
  77.         JCheckBox checkBox = new JCheckBox();
  78.         checkBox.addActionListener(new ActionListener() {
  79.             public void actionPerformed(ActionEvent ev) {
  80.                 JCheckBox c = (JCheckBox)ev.getSource();
  81.                 monthView.setAntialiased(c.isSelected());
  82.             }
  83.         });
  84.         panel.add(checkBox);
  85.         panel.add(Box.createHorizontalStrut(5));
  86.         controlPanel.add(panel);
  87.         
  88.         panel = new JPanel(new FlowLayout(FlowLayout.LEADING));
  89.         label = new JLabel("Traversable:");
  90.         panel.add(label);
  91.         checkBox = new JCheckBox();
  92.         checkBox.addActionListener(new ActionListener() {
  93.             public void actionPerformed(ActionEvent ev) {
  94.                 JCheckBox c = (JCheckBox)ev.getSource();
  95.                 monthView.setTraversable(c.isSelected());
  96.             }
  97.         });
  98.         panel.add(checkBox);
  99.         panel.add(Box.createHorizontalStrut(5));
  100.         // Create a combo box with the days of the week and add a button that will
  101.         // show a color chooser to allow the user to select a color.
  102.         dayOfWeekComboBox = new JComboBox(new String[] { "Sunday", "Monday", "Tuesday",
  103.             "Wednesday", "Thursday", "Friday" });
  104.         colorChooser = new JColorChooser();
  105.         colorDialog = colorChooser.createDialog(this,
  106.                                                 "Choose a color", true, colorChooser, this, this);
  107.         button = new JButton("Select A Color!");
  108.         button.addActionListener(new ActionListener() {
  109.             public void actionPerformed(ActionEvent ev) {
  110.                 colorDialog.setVisible(true);
  111.             }
  112.         });
  113.         panel.add(dayOfWeekComboBox);
  114.         panel.add(button);
  115.         controlPanel.add(panel);
  116.         setLayout(new BorderLayout());
  117.         add(controlPanel, BorderLayout.SOUTH);
  118.         add(monthView, BorderLayout.CENTER);
  119.     }               
  120.     public void actionPerformed(ActionEvent ev) {
  121.         String command = ev.getActionCommand();
  122.         if (command == "OK") {
  123.             int index = dayOfWeekComboBox.getSelectedIndex();
  124.             monthView.setDayForeground(index + 1, colorChooser.getColor());
  125.             monthView.repaint();
  126.         }
  127.     }
  128. }