在一个类中申明文本框,然后加上监听器。可是在监听器中的方法怎么不能访问这个文本框 ? 应该怎么 申明 ? public class JFrame1 extends JFrame
{
public JButton bt = null;
public JTextField tx = new JTextField();
//tx.getDocument().addDocumentListener(new Swing_OnValueChanged ());
public JFrame1()
{
JFrame F = new JFrame("TextListener");
F.setVisible(true);
F.setSize(200, 100);
tx.setText("123");
F.getContentPane().add(tx,BorderLayout.WEST);
JButton bt = new JButton("Exit");
bt.setBackground(Color.blue);
F.getContentPane().add(bt,BorderLayout.EAST);
F.addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e){
System.exit(0);
}
});
tx.getDocument().addDocumentListener(new Swing_OnValueChanged ());
}
public static void main(String args[]){
JFrame1 F = new JFrame1();
}
}
class Swing_OnValueChanged implements DocumentListener
{
public void changedUpdate(DocumentEvent e){
System.out.printf("liyuan1" + e);
}
public void insertUpdate(DocumentEvent e){
System.out.printf("Text Inserted:"+tx.getText());
}
public void removeUpdate(DocumentEvent e){
System.out.printf("liyuan3");
}} 在方法中
public void insertUpdate(DocumentEvent e){
System.out.printf("Text Inserted:"+tx.getText());
}tx 无法访问啊
{
public JButton bt = null;
public JTextField tx = new JTextField();
//tx.getDocument().addDocumentListener(new Swing_OnValueChanged ());
public JFrame1()
{
JFrame F = new JFrame("TextListener");
F.setVisible(true);
F.setSize(200, 100);
tx.setText("123");
F.getContentPane().add(tx,BorderLayout.WEST);
JButton bt = new JButton("Exit");
bt.setBackground(Color.blue);
F.getContentPane().add(bt,BorderLayout.EAST);
F.addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e){
System.exit(0);
}
});
tx.getDocument().addDocumentListener(new Swing_OnValueChanged ());
}
public static void main(String args[]){
JFrame1 F = new JFrame1();
}
}
class Swing_OnValueChanged implements DocumentListener
{
public void changedUpdate(DocumentEvent e){
System.out.printf("liyuan1" + e);
}
public void insertUpdate(DocumentEvent e){
System.out.printf("Text Inserted:"+tx.getText());
}
public void removeUpdate(DocumentEvent e){
System.out.printf("liyuan3");
}} 在方法中
public void insertUpdate(DocumentEvent e){
System.out.printf("Text Inserted:"+tx.getText());
}tx 无法访问啊
解决方案 »
- 如何初始化一个未知元素个数的数组呀?
- JDK1.4支持匿名类么?
- java字符串排序
- 求正规文法转换成正规式的计算机实现算法
- 如何用键盘事件来控制JPANE的位置?我写了一段代码 实现不了~
- 真的没人懂吗?????????一个毕业设计题目都没人懂???
- 有人用过com.sun.net.HttpServer包吗?在哪里能够下载?
- 一时糊涂,问个基础问题:System.out.print()
- 如何探测一个网站的响应时长?
- 怎么我的jar包运行不了,说找不到main class(大虾救命啊)
- 一个简单的Socket编程问题,进来帮忙看看
- 请问为何在链接的时候提示Jpcap中的GetDeviceList()错误
作为public class JFrame1 extends JFrame 的内部类