import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.net.*;class Y2 extends JFrame {

public Y2() {
addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
dispose();
System.exit(0);
}
});
} public static void main(String args[]) {
System.out.println("Starting Y2...");
Y2 mainFrame = new Y2();
mainFrame.setSize(400, 400);
mainFrame.setTitle("撒旦");
JTextField t = new JTextField("请输入您的主机名");
JButton b = new JButton("确定");
String tx = t.getText();
ipa a = new ipa(tx);   //为什么这个传不下去!!!!!
mainFrame.getContentPane().setLayout(new FlowLayout());
mainFrame.getContentPane().add(t);
mainFrame.getContentPane().add(b);
b.addActionListener(a);
mainFrame.setVisible(true);
}
}
class ipa implements ActionListener{
private String text;
public void ipa(String w)
{
text=w;
}

public void actionPerformed(ActionEvent e)
{
try{
InetAddress a = InetAddress.getByName(text);
                                         结果怎么在能传个上面的文本框!!!!!
}
catch (UnknownHostException f){
System.err.println("您输入的主机名正确吗");
}
}
}

解决方案 »

  1.   

    upupupupupupupupupupupupupupupupupupupup不知道这样能顶到上面去不!反正我发的帖子没上过顶!
      

  2.   

    JTextField t ;设置为类的成员,就可以访问了。
    InetAddress a = InetAddress.getByName(t.getText());
      

  3.   

    public void ipa(String w)
    {
    text=w;
    }
    to:
    public ipa(String w)
    {
    text=w;
    }
      

  4.   

    public void actionPerformed(ActionEvent e){ System.out.println(text);
    try{
    InetAddress a = InetAddress.getByName(text);
                                             //结果怎么在能传个上面的文本框!!!!!
    }catch (UnknownHostException f){
    System.err.println("您输入的主机名正确吗");
    }
    }
    只能接收第一次传过来的,第二次的根本没执行,你试试这个,我测过了:
    /*
     * Created on 2005-4-28
     *
     * TODO To change the template for this generated file go to
     * Window - Preferences - Java - Code Style - Code Templates
     */
    package awtPackage;/**
     * @author Administrator
     *
     * TODO To change the template for this generated type comment go to
     * Window - Preferences - Java - Code Style - Code Templates
     */
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    import java.net.*;public class Y2 extends JFrame { public static JTextField t = new JTextField("请输入您的主机名");
    public Y2() {
    addWindowListener(new WindowAdapter() {
    public void windowClosing(WindowEvent e) {
    dispose();
    System.exit(0);
    }
    });
    } public static void main(String args[]) {
    System.out.println("Starting Y2...");
    Y2 mainFrame = new Y2();
    mainFrame.setSize(400, 400);
    mainFrame.setTitle("撒旦");
    JButton b = new JButton("确定");
    String tx = t.getText();
    ipa a = new ipa(tx);   //为什么这个传不下去!!!!!
    mainFrame.getContentPane().setLayout(new FlowLayout());
    mainFrame.getContentPane().add(t);
    mainFrame.getContentPane().add(b);
    b.addActionListener(a);
    mainFrame.setVisible(true);
    }
    }
    class ipa implements ActionListener{
    private String text;
    public ipa(String w){
    System.out.println(1);
    text=w;
    }

    public void actionPerformed(ActionEvent e){
    text = Y2.t.getText();
    System.out.println(text);
    try{
    InetAddress a = InetAddress.getByName(text);
                                             //结果怎么在能传个上面的文本框!!!!!
    }catch (UnknownHostException f){
    System.err.println("您输入的主机名正确吗");
    }
    }
    }