河南成教 / Java程序设计
阅读下列程序,写出程序功能。
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
public class C3401 implements ActionListener
{
JFrame frmFrame;
JButton btnUp, btnDown;
public C3401()
{
frmFrame = new JFrame("按钮应用");
frmFrame.setLayout(new GridLayout(2,1));
btnUp = new JButton("上");
btnUp.setActionCommand("btnUp");
btnUp.addActionListener(this);
btnDown = new JButton("下");
btnDown.setActionCommand("btnDown");
btnDown.addActionListener(this);
frmFrame.add(btnUp);
frmFrame.add(btnDown);
frmFrame.pack();
frmFrame.setVisible(true);
}
public void actionPerformed(ActionEvent e)
{
String cmd = e.getActionCommand();
if ( cmd.equals("btnDown") )
if (btnUp.getBackground()==Color.BLUE)
btnUp.setBackground(Color.red);
else
btnUp.setBackground(Color.blue);
else
if (btnDown.getBackground()==Color.BLUE)
btnDown.setBackground(Color.red);
else
btnDown.setBackground(Color.blue);
}
public static void main(String[] args)
{
new C3401();
}
}
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
public class C3401 implements ActionListener
{
JFrame frmFrame;
JButton btnUp, btnDown;
public C3401()
{
frmFrame = new JFrame("按钮应用");
frmFrame.setLayout(new GridLayout(2,1));
btnUp = new JButton("上");
btnUp.setActionCommand("btnUp");
btnUp.addActionListener(this);
btnDown = new JButton("下");
btnDown.setActionCommand("btnDown");
btnDown.addActionListener(this);
frmFrame.add(btnUp);
frmFrame.add(btnDown);
frmFrame.pack();
frmFrame.setVisible(true);
}
public void actionPerformed(ActionEvent e)
{
String cmd = e.getActionCommand();
if ( cmd.equals("btnDown") )
if (btnUp.getBackground()==Color.BLUE)
btnUp.setBackground(Color.red);
else
btnUp.setBackground(Color.blue);
else
if (btnDown.getBackground()==Color.BLUE)
btnDown.setBackground(Color.red);
else
btnDown.setBackground(Color.blue);
}
public static void main(String[] args)
{
new C3401();
}
}
参考答案:
佳题速递: