import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.border.*;
import java.util.*;
import java.text.SimpleDateFormat;
/**
* <p>Title: ComboBox下拉域演示</p>
* <p>Description: 通過選擇或這輸入一種日期格式來格式化今天的日期</p>
* <p>Copyright: Copyright (c) 2003</p>
* <p>Filename: ComboBoxDemo.java</p>
* @version 1.0
*/
public class ComboBoxDemo extends JPanel
implements ActionListener {
static JFrame frame;
JLabel result;
String currentPattern;
/**
*<br>方法說明:構造器。初始化窗體構件
*<br>輸入參數:
*<br>返回類型:
*/
public ComboBoxDemo() {
setLayout(new BoxLayout(this, BoxLayout.PAGE_AXIS));
String[] patternExamples = {
"dd MMMMM yyyy",
"dd.MM.yy",
"MM/dd/yy",
"yyyy.MM.dd G ´at´ hh:mm:ss z",
"EEE, MMM d, ´´yy",
"h:mm a",
"H:mm:ss:SSS",
"K:mm a,z",
"yyyy.MMMMM.dd GGG hh:mm aaa"
};
currentPattern = patternExamples[0];
//設置一個規范的用戶界面
JLabel patternLabel1 = new JLabel("輸入一個字符格式或者");
JLabel patternLabel2 = new JLabel("從下拉列表中選擇一種:");
JComboBox patternList = new JComboBox(patternExamples);
patternList.setEditable(true);//標注這裡ComboBox可進行編輯
patternList.addActionListener(this);
//創建一個顯示結果用戶界面
JLabel resultLabel = new JLabel("當前 日期/時間",
JLabel.LEADING);//相當於LEFT
result = new JLabel(" ");
result.setForeground(Color.black);
result.setBorder(BorderFactory.createCompoundBorder(
BorderFactory.createLineBorder(Color.black),
BorderFactory.createEmptyBorder(5,5,5,5)
));
//布置構件
JPanel patternPanel = new JPanel();
patternPanel.setLayout(new BoxLayout(patternPanel,
BoxLayout.PAGE_AXIS));
patternPanel.add(patternLabel1);
patternPanel.add(patternLabel2);
patternList.setAlignmentX(Component.LEFT_ALIGNMENT);
patternPanel.add(patternList);
JPanel resultPanel = new JPanel(new GridLayout(0, 1));
resultPanel.add(resultLabel);
resultPanel.add(result);
patternPanel.setAlignmentX(Component.LEFT_ALIGNMENT);
resultPanel.setAlignmentX(Component.LEFT_ALIGNMENT);
add(patternPanel);
add(Box.createRigidArea(new Dimension(0, 10)));
add(resultPanel);
setBorder(BorderFactory.createEmptyBorder(10,10,10,10));
reformat();
}
/**
*<br>方法說明:事件處理
*<br>輸入參數:
*<br>返回類型:
*/
public void actionPerformed(ActionEvent e) {
JComboBox cb = (JComboBox)e.getSource();
String newSelection = (String)cb.getSelectedItem();
currentPattern = newSelection;
reformat();
}
/**
*<br>方法說明:格式和顯示今天的日期
*<br>輸入參數:
*<br>返回類型:
*/
public void reformat() {
Date today = new Date();
SimpleDateFormat formatter =
new SimpleDateFormat(currentPattern);
try {
String dateString = formatter.format(today);
result.setForeground(Color.black);
result.setText(dateString);
} catch (IllegalArgumentException iae) {
result.setForeground(Color.red);
result.setText("Error: " + iae.getMessage());
}
}
/**
*<br>方法說明:主方法
*<br>輸入參數:
*<br>返回類型:
*/
public static void main(String[] args) {
JFrame.setDefaultLookAndFeelDecorated(true);
//創建一個窗體
frame = new JFrame("ComboBoxDemo");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//創建一個面版容器
JComponent newContentPane = new ComboBoxDemo();
newContentPane.setOpaque(true);
frame.setContentPane(newContentPane);
//顯示窗體
frame.pack();
frame.setVisible(true);
}
}