- import javax.swing.*;
-
- import java.awt.*;
-
- import java.awt.event.*;
-
- public class Test
- {
-
- public static void main(String[] args)
- {
-
- new MyFrame();
-
- }
-
- }
-
- class MyFrame
-
- {
-
- MyFrame()
-
- {
-
- JFrame f = new JFrame("MyFrame");
-
- f.setLayout(new BorderLayout());
-
- f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
-
- f.setVisible(true);
-
- f.setBounds(120,100,200,230);
-
- JLabel lab = new JLabel();
-
- JButton btn = new JButton();
-
- btn.addActionListener(new ActionListener()
-
- {
-
- public void actionPerformed(ActionEvent e)
-
- {
-
- lab.setText("success!");//这里提示:从内部类中访问局部变量 lab;需要被声明为最终类型!
-
- //有没有方法不设置成final类型,而用一个控件控制另一个控件的动作?
-
- //System.out.println("button");
-
- }
-
- });
-
- f.add(lab,BorderLayout.SOUTH);
-
- f.add(btn,BorderLayout.EAST);
-
- }
-
- }
复制代码 |