public class Test{
public static void main(String[] args){
TestFrame tFrame = new TestFrame("测试窗口");
}
}class TestFrame extends JFrame{
JTextField rTxb = new JTextField(20);
public TestFrame(String s){
super(s);
AddSomething(this);
setBounds(110,110,600,600);
setVisible(true);

try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (Exception e) {
// TODO: handle exception
}
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}

void AddSomething(JFrame f){
JPanel nPn = new JPanel();
nPn.setSize(300, 300);
nPn.setBackground(Color.blue);
Button sendBt = new Button("发送消息");
Button testBt = new Button("测试用按钮");

SHandler h = new SHandler();
sendBt.addActionListener(h);
testBt.addActionListener(h);

nPn.add(sendBt);
nPn.add(testBt);
nPn.add(rTxb);
f.add(nPn);
}}
class SHandler  implements ActionListener{
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
switch(e.getActionCommand()){
case "发送消息":
System.out.println("调用监听器的是:"+e.getActionCommand());
TestFrame tFrame = null;
tFrame.rTxb.setText("修改你妹!");
break;
case "测试用按钮":
System.out.println("调用监听器的是:"+e.getActionCommand());
break;
}
}
}求问点击按钮“发送消息”时修改rTxb的内容,或者获取其内容也可以...测试用...
点击按钮运行到47行后就崩溃了...
有大神求解吗?
完整代码如上...我在想是不是我的思路有问题...该如何改?

解决方案 »

  1.   

    TestFrame tFrame = null;
    tFrame.rTxb.setText("修改你妹!");
    这两行有问题啦,肯定是空指针异常嘛;
    首先你得取到TestFrame的JTextField
      

  2.   

    可以这样,
    class SHandler  implements ActionListener{
        private TestFrame frame;
        
        public SHandler(TestFrame frame) {
            this.frame = frame;
        }
        
        @Override
        public void actionPerformed(ActionEvent e) {
            switch(e.getActionCommand()){
            case "发送消息":
                System.out.println("调用监听器的是:"+e.getActionCommand());
                //TestFrame tFrame = null;
                //tFrame.rTxb.setText("修改你妹!");
                // frame...TODO
                break;
            case "测试用按钮":
                System.out.println("调用监听器的是:"+e.getActionCommand());
                break;
            }    
        }    
    }
      

  3.   

    可以这样,
    class SHandler  implements ActionListener{
        private TestFrame frame;
        
        public SHandler(TestFrame frame) {
            this.frame = frame;
        }
        
        @Override
        public void actionPerformed(ActionEvent e) {
            switch(e.getActionCommand()){
            case "发送消息":
                System.out.println("调用监听器的是:"+e.getActionCommand());
                //TestFrame tFrame = null;
                //tFrame.rTxb.setText("修改你妹!");
                // frame...TODO
                break;
            case "测试用按钮":
                System.out.println("调用监听器的是:"+e.getActionCommand());
                break;
            }    
        }    
    }就算是这样也是无法修改rTxb的内容吖....
    其实这样相当于new 了一个TestFrame的对象...
      

  4.   

    难道非得写全部代码才明白么,
    在new SHandler的地方传Jframe参数,然后在actionPerformed方法中由Jframe取得JTextField,你可以在Jframe中定义一个get方法
      

  5.   

    class TestFrame extends JFrame {
    JTextField rTxb = new JTextField(20);

    public JTextField getrTxb() {
    return rTxb;
    } public TestFrame(String s) {
    super(s);
    AddSomething(this);
    setBounds(110, 110, 600, 600);
    setVisible(true); try {
    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
    } catch (Exception e) {
    // TODO: handle exception
    }
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    } void AddSomething(JFrame f) {
    JPanel nPn = new JPanel();
    nPn.setSize(300, 300);
    nPn.setBackground(Color.blue);
    Button sendBt = new Button("发送消息");
    Button testBt = new Button("测试用按钮"); SHandler h = new SHandler(this);
    sendBt.addActionListener(h);
    testBt.addActionListener(h); nPn.add(sendBt);
    nPn.add(testBt);
    nPn.add(rTxb);
    f.add(nPn);
    }}class SHandler implements ActionListener {
    private TestFrame frame;

    public SHandler(TestFrame frame) {
    this.frame = frame;
    }

    @Override
    public void actionPerformed(ActionEvent e) {
    switch (e.getActionCommand()) {
    case "发送消息":
    System.out.println("调用监听器的是:" + e.getActionCommand());
    frame.getrTxb().setText("修改你妹!");
    break;
    case "测试用按钮":
    System.out.println("调用监听器的是:" + e.getActionCommand());
    break;
    }
    }
    }