import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class CheckBoxTest {
public static void main(String[]args){
EventQueue.invokeLater(new Runnable(){
public void run(){
CheckBoxFrame f=new CheckBoxFrame();
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setVisible(true);
}
});
}
}
class CheckBoxFrame extends JFrame{
public CheckBoxFrame(){
setTitle("CheckBoxTest");
setSize(DEFAULT_WIDTH,DEFAULT_HEIGHT);
label=new JLabel("wish you a happy day!");
label.setFont(new Font("Serif",Font.PLAIN,DEFAULT_SIZE));
add(label,BorderLayout.CENTER);


ActionListener listener=new ActionListener(){
public void actionPerformed(ActionEvent event){
int mode=0;
if (bold.isSelected())mode+=Font.BOLD;
if (italic.isSelected())mode+=Font.ITALIC;
label.setFont(new Font("Serif",mode,DEFAULT_SIZE));
}
};
JPanel buttonPanel=new JPanel();
bold=new JCheckBox("Bold");
bold.addActionListener(listener);
buttonPanel.add(bold);

italic=new JCheckBox("Italic");
italic.addActionListener(listener);
buttonPanel.add(italic);

add(buttonPanel ,BorderLayout.SOUTH);
}
public static final int DEFAULT_WIDTH=300;
public static final int DEFAULT_HEIGHT=200;


private JLabel label;
private JCheckBox bold;
private JCheckBox italic;
private static final int DEFAULT_SIZE=12;
}请问label=new JLabel("wish you a happy day!");
label.setFont(new Font("Serif",Font.PLAIN,DEFAULT_SIZE));
中,第二个句子是对字体的初始化吗??ActionListener listener=new ActionListener(){
public void actionPerformed(ActionEvent event){
int mode=0;
if (bold.isSelected())mode+=Font.BOLD;
if (italic.isSelected())mode+=Font.ITALIC;
label.setFont(new Font("Serif",mode,DEFAULT_SIZE));
}
};
请问这一段中最后为什么要用分号啊??谢谢……

解决方案 »

  1.   

    1.是的
    2.一句句子结束当然要用分号啦
    {
                public void actionPerformed(ActionEvent event){
                    int mode=0;
                    if (bold.isSelected())mode+=Font.BOLD;
                    if (italic.isSelected())mode+=Font.ITALIC;
                    label.setFont(new Font("Serif",mode,DEFAULT_SIZE));
                }
            }
    你把这段先去掉就容易看明白了
      

  2.   

    第一个问题,是
    第二个问题,这是匿名内部类,你不看里面的ActionListener listener=new ActionListener();这就是个正常的语句,当然要加分号了
      

  3.   

    怎么能是匿名类呢?这样才是吧
    this.addWindowListener(new ActionListener(){
               public void actionPerformed(ActionEvent event){
                    int mode=0;
                    if (bold.isSelected())mode+=Font.BOLD;
                    if (italic.isSelected())mode+=Font.ITALIC;
                    label.setFont(new Font("Serif",mode,DEFAULT_SIZE));
                }});