package addresstest;
import java.awt.BorderLayout;
import java.awt.Label;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.net.InetAddress;
import java.net.UnknownHostException;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
public class AddressFrame extends JFrame {
private JLabel label;
private JLabel ipLabel;
private JTextField text;
private JButton ipButton;
private JButton eButton;
public AddressFrame(){
JPanel northPanel=new JPanel();
label=new JLabel("获取本地主机的IP地址");
northPanel.add(label);
add(northPanel,BorderLayout.NORTH);
ipLabel=new JLabel("ip地址:");
text=new JTextField(" ",20);
JPanel centerPanel=new JPanel();
centerPanel.add(ipLabel);
centerPanel.add(text);
add(centerPanel,BorderLayout.CENTER);
ipButton=new JButton("IP地址");
eButton=new JButton("退出");
JPanel southPanel=new JPanel();
southPanel.add(ipButton);
southPanel.add(eButton);
add(southPanel,BorderLayout.SOUTH);
ipButton.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
try{
InetAddress inetAddr=InetAddress.getLocalHost();
//创建本地主机的InetAddress 对象
String ip=inetAddr.getHostAddress();
//获取本地主机ip地址
text.setText(ip);
}catch(UnknownHostException el){
el.printStackTrace();
}
}
});
eButton.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
System.exit(0);
}
});
pack();
}
}