メニューの作り方



Swingコンポーネントを用いたプルダウンメニューの例を以下に示す。

【例1】 メニュー項目が1つの場合

// メニューの表示
// 項目が一つの場合
import java.applet.*;
import java.awt.*;
import java.awt.image.*;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.border.*;
public class menu1 extends JApplet implements ActionListener{
int x=50,y=50;
String s1, s2;

public void init() {
    MyPanel cp = new MyPanel();
    setContentPane(cp);
    set_menu();  //メニューの設定
        System.out.println("Initが呼ばれました");
 } //end of init()

class MyPanel extends JPanel {
    public void paintComponent(Graphics g) {
//      super.paintComponent(g);
        if(s1=="処理A-1"){
                g.setColor(Color.red);
                g.drawOval(100,100,200,100);
        }
        if(s1=="処理A-2"){
                g.setColor(Color.blue);
                g.fillRect(100,100,200,100);
        }
    g.setColor(Color.black);
        g.drawString("メニューを選択してください",200,200);
        System.out.println("paintComponentが呼ばれました");
    }//end of paintComponent()
} //end of MyPanel extends JPanel{


//-------  メニューの設定  --------------------------------------
public void set_menu()
{
    // メニューバーの作成。
    JMenuBar menubar = new JMenuBar();  //メニューバーを作成
    this.setJMenuBar(menubar);         // メニューバーをアプレット(this)に追加

        // メニューA の作成。
    JMenu menuA = new JMenu("メニューA");    // メニューsyoriの作成
    menubar.add(menuA);   // メニューsyoriをメニューバーmenubarに追加
    JMenuItem fmA1 = new JMenuItem("処理A-1");
        menuA.add(fmA1);
        fmA1.addActionListener(this); 
    JMenuItem fmA2 = new JMenuItem("処理A-2");
        menuA.add(fmA2);
        fmA2.addActionListener(this); 
        
} // end of public void set_menu

//--------メニューが選択されたときの処理 -------------
    public void actionPerformed(ActionEvent e) {
            s1 = e.getActionCommand();

                if ( s1 == "処理A-1" ) {
                x=100; y=300;
                                System.out.println("処理A-1 が呼ばれました");
            }
                else if ( s1 == "処理A-2" ) {
                x=200; y=300;
                                System.out.println("処理A-2 が呼ばれました");
            }
    repaint();
    }//end of public void actionPerformed(ActionEvent e) {

}// end of public class Gazousyori


実行例



【例2】 メニュー項目が2つの場合


// メニューの表示
// 項目が2つの場合
import java.applet.*;
import java.awt.*;
import java.awt.image.*;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.border.*;
public class menu1 extends JApplet implements ActionListener{
int x=50,y=50;
String s1, s2;

public void init() {
    MyPanel cp = new MyPanel();
    setContentPane(cp);
    set_menu();  //メニューの設定
        System.out.println("Initが呼ばれました");
 } //end of init()

class MyPanel extends JPanel {
    public void paintComponent(Graphics g) {
//      super.paintComponent(g);
        if(s1=="処理A-1"){
                g.setColor(Color.red);
                g.drawOval(100,100,80,80);
        }
        if(s1=="処理A-2"){
                g.setColor(Color.blue);
                g.fillOval(50,150,250,180);
        }
        if(s2=="処理B-1"){
                g.setColor(Color.green);
                g.drawRect(100,200,100,80);
        }
        if(s2=="処理B-2"){
                g.setColor(Color.yellow);
                g.fillRect(200,100,200,200);
        }
    g.setColor(Color.black);
        g.drawString("メニューを選択してください",200,200);
        System.out.println("paintComponentが呼ばれました");
    }//end of paintComponent()
} //end of MyPanel extends JPanel{


//-------  メニューの設定  --------------------------------------
public void set_menu()
{
    // メニューバーの作成。
    JMenuBar menubar = new JMenuBar();  //メニューバーを作成
    this.setJMenuBar(menubar);         // メニューバーをアプレット(this)に追加

        // メニューA の作成。
    JMenu menuA = new JMenu("メニューA");    // メニューsyoriの作成
    menubar.add(menuA);   // メニューsyoriをメニューバーmenubarに追加
    JMenuItem fmA1 = new JMenuItem("処理A-1");
        menuA.add(fmA1);
        fmA1.addActionListener(this); 
    JMenuItem fmA2 = new JMenuItem("処理A-2");
        menuA.add(fmA2);
        fmA2.addActionListener(this); 
        
    // メニューB の作成。
    JMenu menuB = new JMenu("メニューB");    // メニューsyoriの作成
    menubar.add(menuB);   // メニューsyoriをメニューバーmenubarに追加
    JMenuItem fmB1 = new JMenuItem("処理B-1");
        menuB.add(fmB1);
        fmB1.addActionListener(this); 
    JMenuItem fmB2 = new JMenuItem("処理B-2");
        menuB.add(fmB2);
        fmB2.addActionListener(this); 
} // end of public void set_menu

//--------メニューが選択されたときの処理 -------------
    public void actionPerformed(ActionEvent e) {
            s1 = e.getActionCommand();

                if ( s1 == "処理A-1" ) {
                x=100; y=300;
                                System.out.println("処理A-1 が呼ばれました");
            }
                else if ( s1 == "処理A-2" ) {
                x=200; y=300;
                                System.out.println("処理A-2 が呼ばれました");
            }
            s2 = e.getActionCommand();
            if ( s2 == "処理B-1" ) {
                x=300;y=100;
                                System.out.println("処理B-1 が呼ばれました");
            }
            else if ( s2 == "処理B-2" ) {
                x=300;y=200;
                                System.out.println("処理B-2 が呼ばれました");
            }
    repaint();
    }//end of public void actionPerformed(ActionEvent e) {

}// end of public class Gazousyori


実行例