本人真的想不到有什么方法可以在GetHostName类创造的窗体关闭时同时令HomeWork类的ShowInformation显示出我输入的IP(T_IPAddress),乱打乱撞,无意中发现了这样的方法(用过VC++的原因吧),这方法确实可行,效果也达到了,但变得不实际和专业了,求新方法来解决。
import java.net.*;
import java.awt.*;
import java.awt.event.*;public class HomeWork extends Frame implements ActionListener
{
Label L_Handle,L_Data;
Button B_HostIP,B_GetHostName,B_GetIP,B_SaveData,B_ReadData,B_DeleteData;
TextArea ShowInformation;
Frame MF; public static void main(String[] args) 
{
String aaaa;
HomeWork main = new HomeWork();
GetHostName GHName = new GetHostName();
main.MainFrame();
while(1==1)
{
main.ShowInformation.setText(GHName.ReturnValue());
}
}
HomeWork()
{
super("DNS集成服务系统");
}
public void MainFrame()
{
L_Handle = new Label("操作功能");
L_Data = new Label("数据功能");
B_HostIP = new Button("获取本地主机");
B_GetHostName = new Button("通过IP地址扫描主机名称");
B_GetIP = new Button("通过主机名称查询IP地址");
B_SaveData = new Button("保存记录");
B_ReadData = new Button("查看记录");
B_DeleteData = new Button("清除记录");
ShowInformation = new TextArea(15,55);
setLayout(new FlowLayout());
add(L_Handle);
add(B_HostIP);
add(B_GetHostName);
add(B_GetIP);
add(L_Data);
add(B_SaveData);
add(B_ReadData);
add(B_DeleteData);
add(ShowInformation);
B_HostIP.addActionListener(this);
B_GetHostName.addActionListener(this);
B_GetIP.addActionListener(this);
B_SaveData.addActionListener(this);
B_ReadData.addActionListener(this);
B_DeleteData.addActionListener(this);
//GHName.addWindowListener(this);
setSize(500,400);
setVisible(true);
}
public void actionPerformed(ActionEvent e)
{
if(e.getSource()==B_HostIP)
{
ShowInformation.setText("OK了。");
}
else if(e.getSource()==B_GetHostName)
{ }
else if(e.getSource()==B_GetIP)
{ }
else
{
dispose();
System.exit(0);
}
}
/*public void windowClosing(WindowEvent e)
{
GHName.dispose();
}*/}
class GetHostName extends Frame implements ActionListener
{
Label L_IPAddress;
TextField T_IPAddress;
Button OK,Cancel;
String IPString=""; String ReturnValue()
{
return IPString;
} GetHostName()
{
super("通过IP地址扫描主机名称");
L_IPAddress = new Label("主机名称");
T_IPAddress = new TextField(25);
OK = new Button("确定");
Cancel = new Button("取消");
setLayout(new FlowLayout());
add(L_IPAddress);
add(T_IPAddress);
add(OK);
add(Cancel);
OK.addActionListener(this);
Cancel.addActionListener(this);
setSize(300,150);
setVisible(true);
}
public void actionPerformed(ActionEvent e)
{
if(e.getSource()==OK)
{
IPString =  T_IPAddress.getText();
this.setVisible(false);
}
}
}