(LIST1) ================================================================= import java.awt.*; import java.awt.event.*; import com.sun.java.swing.*; // JComponentを拡張したJPanelを拡張することによって // ライトウェイトパネルを作成します. public class DrinkDemo extends JPanel { static JFrame frame; static String gui1 = new String("インターフェイス1"); static String gui2 = new String("インターフェイス2"); public DrinkDemo() { super(true); // レイアウトマネージャを設定します. // 既存のレイアウトマネージャをそのまま使用可能です. setLayout(new BorderLayout()); // ラジオボタン配置用のパネルを作成します. JPanel btnPanel = new JPanel(); btnPanel.setAlignmentX(LEFT_ALIGNMENT); btnPanel.setBorderStyle(ETCHED); ButtonGroup group = new ButtonGroup(); //ラジオボタンをテキスト指定で作成します. JRadioButton gui1Button = new JRadioButton(gui1); // キーアクセラレートを設定します. gui1Button.setKeyAccelerator('b'); // ボタンのアクションコマンドを設定します. gui1Button.setActionCommand("GUI1"); // ボタンの選択状態を設定します. gui1Button.setSelected(true); // ツールチップを設定します. // ほとんどのSwingコンポーネントは,Componentを継承して // いるJComponentのサブクラスとして実装されており,ツール // チップ,キーボード生成アクション等といった機能を継承 // しています.そのためSwingコンポーネントにツールチップ // を作成するのは大変簡単です. // setToolTipTextメソッドで簡単に設定することができます. gui1Button.setToolTipText("インターフェイス1に設定します"); // パネルにボタンを追加し,グループとして処理するよう // 設定します. btnPanel.add(gui1Button); group.add(gui1Button); JRadioButton gui2Button = new JRadioButton(gui2); gui2Button.setKeyAccelerator('r'); gui2Button.setActionCommand("GUI2"); gui2Button.setToolTipText("インターフェイス2に設定します"); btnPanel.add(gui2Button); group.add(gui2Button); // ラジオボタンのアクションリスナを登録します. RadioListener myListener = new RadioListener(); gui1Button.addActionListener(myListener); gui2Button.addActionListener(myListener); // 飲料水ツリーノードを作成します. TreeNode drink = new TreeNode("飲料水"); TreeNode category; // お茶ツリーノードを作成します. category = new TreeNode("お茶"); // 飲料水に追加します. drink.add(category); // お茶の種類を追加します. category.add(new TreeNode("抹茶")); category.add(new TreeNode("麦茶")); category.add(new TreeNode("玄米茶")); category.add(new TreeNode("ウーロン茶")); // コーヒーツリーノードを作成します. category = new TreeNode("コーヒー"); // 飲料水に追加します. drink.add(category); // コーヒーの種類を追加します. category.add(new TreeNode("アメリカン")); category.add(new TreeNode("ブレンド")); category.add(new TreeNode("キリマンジェロ")); category.add(new TreeNode("モカ")); category.add(new TreeNode("ブルーマウンテン")); // 紅茶ツリーノードを作成します. category = new TreeNode("紅茶"); // 飲料水に追加します. drink.add(category); // 紅茶の種類を追加します. category.add(new TreeNode("Java")); category.add(new TreeNode("ダージリン")); category.add(new TreeNode("ディンブラ")); category.add(new TreeNode("クラシックブレンド")); category.add(new TreeNode("オレンジペコ")); JTree tree = new JTree(drink); //Create the scroll pane and add the tree to it. // スクロール領域を作成し,ツリーを追加します. JScrollPane scrollPane = new JScrollPane(); scrollPane.getViewport().add(tree); //Add the scroll pane to this panel. add("Center",scrollPane); add("South",btnPanel); } class RadioListener implements ActionListener { public void actionPerformed(ActionEvent e) { // アクションコマンドが GUI1 の場合 if (e.getActionCommand() == "GUI1"){ System.out.println("GUI1"); setGui1(); } else { System.out.println("GUI2"); setGui2(); } frame.invalidate(); frame.validate(); frame.repaint(); } } // UIFactoryでFactoryを設定します. public void setGui1() { try { UIManager.setUIFactory( "com.sun.java.swing.basic.BasicFactory", frame); } catch (ClassNotFoundException e){ System.err.println(e); } } // UIFactoryでFactoryを設定します. public void setGui2() { try { UIManager.setUIFactory( "com.sun.java.swing.rose.RoseFactory", frame); } catch (ClassNotFoundException e){ System.err.println(e); } } public static void main(String[] args) { WindowListener l = new WindowAdapter() { public void windowClosing(WindowEvent e) { System.exit(0);} }; frame = new JFrame("ツリー・デモ"); frame.addWindowListener(l); frame.add("Center", new DrinkDemo()); frame.setSize(300, 300); frame.show(); } } =================================================================