import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class FileChooseTest {
public static void main(String[] args){
JFrame frame=new JFrame();
final JTextField textfield=new JTextField();
JButton button= new JButton("Choose File");
button.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
frame.setTitle("testing");
}
});
frame.setLayout(new BorderLayout());
frame.getContentPane().add(button,BorderLayout.NORTH);
frame.getContentPane().add(textfield,BorderLayout.SOUTH);
frame.addWindowListener(new WindowAdapter(){
public void windowClosing (WindowEvent e){
System.exit(0);
}
});
frame.pack();
frame.setVisible(true);
}
}在eclipse编辑以上代码,在 frame.setTitle("testing"); 就出现了标题所示的提示,到底是什么意思呢?在frame定义处加个final就什么提示也没有,为什么呢?
是不是匿名类不能引用非final变量?
import java.awt.event.*;
import javax.swing.*;
public class FileChooseTest {
public static void main(String[] args){
JFrame frame=new JFrame();
final JTextField textfield=new JTextField();
JButton button= new JButton("Choose File");
button.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
frame.setTitle("testing");
}
});
frame.setLayout(new BorderLayout());
frame.getContentPane().add(button,BorderLayout.NORTH);
frame.getContentPane().add(textfield,BorderLayout.SOUTH);
frame.addWindowListener(new WindowAdapter(){
public void windowClosing (WindowEvent e){
System.exit(0);
}
});
frame.pack();
frame.setVisible(true);
}
}在eclipse编辑以上代码,在 frame.setTitle("testing"); 就出现了标题所示的提示,到底是什么意思呢?在frame定义处加个final就什么提示也没有,为什么呢?
是不是匿名类不能引用非final变量?
解决方案 »
- 怎样将字符串中的相同的单词,用空格来代替
- 请问如何在Table中用对勾显示"是否"
- 怎么用java作一个对文件内容搜索的引擎
- 新手请教:怎样才能在DOS任何目录下都能编译运行.java源程序
- 急问:::10分: java命令行中-D定义的属性值中带空格如何处理?
- 调用javax.swing.JFrame时,为什么要这样写??
- 谁能推荐本书给初学者阿?
- 我以前是做系统管理员的,由于工作需要,明天开始学习JAVA,有一问题问一下?
- Class.forName有什么作用?
- 一个奇怪的问题:applet在某些版本的win98下不能运行。
- 光天化日,跪求UDP丢包问题!散分,散分!!!
- 请问在SWT中一个图被缩放后,如何根据鼠标事件的坐标得到原图的坐标?
改成final JFrame frame=new JFrame();
package zhao;import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class FileChooseTest {
static JFrame frame = null;
public static void main(String[] args){
frame=new JFrame();
final JTextField textfield=new JTextField();
JButton button= new JButton("Choose File");
button.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
frame.setTitle("testing");
}
});
frame.setLayout(new BorderLayout());
frame.getContentPane().add(button,BorderLayout.NORTH);
frame.getContentPane().add(textfield,BorderLayout.SOUTH);
frame.addWindowListener(new WindowAdapter(){
public void windowClosing (WindowEvent e){
System.exit(0);
}
});
frame.pack();
frame.setVisible(true);
}
}
button.addActionListener(new ActionListener(){ //因为你在这个类ActionListener用到的frame是main方法里面的变量,
public void actionPerformed(ActionEvent e){
frame.setTitle("testing");
}
});