package delnopdir;
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import java.io.File;public class delNopDir extends JFrame{
    private JLabel JLTargetDir=new JLabel("目标文件夹:");
    private JTextField JTFTargetDir=new JTextField("");
    private JButton JBSearch=new JButton("...");
    public delNopDir() {
        Container con=getContentPane();
        con.setLayout(null);
        setSize(400,200);
        setTitle("删除空目录:");
        JLTargetDir.setBounds(20,20,80,30);
        con.add(JLTargetDir);
        JTFTargetDir.setBounds(100,20,220,30);
        con.add(JTFTargetDir);
        JBSearch.setBounds(330,20,30,30);
        con.add(JBSearch);
        JBSearch.addActionListener(new search());
        setVisible(true);
    }
    public JTextField getJTFTargetDir(){
        return JTFTargetDir;
    }
    public static void main(String[] args)
    {
        delNopDir dnp=new delNopDir();
        dnp.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }
}class search implements ActionListener
{
    JFileChooser fc;
    public void actionPerformed(ActionEvent e)
    {
        fc=new JFileChooser();
        if(JFileChooser.APPROVE_OPTION==fc.showOpenDialog(null))
        {
            File f=fc.getSelectedFile();
            (new delNopDir()).getJTFTargetDir().setText(f.getAbsolutePath());//此处我创建的对象和在主程序创建的对象应该是两个对象,可我用这个对象却能设置那个对象的变量的值,求大侠解释下,万分感激
        }
    }
}

解决方案 »

  1.   

    楼上说的对   你是不是要这样的 ?import java.awt.*;
    import javax.swing.*;
    import java.awt.event.*;
    import java.io.File;public class delNopDir extends JFrame{
        private JLabel JLTargetDir=new JLabel("目标文件夹:");
        private JTextField JTFTargetDir=new JTextField("");
        private JButton JBSearch=new JButton("...");
        public delNopDir() {
            Container con=getContentPane();
            con.setLayout(null);
            setSize(400,200);
            setTitle("删除空目录:");
            JLTargetDir.setBounds(20,20,80,30);
            con.add(JLTargetDir);
            JTFTargetDir.setBounds(100,20,220,30);
            con.add(JTFTargetDir);
            JBSearch.setBounds(330,20,30,30);
            con.add(JBSearch);
            JBSearch.addActionListener(new search());
            setVisible(true);
        }
        public JTextField getJTFTargetDir(){
            return JTFTargetDir;
        }
        public static void main(String[] args)
        {
            delNopDir dnp=new delNopDir();
            dnp.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        }
        
        class search implements ActionListener
        {
            JFileChooser fc;
            public void actionPerformed(ActionEvent e)
            {
                fc=new JFileChooser();
                if(JFileChooser.APPROVE_OPTION==fc.showOpenDialog(null))
                {
                    File f=fc.getSelectedFile();
                    JTFTargetDir.setText(f.getAbsolutePath());
                }
            }
        }
    }
      

  2.   

    如果在MAIN中创建两个delNopDir对象(del1和del2),那(new delNopDir()).getJTFTargetDir()得到的会是哪个对象的变量呢?