JButtonでメニュー画面を作る(その2)
戻る
::::::::::::::
ButtonMenuTest2.java
::::::::::::::
import java.util.*;
import java.util.regex.*;
import java.net.*;
import javax.swing.*;
import java.awt.event.*;
import java.awt.*;
/**
* $Id: menuContext.html,v 1.1 2009/06/22 16:12:17 kishi Exp kishi $
*/
public class ButtonMenuTest2 extends JFrame implements ActionListener {
// 本当は全てJPanelを継承した別クラスにして分離します
// ここは便宜上1個のクラスにまとめています
private JPanel topPanel;
private JPanel centerPanel;
private JPanel bottomPanel;
private JLabel messageLabel;
private java.util.Map menuContextMap;
private java.util.Map menuButtonMap;
/**
* 本当はメニューのコンテキストを記述したXMLを読み込む
*/
private void setContext() {
menuContextMap = new java.util.LinkedHashMap();
MenuContext context1 = new MenuContext();
context1.setDescription( "アイテム登録" );
menuContextMap.put( "CREATE_ITEM", context1 );
MenuContext context2 = new MenuContext();
context2.setDescription( "刻印データ出力" );
menuContextMap.put( "SYMBOL_DATA", context2 );
MenuContext context3 = new MenuContext();
context3.setDescription( "検査" );
menuContextMap.put( "INSPECTION", context3 );
}
/**
* コンストラクタ
*/
public ButtonMenuTest2() {
// 上部に配置するパネル
topPanel = new JPanel();
topPanel.setPreferredSize( new Dimension( 640, 40 ) );
setContext();
Iterator iterator = menuContextMap.keySet().iterator();
menuButtonMap = new java.util.LinkedHashMap();
while ( iterator.hasNext() ) {
String command = ( String ) iterator.next();
MenuContext menuContext = ( MenuContext ) menuContextMap.get( command );
String description = menuContext.getDescription();
JButton button = new JButton( description );
topPanel.add( button );
button.setActionCommand( command );
button.addActionListener( this );
menuButtonMap.put( command, button );
}
// 中心に配置するパネル
centerPanel = new JPanel();
centerPanel.setPreferredSize( new Dimension( 640, 480 ) );
centerPanel.setBackground( Color.cyan );
// 下部に配置するパネル
bottomPanel = new JPanel();
bottomPanel.setPreferredSize( new Dimension( 640, 80 ) );
messageLabel = new JLabel();
bottomPanel.add( messageLabel );
//-----------------------------------------------------
// 全ての部品をContainerに配置する
//-----------------------------------------------------
Container container = this.getContentPane();
container.add( topPanel, BorderLayout.NORTH );
container.add( centerPanel, BorderLayout.CENTER );
container.add( bottomPanel, BorderLayout.SOUTH );
}
public void actionPerformed( ActionEvent event ) {
String command = event.getActionCommand();
System.out.println( "COMMAND: " + command );
messageLabel.setText( "[" + command + "]が選択されました" );
// 選択されたメニューに応じてレンダリングする
centerPanel.removeAll();
if ( "CREATE_ITEM".equals( command ) ) {
JButton button = new JButton( "アイテムをシステムに新規登録します" );
button.setBackground( Color.yellow );
centerPanel.add( button );
} else if ( "SYMBOL_DATA".equals( command ) ) {
centerPanel.add( new JButton( "ヒロシです。刻印するとです。" ) );
centerPanel.add( new JLabel( "データは、CSV形式になるとです。" ) );
} else if ( "INSPECTION".equals( command ) ) {
JTextField textField = new JTextField( 20 );
centerPanel.add( textField );
JButton button = new JButton( "▲検査をするとです▲" );
button.setBackground( Color.orange );
centerPanel.add( button );
}
//===========================================-
// 選択したボタンを使用不可にする
//===========================================-
resetAllButtons();
getJButton( command ).setEnabled( false );
//===========================================-
// 再描画
//===========================================-
centerPanel.repaint();
}
private void resetAllButtons() {
/**
button1.setEnabled( true );
button2.setEnabled( true );
button3.setEnabled( true );
**/
Iterator iterator = menuButtonMap.keySet().iterator();
while ( iterator.hasNext() ) {
String command = ( String ) iterator.next();
JButton button = ( JButton ) menuButtonMap.get( command );
button.setEnabled( true );
}
}
private JButton getJButton( String command ) {
return ( JButton ) menuButtonMap.get( command );
}
/*****************/
/** MAINメソッド */
/*****************/
public static void main( String[] args ) {
ButtonMenuTest2 viewer = new ButtonMenuTest2();
/* 終了処理 */
viewer.addWindowListener( new WindowAdapter() {
public void windowClosing( WindowEvent e ) {
System.exit( 0 );
}
}
);
viewer.setBounds( 0, 0, 600, 800 );
// タイトルの表示
viewer.setTitle( "メニューをボタンで生成する" );
viewer.setVisible( true );
viewer.pack();
}
}
::::::::::::::
MenuContext.java
::::::::::::::
import java.util.*;
import java.util.regex.*;
import java.net.*;
import javax.swing.*;
import java.awt.event.*;
import java.awt.*;
/**
* $Id: menuContext.html,v 1.1 2009/06/22 16:12:17 kishi Exp kishi $
*/
public class MenuContext {
private String description;
public void setDescription( String description ) {
this.description = description;
}
public String getDescription() {
return description;
}
}
戻る