JButtonでメニュー画面を作る

戻る






import java.util.*; import java.util.regex.*; import java.net.*; import javax.swing.*; import java.awt.event.*; import java.awt.*; /** * $Id: ButtonMenuTest.html,v 1.1 2009/06/22 16:11:39 kishi Exp kishi $ */ public class ButtonMenuTest extends JFrame implements ActionListener { // 本当は全てJPanelを継承した別クラスにして分離します // ここは便宜上1個のクラスにまとめています private JPanel topPanel; private JPanel centerPanel; private JPanel bottomPanel; private JButton button1; private JButton button2; private JButton button3; private JLabel messageLabel; /** * コンストラクタ */ public ButtonMenuTest() { // 上部に配置するパネル topPanel = new JPanel(); topPanel.setPreferredSize( new Dimension( 640, 40 ) ); button1 = new JButton( "アイテム登録" ); topPanel.add( button1 ); button1.setActionCommand( "CREATE_ITEM" ); button1.addActionListener( this ); button2 = new JButton( "刻印データ出力" ); topPanel.add( button2 ); button2.setActionCommand( "SYMBOL_DATA" ); button2.addActionListener( this ); button3 = new JButton( "検査" ); topPanel.add( button3 ); button3.setActionCommand( "INSPECTION" ); button3.addActionListener( this ); // 中心に配置するパネル 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 ); resetAllButtons(); button1.setEnabled( false ); } else if ( "SYMBOL_DATA".equals( command ) ) { centerPanel.add( new JButton( "ヒロシです。刻印するとです。" ) ); centerPanel.add( new JLabel( "データは、CSV形式になるとです。" ) ); resetAllButtons(); button2.setEnabled( false ); } else if ( "INSPECTION".equals( command ) ) { JButton button = new JButton( "▲検査をするとです▲" ); button.setBackground( Color.orange ); centerPanel.add( button ); resetAllButtons(); button3.setEnabled( false ); } centerPanel.repaint(); } private void resetAllButtons() { button1.setEnabled( true ); button2.setEnabled( true ); button3.setEnabled( true ); } /*****************/ /** MAINメソッド */ /*****************/ public static void main( String[] args ) { ButtonMenuTest viewer = new ButtonMenuTest(); /* 終了処理 */ 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(); } }
戻る inserted by FC2 system