菜单对话框的例子并绘图标


  这是一个菜单的例子,在关闭时弹出对话框

  绘一个图标,放在弹出对话框的左边


  1import java.awt.*;
  2import java.awt.event.*;
  3class ConfirmDialog extends Dialog implements ActionListener
  4{
  5    private StopSign stop=new StopSign();
  6    private Button okay=new Button("OK");
  7    private Button cancel=new Button("cancel");
  8    private Label label=new Label("Are you sure?",Label.CENTER);
  9    public  boolean isOkay=false;
 10    private class WindowCloser extends WindowAdapter
 11    {
 12        public void windowClosing(WindowEvent we)
 13        {
 14            ConfirmDialog.this.isOkay=false;
 15            ConfirmDialog.this.hide();
 16        }

 17    }

 18    public ConfirmDialog(Frame parent)
 19    {
 20        this(parent,"Please confirm","Are you sure?");
 21    }

 22    public ConfirmDialog(Frame parent, String title, String question)
 23    {
 24        super(parent,title, true);
 25        label.setText(question);
 26        setup();
 27        okay.addActionListener(this);
 28        cancel.addActionListener(this);
 29        addWindowListener(new WindowCloser());
 30        setResizable(false);//不能改变对话框的大小
 31        pack();
 32        show();
 33    }

 34    private void setup()
 35    {
 36        Panel buttons=new Panel();
 37        buttons.setLayout(new FlowLayout());
 38        buttons.add(okay);
 39        buttons.add(cancel);
 40        setLayout(new BorderLayout());
 41        add("Center",label);
 42        add("South",buttons);
 43        add("West",stop);
 44    }

 45    public void actionPerformed(ActionEvent ae)
 46    {
 47        isOkay=(ae.getSource()==okay);
 48        hide();
 49    }

 50}

 51class StopSign extends Canvas
 52{
 53    public StopSign()
 54    {
 55        super();
 56        setBackground(Color.cyan);
 57    }

 58    public Dimension getPreferredSize()  //覆盖该方法
 59    {
 60        return new Dimension(120,140);
 61    }

 62    public void paint(Graphics g)
 63    {
 64        final int MID=60;
 65        final int TOP=20;
 66        
 67        g.setColor(Color.blue);
 68        g.fillRect(0,175,300,50);//背景;填充指定的矩形
 69
 70        g.setColor(Color.yellow); 
 71        g.fillOval(-20,-20,40,40);  //太阳;用当前颜色填充由指定矩形限定的椭圆
 72
 73        g.setColor(Color.white);
 74        g.fillOval(MID-20,TOP,40,40);  //
 75        g.fillOval(MID-35,TOP+35,70,50);//上躯干
 76        g.fillOval(MID-50,TOP+80,100,60);//下躯干
 77
 78        g.setColor(Color.black);
 79        g.fillOval(MID-10,TOP+10,5,5);  //左眼
 80        g.fillOval(MID+5,TOP+10,5,5); //右眼
 81
 82        g.drawArc(MID-10, TOP+202010190160);//笑容
 83        
 84        g.drawLine(MID-25,TOP+60, MID-50, TOP+30);  //左手臂
 85        g.drawLine(MID+25,TOP+60, MID+55, TOP+30);  //右手臂
 86
 87        g.drawLine(MID-20, TOP+5, MID+20, TOP+5);  //帽子的边沿
 88        g.fillRect(MID-15,TOP-20,30,25);//帽顶
 89    }

 90}

 91public class MenuTest  extends  Frame implements ActionListener 
 92{
 93    private MenuItem fileNew=new MenuItem("New");
 94    private MenuItem fileOpen=new MenuItem("Open");
 95    private MenuItem fileExit=new MenuItem("Exit");
 96    private MenuItem editCut=new MenuItem("Cut");
 97    private MenuItem editCopy=new MenuItem("Copy");
 98    private MenuItem editPaste=new MenuItem("Paste");
 99    public MenuTest()
100    {
101        super("Menu Test Program");
102        Menu file=new Menu("File");
103        file.add(fileNew);  fileNew.setEnabled(false);
104        file.add(fileOpen); fileOpen.setEnabled(false);
105        file.addSeparator();  //在指定位置分隔一行
106        file.add(fileExit); fileExit.setEnabled(true);
107        Menu edit=new Menu("Edit");
108        edit.add(editCut);  editCut.setEnabled(false);
109        edit.add(editCopy); editCopy.setEnabled(false);
110        edit.add(editPaste);editPaste.setEnabled(false);
111        MenuBar bar=new MenuBar();
112        bar.add(file);
113        bar.add(edit);
114        setMenuBar(bar);
115        fileExit.addActionListener(this);
116        setSize(100,100);
117        show();
118    }

119    public void actionPerformed(ActionEvent e)
120    {
121        if(e.getSource()==fileExit)
122        {
123            ConfirmDialog exit=new ConfirmDialog(this,"Confirm Exit","Do you really want to exit?");
124            if(exit.isOkay)
125               System.exit(0);
126        }

127    }

128    public static void main(String[] args) 
129    {
130        MenuTest f=new MenuTest();
131    }

132}

133
posted on 2005-11-09 23:16  newzpflying  阅读(422)  评论(0)    收藏  举报