河南成教 / Java程序设计
阅读下列程序,写出程序功能。
import java.awt.*;
import java.applet.*;
import javax.swing.*;
import java.awt.event.*;
public class C3402 extends Applet
{
static JFrame myWin;
static SetBG setbg=new SetBG();
static JButton button;
public void init()
{
myWin=new JFrame("C3402");
myWin.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
myWin.setBounds(100,200,300,400);
myWin.setLayout(new FlowLayout());
Container con=myWin.getContentPane();
con.setBackground(Color.red);
button=new JButton("OK");
button.addActionListener(setbg);
myWin.add(button);
myWin.setVisible(true);
}
}
class SetBG implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
Container con=C3402.myWin.getContentPane();
if (con.getBackground()==Color.red)
con.setBackground(Color.green);
else
con.setBackground(Color.red);
}
}
import java.awt.*;
import java.applet.*;
import javax.swing.*;
import java.awt.event.*;
public class C3402 extends Applet
{
static JFrame myWin;
static SetBG setbg=new SetBG();
static JButton button;
public void init()
{
myWin=new JFrame("C3402");
myWin.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
myWin.setBounds(100,200,300,400);
myWin.setLayout(new FlowLayout());
Container con=myWin.getContentPane();
con.setBackground(Color.red);
button=new JButton("OK");
button.addActionListener(setbg);
myWin.add(button);
myWin.setVisible(true);
}
}
class SetBG implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
Container con=C3402.myWin.getContentPane();
if (con.getBackground()==Color.red)
con.setBackground(Color.green);
else
con.setBackground(Color.red);
}
}
参考答案:
佳题速递: