阅读程序,写出程序功能。
import javax.swing. *;
import java.awt.event. *;
public class Class33 extends JFrame implements ActionListener
{
JButton b1=new JButton("b1");
JButton b2=new JButton("b2");
public Class33( )
{
super("Class33");
setSize(300,100);
setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
JPanel panel=new JPanel( );
panel.add(b1);panel.add(b2);
setContentPane (panel);
b1.addActionListener(this);
b2.addActionListener(this);
setVisible (true);
}
public void actionPerformed(ActionEvent e)
{
Object s=e.getSource( );
if (s= =b1)setTitle("Teacher");
if (s= =b2)setTitle("Student");
}
public static void main(String [ ] args)
{
new Class33( );
}
}
【正确答案】程序功能:在窗口面板上添加了两个按钮,当单击b1按钮时窗口标题变为Teacher,单击b2按钮时窗口标题变为Student。
【答案解析】 程序首先添加了两个按钮,然后通过addActionListener(this)为这两个按钮添加了点击事件。添加的事件是通过setTitle( )设置窗口的标题。