为什么一定要加final

import java.awt.Font;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;

public class MylistenerDemo03 {
  public static void main(String[] args) {
   // TODO Auto-generated method stub
   JFrame frame = new JFrame("欢迎来到幻界");
   final JButton but = new JButton("显示");
   final JLabel lab = new JLabel();
   final JTextField text = new JTextField(10);
   JPanel pan = new JPanel();
   Font font =new Font("Serief",Font.ITALIC + Font.BOLD,28);
   lab.setFont(font);
   lab.setText("等待用户输入信息!");
   but.addActionListener(new ActionListener() {
    public void actionPerformed(ActionEvent e) {
     if(e.getSource()== but){
      lab.setText(text.getText());
     }
    }
   });
   frame.addWindowListener(new WindowAdapter() {
    public void windowClosing(WindowEvent e){
     System.out.println("窗口正在关闭....");
    }
   });
   frame.setLayout(new GridLayout(2,1));
   pan.setLayout(new GridLayout(1,2));
   pan.add(text);
   pan.add(but);
   frame.add(pan);
   frame.add(lab);
   frame.pack();
   frame.setSize(300, 150);
   frame.setLocation(150, 200);
   frame.setVisible(true);
  }

}






为什么上面的JButton,JLabel,JTextField都要加上final才能运行

你可能感兴趣的:(final)