我写了一个小程序 但是有个问题搞不懂 请指教 我在上面做了标记
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
public class LeiBuLeiDemo
{
public static void main(String args[])
{
ButtonFrame frame = new ButtonFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.show();
}
}
class ButtonFrame extends JFrame
{
private static final int WIDTH = 400;
private static final int HEIGHT = 300;
public ButtonFrame()
{
JButton btn1 = new JButton("按钮1");
final JButton btn2 = new JButton("按钮2"); /////为什么btn2这个按钮一定要声明为final
Container con = getContentPane();
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
Dimension frameSize = this.getSize();
setLocation((screenSize.width - WIDTH)/2,(screenSize.height - HEIGHT)/2);
setSize(WIDTH,HEIGHT);
Image image = Toolkit.getDefaultToolkit().getImage("G:/JAVA/Book/bookmark.gif");
setIconImage(image);
setTitle("四个不同外观的按钮");
btn1.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
if(btn2.getBackground() == Color.pink)
btn2.setBackground(Color.red);
else
btn2.setBackground(Color.pink);
}
}
);
con.setLayout(new FlowLayout());
con.add(btn1);
con.add(btn2);
}
}