Java Appletの解説
<23.メニュー>
フレームに付けるメニューについて解説します。
1.メニュー
メニュー、サブメニューおよびチェックマーク付きメニューの解説します。
メニュー以外の部分の解説は省略します
1.<Javaファイル>
−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−
mport java.applet.Applet;
import java.awt.*;
import java.awt.event.*;
import java.lang.*;
public class temenu6 extends Applet{
String sp,sp1="未入力",sp2="未入力",sp3="未入力",sp4="未入力";
String cau="",cau1="注意:Enterキーを押して入力";
double tfju;
int x1,y1,w1,h1;
TextField tf1,tf2,tf3,tf4;
mytemenu6 w;
public void init(){
Button bt;
Panel p=new Panel();
p.setLayout(new GridLayout(3,4,0,10));
p.add(new Label("X座標"));
p.add(tf1=new TextField("",4));
p.add(new Label("Y座標"));
p.add(tf2=new TextField("",4));
p.add(new Label("幅"));
p.add(tf3=new TextField("",4));
p.add(new Label("高さ"));
p.add(tf4=new TextField("",4));
p.add(new Label(""));
p.add(bt=new Button("表示"));
add("Center",p);
w=new mytemenu6("menu1");
tf1.requestFocus();
tf1.addActionListener(new MyListener());
tf2.addActionListener(new MyListener());
tf3.addActionListener(new MyListener());
tf4.addActionListener(new MyListener());
bt.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
if(sp1=="未入力"||sp2=="未入力"||sp3=="未入力"||sp4=="未入力")
{cau=cau1;repaint();}
else {cau="";repaint();}
x1=Integer.valueOf(sp1).intValue();
y1=Integer.valueOf(sp2).intValue();
w1=Integer.valueOf(sp3).intValue();
h1=Integer.valueOf(sp4).intValue();
w.init(x1,y1,w1,h1);
}});}
class MyListener implements ActionListener{
public void actionPerformed(ActionEvent e){
TextField t=(TextField)e.getSource();
sp=t.getText();
tfju=Double.valueOf(sp).doubleValue();
if (Double.isNaN(tfju)) {if(t==tf1){sp1="0";}
else if(t==tf2){sp2="0";}
else if(t==tf3){sp3="0";}
else if(t==tf4){sp4="0";}
}
else {if(t==tf1){sp1=sp;tf1.transferFocus();}
else if(t==tf2){sp2=sp;tf2.transferFocus();}
else if(t==tf3){sp3=sp;tf3.transferFocus();}
else if(t==tf4){sp4=sp;}
}}}
public void paint(Graphics g){transferFocus();
g.setFont(new Font("TimesRoman",Font.BOLD,16));
g.setColor(Color.red);
g.drawString(cau,20,150);
}}
class mytemenu6 extends Frame{
int wx1,wy1,ww1,wh1;
Graphics g;
byte ovl=0;
public mytemenu6(String t){
setSize(400,300);
setTitle(t);}
public void init(int tn1,int tn2,int tn3,int tn4){
setBackground(Color.white);
setForeground(Color.blue);
wx1=tn1+5;
wy1=tn2+55;
ww1=tn3;
wh1=tn4;
MenuItem wmet1,wmet2,wcol1,wcol2,wcol3;
MenuItem subwmet1,subwmet2,subwmet3,subwmet4;
CheckboxMenuItem wmet3;
MenuBar wmb=new MenuBar();
Menu wm1=new Menu("方法");
wm1.addSeparator();
Menu swm1=new Menu("四角形");
swm1.add(subwmet1=new MenuItem("枠"));
swm1.add(subwmet2=new MenuItem("中塗り"));
wm1.add(swm1);
Menu swm2=new Menu("円");
swm2.add(subwmet3=new MenuItem("枠"));
swm2.add(subwmet4=new MenuItem("中塗り"));
wm1.add(swm2);
wm1.addSeparator();
wm1.add(wmet3=new CheckboxMenuItem("重ねて表示"));
wm1.addSeparator();
wm1.add(wmet1=new MenuItem("消去"));
wm1.addSeparator();
wm1.add(wmet2=new MenuItem("閉じる"));
wm1.addSeparator();
Menu wm2=new Menu("色");
wm2.addSeparator();
wm2.add(wcol1=new MenuItem("青"));
wm2.add(wcol2=new MenuItem("赤"));
wm2.add(wcol3=new MenuItem("緑"));
wm2.addSeparator();
wmb.add(wm1);
wmb.add(wm2);
setMenuBar(wmb);
wmet1.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
g=getGraphics();
g.clearRect(0,0,getSize().width-1,getSize().height-1);
}});
wmet2.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
dispose();
}});
wmet3.addItemListener(new ItemListener(){
public void itemStateChanged(ItemEvent e){
ovl=(byte)(ovl^1);
}});
subwmet1.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
g=getGraphics();
if(ovl==0){g.clearRect(0,0,getSize().width-1,getSize().height-1);}
g.drawRect(wx1,wy1,ww1,wh1);
}});
subwmet2.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
g=getGraphics();
if(ovl==0){g.clearRect(0,0,getSize().width-1,getSize().height-1);}
g.fillRect(wx1,wy1,ww1,wh1);
}});
subwmet3.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
g=getGraphics();
if(ovl==0){g.clearRect(0,0,getSize().width-1,getSize().height-1);}
g.drawOval(wx1,wy1,ww1,wh1);
}});
subwmet4.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
g=getGraphics();
if(ovl==0){g.clearRect(0,0,getSize().width-1,getSize().height-1);}
g.fillOval(wx1,wy1,ww1,wh1);
}});
wcol1.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
setForeground(Color.blue);
}});
wcol2.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
setForeground(Color.red);
}});
wcol3.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
setForeground(Color.green);
}});
show();
addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e){
dispose();
}});}}
−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−
例1.ここをクリックして下さい。
2.<解説>
1)初期設定
1.int wx1,wy1,ww1,wh1;四角形、円を描くための座標(wx1,wy1)、幅ww1、高さwh1
2.byte ovl=0;図形を重ねて表示するかどうかのフラッグ
3.MenuItem wmet1,wmet2,wcol1,wcol2,wcol3;メニュー
4.MenuItem subwmet1,subwmet2,subwmet3,subwmet4;サブメニュー
5.CheckboxMenuItem wmet3;チェックボックスメニュー
6.MenuBar wmb=new MenuBar();メニューバー生成
2)メニュー生成
1.Menu wm1=new Menu("方法");親メニュー
2.wm1.addSeparator();メニューセパレータ:メニューに線を入れて分ける。
3.Menu swm1=new Menu("四角形");子メニュー
4."四角形"のサブメニュー
swm1.add(subwmet1=new MenuItem("枠"));
swm1.add(subwmet2=new MenuItem("中塗り"));
5.wm1.add(swm1);サブメニューをメニューに追加。
以下同じ様に"方法"の親メニューに"円" "重ねて表示""消去""閉じる"を追加します。
6.wm1.add(wmet3=new CheckboxMenuItem("重ねて表示"));
チェックボックスメニューを追加する。
7.Menu wm2=new Menu("色");
"色"の親メニューも同じように子メニューを追加します。
8.親メニューをメニューバーに追加。
wmb.add(wm1);
wmb.add(wm2);
9.setMenuBar(wmb);メニューバーをフレームに追加。
3)メニューの処理
wmet1.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
g=getGraphics();
g.clearRect(0,0,getSize().width-1,getSize().height-1);
}});
これはwmet1メニューに対する処理です。これは消去を意味します。
その他の処理の解説は省略します。
娯楽室の各部屋