メニューで選択された内容に従い、パネルの表示を切り替える

戻る
このようにハードコーディングで毎回GUIを作るのは、はっきり言ってタコなので、 サーバ側からXMLを供給し、それに従いクライアントが動的にメニューおよびメインペインにJComponentを表示するパターンを作成してみましょう(⇒次回以降の予定)。 これによりコピペによる作業は不要となるわけじゃ。。。




:::::::::::::: BottomPanel.java :::::::::::::: import javax.swing.*; import java.awt.*; import java.awt.event.*; /** * $Id: passanger.html,v 1.1 2009/06/22 16:12:19 kishi Exp kishi $ */ public class BottomPanel extends JPanel { private JLabel messageLabel; public BottomPanel() { super(); messageLabel = new JLabel(); update( "ここにメッセージが表示されます。" ); this.add( messageLabel ); } public void update( String text ) { messageLabel.setText( text ); } } :::::::::::::: CenterPanel.java :::::::::::::: import javax.swing.*; import java.awt.*; import java.awt.event.*; /** * $Id: passanger.html,v 1.1 2009/06/22 16:12:19 kishi Exp kishi $ */ public class CenterPanel extends JPanel { private Color defaultColor; public CenterPanel() { super(); defaultColor = this.getBackground(); } public void resetBackground() { this.setBackground( defaultColor ); } } :::::::::::::: MyMenuBar.java :::::::::::::: import java.util.*; import javax.swing.*; import java.awt.*; import java.awt.event.*; /** * $Id: passanger.html,v 1.1 2009/06/22 16:12:19 kishi Exp kishi $ */ public class MyMenuBar extends JMenuBar { /** 実際はPassangerがインスタンス化されたもの */ private Object parentObject; public MyMenuBar() { super(); } public void setParentObject( Object parentObject ) { this.parentObject = parentObject; } public void addJMenu() { JMenu configuration = new JMenu( "設定" ); JMenu menu2 = new JMenu( "テストラン" ); Action action = new MyMenuBarAction(); ( ( MyMenuBarAction ) action ).setParentObject( parentObject ); JMenu itemMenu = new JMenu( "アイテム" ); JMenu deviceMenu = new JMenu( "デバイス" ); JMenu processMenu = new JMenu( "プロセス" ); configuration.add( itemMenu ); configuration.add( deviceMenu ); configuration.add( processMenu ); // 各メニューに対してのアクションを指定 addMenuItem( configuration, action, "終了", "EXIT" ); addMenuItem( itemMenu, action, "アイテムロード", "LOAD_ITEM" ); addMenuItem( itemMenu, action, "アイテム保存", "SAVE_ITEM" ); addMenuItem( deviceMenu, action, "デバイス設定", "SET_DEVICE" ); addMenuItem( deviceMenu, action, "PING", "PING" ); addMenuItem( processMenu, action, "プロセス作成", "CREATE_PROCESS" ); addMenuItem( processMenu, action, "プロセス編集", "EDIT_PROCESS" ); addMenuItem( processMenu, action, "プロセス保存", "SAVE_PROCESS" ); addMenuItem( menu2, action, "機能1", "A" ); addMenuItem( menu2, action, "機能2", "B" ); addMenuItem( menu2, action, "機能3", "C" ); addMenuItem( menu2, action, "機能4", "D" ); addMenuItem( menu2, action, "機能5", "E" ); this.add( configuration ); this.add( menu2 ); } private void addMenuItem( JMenu menu, Action action, String description, String commandName ) { JMenuItem item = new JMenuItem( description ); menu.add( item ); item.setActionCommand( commandName ); item.addActionListener( action ); } } :::::::::::::: MyMenuBarAction.java :::::::::::::: import javax.swing.*; import java.awt.*; import java.awt.event.*; /** * $Id: passanger.html,v 1.1 2009/06/22 16:12:19 kishi Exp kishi $ */ public class MyMenuBarAction extends AbstractAction { private Object parentObject; public MyMenuBarAction() { super(); } public void setParentObject( Object parentObject ) { this.parentObject = parentObject; System.out.println( parentObject ); } public void actionPerformed( ActionEvent event ) { String command = event.getActionCommand(); PassengerMain passenger = ( PassengerMain ) parentObject; CenterPanel centerPanel = ( CenterPanel ) passenger.getCenterPanel(); BottomPanel bottomPanel = ( BottomPanel ) passenger.getBottomPanel(); // 実行されたコマンドをダンプします System.out.println( command ); if ( "EXIT".equals( command ) ) { System.exit( 0 ); } else { bottomPanel.update( command ); } if ( "LOAD_ITEM".equals( command ) ) { centerPanel.setBackground( Color.white ); } else { centerPanel.resetBackground(); } centerPanel.removeAll(); if ( "A".equals( command ) ) { centerPanel.add( new JButton( "機能1で使用するボタン" ) ); } centerPanel.repaint(); } } :::::::::::::: PassengerMain.java :::::::::::::: import javax.swing.*; import java.awt.*; import java.awt.event.*; /** * $Id: passanger.html,v 1.1 2009/06/22 16:12:19 kishi Exp kishi $ */ public class PassengerMain extends JFrame { private static final String REAL_AUTHOR = "岸 康弘"; private CenterPanel centerPanel; private BottomPanel bottomPanel; public PassengerMain() { // 上部に表示するメニュー MyMenuBar menuBar = new MyMenuBar(); // 親画面の参照を引き渡しておく menuBar.setParentObject( this ); // JMenuの追加 menuBar.addJMenu(); // 中心に配置するパネル centerPanel = new CenterPanel(); centerPanel.setPreferredSize( new Dimension( 640, 480 ) ); // 下部に配置するパネル bottomPanel = new BottomPanel(); bottomPanel.setPreferredSize( new Dimension( 640, 80 ) ); //----------------------------------------------------- // 全ての部品をContainerに配置する //----------------------------------------------------- Container container = this.getContentPane(); container.add( menuBar, BorderLayout.NORTH ); container.add( centerPanel, BorderLayout.CENTER ); container.add( bottomPanel, BorderLayout.SOUTH ); } public JComponent getCenterPanel() { return centerPanel; } public JComponent getBottomPanel() { return bottomPanel; } public static void main( String[] args ) { PassengerMain designer = new PassengerMain(); /* 終了処理 */ designer.addWindowListener( new WindowAdapter() { public void windowClosing( WindowEvent e ) { System.exit( 0 ); } } ); designer.setBounds( 0, 0, 600, 800 ); // タイトルの表示 designer.setTitle( "Passenger Emulation System demo by " + REAL_AUTHOR ); designer.setVisible( true ); designer.pack(); } }
戻る inserted by FC2 system