GUI 监听器问题 运行空指针异常
package java9;import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
import javax.xml.bind.Marshaller.Listener;
public class Example7 extends JFrame implements ActionListener{
JCheckBox JC1;
JCheckBox JC2;
JCheckBox JC3;
JTextArea JT1;
Example7()
{
this.setSize(300, 300);
this.setTitle("我的窗体");
//添加复选框
JCheckBox JC1= new JCheckBox("艺术");
JCheckBox JC2= new JCheckBox("跳舞");
JCheckBox JC3= new JCheckBox("画画");
//创建面板
JPanel P1=new JPanel();
JPanel P3=new JPanel();
JT1=new JTextArea(1,1);
//讲复选框,单选按钮加入面板
P1.add(JC1);
P1.add(JC2);
P1.add(JC3);
P3.add(JT1);
//
//将面板,文本域加入窗体
this.add(P1,BorderLayout.NORTH);
this.add(P3,BorderLayout.SOUTH);
JC1.addActionListener(this);
JC2.addActionListener (this);
JC3.addActionListener(this);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setVisible(true);}
public void actionPerformed(ActionEvent e) {
String a="";
if(JC1.isSelected())
{
a+="艺术";
}
if(JC2.isSelected())
{
a+="跳舞";
}
if(JC3.isSelected())
{
a+="画画";
}
JT1.setText("我喜欢的活动:"+a+"\n");
}
public static void main(String[] args) {
new Example7();
}
}