import java.awt.*; import javax.swing.*; import javax.swing.event.*; import java.awt.event.*; import java.util.*; public class Test { public static void main (String[] args) { Win win = new Win("Test Window"); } } /*******************/ /* クラスWinの定義 */ /*******************/ class Win extends JFrame implements ListSelectionListener { JTextArea ta; JList <String> lt; JLabel lb; String str1[] = {"桜", "紫陽花", "朝顔"}; String str2[] = {"桜が選択されました", "紫陽花が選択されました", "朝顔が選択されました"}; String str3[] = {"hana1.gif", "hana2.gif", "hana3.gif"}; /******************/ /* コンストラクタ */ /******************/ Win (String name) { // Frameクラスのコンストラクタ(Windowのタイトルを引き渡す) super(name); // コンポーネントの定義 Font f = new Font("MS 明朝", Font.PLAIN, 20); // リスト lt = new JList <String> (str1); lt.setFont(f); lt.setSelectedIndex(0); lt.addListSelectionListener(this); // ラベル lb = new JLabel(); ImageIcon im = new ImageIcon(str3[0]); lb.setIcon(im); // テキストエリア ta = new JTextArea("桜が選択されました"); ta.setFont(f); // スプリットペインを設定 // 左右に分割 // 以下の1行は,次のように3行で書いても同じ // JSplitPane sp1 = new JSplitPane(); // sp1.setLeftComponent(lt); // sp1.setRightComponent(lb); JSplitPane sp1 = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, lt, lb); sp1.setOneTouchExpandable(true); sp1.setContinuousLayout(false); // 上下に分割 JSplitPane sp2 = new JSplitPane(JSplitPane.VERTICAL_SPLIT, sp1, ta); getContentPane().add(sp2); // Windowの大きさ setSize(340, 350); // ウィンドウを表示 setVisible(true); // イベントアダプタ addWindowListener(new WinEnd()); } /******************************/ /* 上,左,下,右の余白の設定 */ /******************************/ public Insets getInsets() { return new Insets(35, 10, 10, 10); } /********************************/ /* リストが選択されたときの処理 */ /********************************/ public void valueChanged(ListSelectionEvent e) { if (e.getSource() == lt) { int k = lt.getSelectedIndex(); ta.setText(str2[k]); ImageIcon im = new ImageIcon(str3[k]); lb.setIcon(im); } } /************/ /* 終了処理 */ /************/ class WinEnd extends WindowAdapter { public void windowClosing(WindowEvent e) { System.exit(0); } } }