大概的内容是把一个按钮添加了事件,点击后在右侧文字,但不明原因要把标签类定义成final类,代码如下:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;public class Xck {
public static void main (String[] args) {
JFrame f=new JFrame("my first frame");
f.setSize(250,100);
f.setVisible(true);
/*try{UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());}
catch(Exception e){}
Frame.setDefaultCloseOperation(Frame.EXIT_ON_CLOSE);
f.dispose();*/
/*f.addWindowListener(new WindowAdapter()//WindowAdapter是抽象类,实现了所有的WindowListener方法
{
public void windowClosing(WindowEvent e)
{ System.exit(0);}
});*/
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Panel p=new Panel();
p.setLayout(new GridLayout(1,2));
Button b=new Button("这是按钮");
final Label l=new Label("这是标签");
b.addActionListener(
new ActionListener()
{
public void actionPerformed(ActionEvent e){l.setText("click!");}
}
);
p.add(b);
p.add(l);
f.add(p);
}}
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;public class Xck {
public static void main (String[] args) {
JFrame f=new JFrame("my first frame");
f.setSize(250,100);
f.setVisible(true);
/*try{UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());}
catch(Exception e){}
Frame.setDefaultCloseOperation(Frame.EXIT_ON_CLOSE);
f.dispose();*/
/*f.addWindowListener(new WindowAdapter()//WindowAdapter是抽象类,实现了所有的WindowListener方法
{
public void windowClosing(WindowEvent e)
{ System.exit(0);}
});*/
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Panel p=new Panel();
p.setLayout(new GridLayout(1,2));
Button b=new Button("这是按钮");
final Label l=new Label("这是标签");
b.addActionListener(
new ActionListener()
{
public void actionPerformed(ActionEvent e){l.setText("click!");}
}
);
p.add(b);
p.add(l);
f.add(p);
}}
解决方案 »
- 一道很有意思的笔试题:继承中关于super的相关问题
- photoshop
- 为什么我的jbuilder9不能用了?就算是新建一个新的application,什么都不加直接编译还是出错?
- 有谁了解上海和为科技有限公司 !正在找工作中,多谢
- 請問有誰用過solex這個軟件,是一個網絡測試軟件,我下載後是擴展名為jar的文件,請問怎麼安裝
- 关于swing中JTextField限长的问题
- 怎样编写动画程序?
- 寻求牌类游戏源代码
- 怎样设置一个页面过期?清囊相赠!!!
- java.lang.ArrayIndexOutOfBoundsException: Index 1 out of bounds for length 1
- AudioSystem分段写入wav文件 求大虾解答 在线等啊
- 项目紧急求助,关于java编写的飞机救援问题
public class Xck {
public static JLabel g=new JLabel(); public static void main(String[] args) {
JFrame f = new JFrame("my first frame");
f.setSize(250, 100);
f.setVisible(true);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Panel p = new Panel();
p.setLayout(new GridLayout(1, 2));
Button b = new Button("这是按钮");
JLabel l = Xck.g;
b.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e) {
Xck.g.setText("这是标签");
}
}
);
p.add(b);
p.add(l);
f.add(p);
}
}
在内部类里面访问局部变量必须将其设置为final