import java.awt.BorderLayout; import java.awt.Container; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.net.InetAddress; import java.net.UnknownHostException; import javax.swing.JFrame; import javax.swing.JScrollPane; import javax.swing.JTextArea; import javax.swing.JTextField; /* IPアドレス⇔ホスト名の相互変換を行うGUIアプリケーション */ public class MyJFrame1 extends JFrame implements ActionListener{ private JTextField tf; // IPアドレスまたはホスト名の入力欄 private JTextArea ta; // 変換結果の表示 public static void main(String[] args) { // MyJFrame1オブジェクトを生成する new MyJFrame1(); } public void actionPerformed(ActionEvent e){ String text = tf.getText(); try { // 入力されたIPアドレスまたはホスト名から // InetAddressオブジェクトを得て,そこから // ホスト名とIPアドレスとを取得する InetAddress ia = InetAddress.getByName(text); ta.append(ia.getHostName()+"\n"); ta.append(ia.getHostAddress()+"\n"); } catch (UnknownHostException e1) { // InetAddressオブジェクトの生成に失敗したら // 原因を表示する //e1.printStackTrace(); ta.append(e1.toString()); } } MyJFrame1(){ // GUI部品の生成と配置 Container cnt = getContentPane(); tf = new JTextField(); cnt.add(tf,BorderLayout.NORTH); tf.addActionListener(this); ta = new JTextArea("IP address かホスト名を入れてください\n"); JScrollPane sp = new JScrollPane(ta); cnt.add(sp,BorderLayout.CENTER); // 窓の表示(タイトルバーにlocalhostの情報を表示する) setTitle(getLocalHostTitle()); setSize(300,200); setDefaultCloseOperation(EXIT_ON_CLOSE); setVisible(true); } private String getLocalHostTitle(){ // localhostのホスト名とIPアドレスをつなげた文字列を返す String titleStr = ""; try { // localhostに対するInetAddressオブジェクトを // 得て,そこからホスト名とIPアドレスを取得する InetAddress lh = InetAddress.getLocalHost(); titleStr += lh.getHostName(); titleStr += "("+lh.getHostAddress()+")"; } catch (UnknownHostException e) { // InetAddressオブジェクトの生成に失敗したら // 原因を表示する e.printStackTrace(); } return titleStr; } } ```### 実現したいこと ・テキストフィールドにIPアドレスまたはホスト名を入力し、その得られたオブジェクト(InetAddress)のIP アドレス /ホスト名 / スコープをテキストエリアに表示させ窓を閉じたら実行を終了するプログラムを作りたいのですがわからなくなったので教えていただきたいです。 ###とりあえずできたこと(変えてもらっても大丈夫です) import java.awt.BorderLayout; import java.awt.Container; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.net.InetAddress; import java.net.UnknownHostException; import javax.swing.JFrame; import javax.swing.JScrollPane; import javax.swing.JTextArea; import javax.swing.JTextField; /* IPアドレス⇔ホスト名の相互変換を行うGUIアプリケーション */ public class MyJFrame1 extends JFrame implements ActionListener{ private JTextField tf; // IPアドレスまたはホスト名の入力欄 private JTextArea ta; // 変換結果の表示 public static void main(String[] args) { // MyJFrame1オブジェクトを生成する new MyJFrame1(); } public void actionPerformed(ActionEvent e){ String text = tf.getText(); try { // 入力されたIPアドレスまたはホスト名からInetAddressオブジェクトを得て, そこからホスト名とIPアドレスとを取得する InetAddress ia = InetAddress.getByName(text); ta.append(ia.getHostName()+"\n"); ta.append(ia.getHostAddress()+"\n"); } catch (UnknownHostException e1) { // InetAddressオブジェクトの生成に失敗したら原因を表示する //e1.printStackTrace(); ta.append(e1.toString()); } } MyJFrame1(){ // GUI部品の生成と配置 Container cnt = getContentPane(); tf = new JTextField(); cnt.add(tf,BorderLayout.NORTH); tf.addActionListener(this); ta = new JTextArea("IP address かホスト名を入れてください\n"); JScrollPane sp = new JScrollPane(ta); cnt.add(sp,BorderLayout.CENTER); // 窓の表示(タイトルバーにlocalhostの情報を表示する) setTitle(getLocalHostTitle()); setSize(300,200); setDefaultCloseOperation(EXIT_ON_CLOSE); setVisible(true); } private String getLocalHostTitle(){ // localhostのホスト名とIPアドレスをつなげた文字列を返す String titleStr = ""; try { // localhostに対するInetAddressオブジェクトを得て,そこからホスト名とIPアドレスを取得する InetAddress lh = InetAddress.getLocalHost(); titleStr += lh.getHostName(); titleStr += "("+lh.getHostAddress()+")"; } catch (UnknownHostException e) { // InetAddressオブジェクトの生成に失敗したら原因を表示する e.printStackTrace(); } return titleStr; } }
あなたの回答
tips
プレビュー