大虾们帮我看看下面这段代码,

lenshen2k 2005-01-06 11:05:30
package uni8080;

import javax.swing.*;

import java.awt.*;
import java.awt.event.*;
import javax.swing.tree.*;
import javax.swing.event.*;
import javax.swing.border.*;
import javax.swing.table.*;


public class socket8080 extends JFrame
{
/**
* 主模块,初始化所有子模块,并设置主框架的相关属性
*/
public socket8080()
{

RightPanel rightPanel = new RightPanel();
CenterPanel centerPanel = new CenterPanel();
// 设置主框架的布局
Container c = this.getContentPane();
c.add(rightPanel,BorderLayout.EAST);
c.add(centerPanel,BorderLayout.CENTER);

// c.add(bottomPanel,BorderLayout.SOUTH);

// 利用无名内隐类,增加窗口事件
this.addWindowListener(new WindowAdapter()
{
public void WindowClosing(WindowEvent e)
{
// 释放资源,退出程序
dispose();
System.exit(0);
}
});



setSize(500,350);
setTitle("socket通讯");
setLocation(200,150);
show();
}

/**
* 最右边模块,继承JPanel,初始化各种按钮
*/
class RightPanel extends JPanel
{
public RightPanel()
{
this.setLayout(new GridLayout(9,1));


// 初始化各种按钮
//JCheckBox checkBox = new JCheckBox("复选按钮");
JLabel lab0 = new JLabel("");
JLabel lab1 = new JLabel("");
JLabel lab2 = new JLabel("");
JLabel lab3 = new JLabel("");
JLabel lab4 = new JLabel("");
JButton sendbtn = new JButton(" 发 送 ");
sendbtn.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e) {
//dispose();
//System.exit(0);
}
});

JButton startbtn = new JButton(" 启动服务 ");
startbtn.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e) {
Server ufs=new Server();
ufs.getIni();
//setButton("start");
//startbtn=new startbtn
startbtn.setEnabled(false);
}
});

JButton stopbtn = new JButton(" 停止服务 ");
stopbtn.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e) {
stopbtn.setEnabled(false);



//dispose();
//System.exit(0);
}
});

JButton closebtn = new JButton(" 关 闭 ");
closebtn.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e) {
dispose();
System.exit(0);
}
});


add(lab0);
add(sendbtn);
add(lab1);
add(startbtn);
add(lab2);
add(stopbtn);
add(lab3);
add(closebtn);
add(lab4);
this.setBorder(new EtchedBorder(EtchedBorder.LOWERED,Color.LIGHT_GRAY,Color.DARK_GRAY));
}
}

////////////////////////////////////////////////////////////////////////////
/**
* 中间层模块,继承JPanel,初始化页签,并在页签中设置文本区,
* 文本区上下用分隔条分隔
*/
class CenterPanel extends JPanel
{
public CenterPanel()
{
JTabbedPane tab = new JTabbedPane(JTabbedPane.TOP);
JTextPane SendTextPane = new JTextPane();

JTextPane RecTextPane = new JTextPane();
RecTextPane.setEnabled(false);

JTextPane SysTextPane = new JTextPane();
SysTextPane.setEnabled(false) ;
RecTextPane.setCursor(new Cursor(Cursor.TEXT_CURSOR));
RecTextPane.setText("");

tab.addTab("发送信息",SendTextPane);
tab.addTab("受到的信息",RecTextPane);
tab.addTab("系统信息",SysTextPane);

tab.setPreferredSize(new Dimension(380,300));
this.add(tab);
//this.add(SysTextPane);
this.setEnabled(true);
this.setBorder(new EtchedBorder(EtchedBorder.LOWERED,Color.LIGHT_GRAY,Color.DARK_GRAY));
}

}

public static void main(String args[])
{
new socket8080();
}

}

=======================================
这段代码中的这一段
JButton startbtn = new JButton(" 启动服务 ");
startbtn.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e) {
Server ufs=new Server();
ufs.getIni();
//setButton("start");
//startbtn=new startbtn
startbtn.setEnabled(false);
}
});

----------------------------------------
startbtn.setEnabled(false); 这个编译时老是说

"socket8080.java": local variable startbtn is accessed from within inner class; needs to be declared final at line 83, column 33

这是什么原因啊?谢谢了!!
---------------------------------------

=======================================
...全文
102 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
lele2k 2005-01-06
  • 打赏
  • 举报
回复
在外面先声明,如final JButton startbtn = new JButton(" 启动服务 ");
lenshen2k 2005-01-06
  • 打赏
  • 举报
回复
谢谢各位,我试试看!
lenshen2k 2005-01-06
  • 打赏
  • 举报
回复
这样来那不界面那块程序要重做?
skylan 2005-01-06
  • 打赏
  • 举报
回复
或者在在外面先声明,如final JButton button("start") = new JButton(),然后在匿名内部类里面就可以用了
skylan 2005-01-06
  • 打赏
  • 举报
回复
final startbtn=new startbtn()
skylan 2005-01-06
  • 打赏
  • 举报
回复
匿名内部类说无法访问到那个本地变量,把startbtn=new startbtn改成final startbtn=new startbtn
tomcatjava 2005-01-06
  • 打赏
  • 举报
回复
匿名内部类说无法访问到那个本地变量,超出它的作用域了。

建议把那些JButton声明为类的成员变量,就可以了

62,614

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧