小弟刚学JAVA.今天照着一个例子打代码。 我只是稍微按自己的想法改动了一点,就运行不了了,自己也找不到错哪。想请高人指点一下,化解小弟的疑惑!谢谢
TestComboBoxDemo.java
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.util.*;
import java.text.SimpleDateFormat;
import javax.swing.border.*;public class TestComboBoxDemo 
{
public static void main(String []args)
{
JFrame.setDefaultLookAndFeelDecorated(true);
JFrame frame = new JFrame("ComboBoxDemo");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

JComponent newcontentPane = new ComboBoxDemo();

//setOpaque()如果为true则该组件绘制其边界内的所有像素,否则该组件可能不绘制其某些或所有像素,从而容许其下面的像素透露出来
newcontentPane.setOpaque(true);

frame.setContentPane(newcontentPane);

frame.pack();//调整此窗口的大小,以适合其组件的首选大小和布局
frame.setVisible(true);
}}public class ComboBoxDemo extends JPanel implements ActionListener
{
static JFrame frame;
JLabel result;
String currentPattern;

public ComboBoxDemo()
{
//setLayout(new BoxLayout(this,BoxLayout.PAGE_AXIS));//创建一个将沿给定轴放置组件的布局管理器
setLayout(new BoxLayout(this,BoxLayout.PAGE_AXIS));//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.MMMM.dd GGG hh:mm aaa"
};

currentPattern = patternExamples[0];

JLabel patternLabel1 = new JLabel("输入一个字符格式或者");
JLabel patternLabel2 = new JLabel("从下拉列表中选择一种:");

JComboBox patternList = new JComboBox(patternExamples);
patternList.setEditable(true);
patternList.addActionListener(this);

//创建一个显示结果用户界面
JLabel resultLabel = new JLabel("当前 日期/时间",JLabel.LEADING);

JLabel 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(this,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)));//创建一个总是具有指定大小的不可见组件,0,10为宽度和高度
add(resultPanel);
setBorder(BorderFactory.createEmptyBorder(10,10,10,10));
reformat();
}

public void actionPerformed(ActionEvent e)
{
JComboBox cb = (JComboBox)e.getSource();
String newSelection = (String)cb.getSelectedItem();
currentPattern = newSelection;
reformat();
} /* Format 是一个用于格式化语言环境敏感的信息(如日期、消息和数字)的抽象基类.
 * Format 定义了编程接口,用于将语言环境敏感的对象格式化为 String(使用 format 方法)
 * 和将 String 重新分析为对象(使用 parseObject 方法)
 */
public void reformat()
{
Date today = new Date();
//SimpleDateFormat(str)用给定的currentPattern模式和默认语言环境的日期格式符号构造 SimpleDateFormat
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());
}
}

}