package getaddress2;
import java.awt.*;
import java.awt.event.*;
import java.net.*;
import javax.swing.*;
public class GetAddress2 extends JFrame {
private JLabel lb1,lb2;
private JTextField tf1,tf2;
public static void main(String[] args) {
GetAddress2 sm= new GetAddress2();
}
public GetAddress2()
{
super("範例");
try{
InetAddress ia = InetAddress.getLocalHost();
lb1 = new JLabel("主機名稱");
lb2 = new JLabel("IP位址");
tf1 = new JTextField(ia.getHostName());
tf2 = new JTextField(ia.getHostAddress());
setLayout(new GridLayout(2,2));
add(lb1);
add(tf1);
add(lb2);
add(tf2);
addWindowListener(new SampleWindowListener());
pack();
setVisible(true);
}
catch(Exception e){
e.printStackTrace();
}
}
class SampleWindowListener extends WindowAdapter
{
public void windowClosing(WindowEvent e)
{
System.exit(0);
}
}
}