本帖最后由 tianranhe 于 2013-03-07 16:41:39 编辑

解决方案 »

  1.   

    this.taskNameNew = taskNameNew; 你这是干什么,根本就没有得到文本框里的值啊;在修改按钮事件里:
    frame.getbutton().setText(jtext.getText()),然后刷新下fram1
      

  2.   

    有啊,在Frame2第5行:tf.getText()
    button2.addActionListener(new Listener2(button1, tf.getText(),frame1));
      

  3.   


    有啊,在 Frame2第5行:tf.getText(),调用的时候赋给参数了
    button2.addActionListener(new Listener2(button1, tf.getText(),frame1));
      

  4.   

    我做了如下修改:import java.awt.FlowLayout;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;import javax.swing.JButton;
    import javax.swing.JFrame;
    import javax.swing.JTextField;public class Frame1 {
    static JButton button1;
    public static void main(String []args){
    button1 = new JButton("button1");
    JFrame frame1 = new JFrame("frame1");
    button1.addActionListener(new Listener1(button1, frame1)); 
    frame1.add(button1);
    frame1.setLayout(new FlowLayout());
    frame1.setBounds(400, 100, 400, 300);
    frame1.setVisible(true);
    }
    static JButton getbun(){
    return button1;
     
    }
    }class Frame2{
    JButton button2;
    static JTextField tf;
    public Frame2(JButton button1, JFrame frame1){
     tf = new JTextField(20);
    JButton button2 = new JButton("修改");
    button2.addActionListener(new Listener2(button1, tf.getText(),frame1));

    JFrame frame2 = new JFrame("frame2");
    frame2.setLayout(new FlowLayout());
    frame2.setBounds(450, 150, 300, 200);
    frame2.add(tf);
    frame2.add(button2);
            frame2.setVisible(true); }
    static JTextField gettext(){
    return tf;
    }

    }class Listener1 implements ActionListener{
    JButton button;
    JFrame frame;
    public Listener1(JButton button, JFrame frame){
    this.frame = frame;
    this.button = button;
    }
    public void actionPerformed(ActionEvent e){
            Frame2 frame2 = new Frame2(button,frame);
    }
    }
    class Listener2 implements ActionListener{
    String taskNameOld, taskNameNew;
    JButton button;
    JFrame frame;

    public Listener2(JButton button, String stringNew, JFrame frame){
    this.frame = frame; this.taskNameOld = button.getText();
    this.taskNameNew = taskNameNew;
    }
    public void actionPerformed(ActionEvent e){
    if(taskNameNew != taskNameOld)
    {
    Frame1.getbun().setText(Frame2.gettext().getText().toString());//运行时Eclipse报错
    frame.repaint();
    }
    }
    }
      

  5.   

    你之前的button.setText(taskNameNew);这行中的button是什么?没有被初始化的按钮
      

  6.   

    button空指针了。因为在Listener2的构造方法中少写了一句
    this.button = button;
      

  7.   


    好了,原来要return一下。
    太感谢你了
      

  8.   


    我把button1,从Frame1传到Frame2, 等于已经初始化了啊我做一个小东西,这只是一个调试用的demo
      

  9.   

    很简单
    你少了this.button=button;所以button是空,所以是空指针异常
    然后
    你获取tf内容应该是监听方法里面获取,如在actionPerformed里面
    像你button2.addActionListener(new Listener2(button1, tf.getText(),frame1));这样的话
    在初始化的时候就已经获取了,这时tf里面是没有内容的,应该写在监听方法里面,这样才是点击按钮时获取
    改了如下
    class Listener2 implements ActionListener{
        String taskNameOld, taskNameNew;
        JButton button;
        JFrame frame;
        JTextField tf;
        public Listener2(JButton button,JTextField tf, JFrame frame){
            this.frame = frame;
            this.button = button;
            this.taskNameOld = button.getText();
            this.tf = tf;
        }
        public void actionPerformed(ActionEvent e){
            if(taskNameNew != taskNameOld)
        { taskNameNew = tf.getText();
                button.setText(taskNameNew);//运行时Eclipse报错
                frame.repaint();
            }
        }
    }
    然后上面的有一句button2.addActionListener(new Listener2(button1, tf,frame1));改成这样
    另外还想跟你说,判断字符是不是一样是用equals方法,不是用==或者!=,这样是判断地址,而不是内容