Google

建立窗口和菜单(一)

2007-07-12 22:37 来源: china1985.cublog.cn 作者:china1985 网友评论 0 条 浏览次数 55
import java.awt.*;
import java.awt.event.*;

class MyFrame extends Frame implements ItemListener,ActionListener{
    Checkbox box;
    TextArea text;
    Button button;
    
    MyFrame(String s){
        super(s);//super()要写在构造函数最开始的地方
        box = new Checkbox("设置窗口是否可调整大小");
        text = new TextArea(12,12);
        button = new Button("关闭窗口");
        button.addActionListener(this);//对按钮“关闭窗口”进行监听
        box.addItemListener(this);//对复选框进行监听
        setBounds(100,100,200,300);//设置窗口的位置在屏幕上的坐标为(100,100),窗口大小宽设置为200,高设置为300
        setVisible(true);//设置窗口可见,默认时窗口不可见
        //设置组件的位置
        add(text,BorderLayout.CENTER);
        add(button,BorderLayout.NORTH);
        add(box,BorderLayout.SOUTH);
        setResizable(false);//把窗口设置为不可调整
        validate();//确保当前添加的组件不以窗口的调整而改变,使窗口里的组件正常显示
        }
    public void itemStateChanged(ItemEvent e){
        if(box.getState()==true){
            setResizable(true);//如果复选框被选中的话,那么窗体的大小就可以通过鼠标来调整
            }
        else{
            setResizable(false);
            }
        }
    public void actionPerformed(ActionEvent e){
   
            dispose();//发生ActionEvent,撤销当前窗口,并释放当前窗口所占资源
           
        }
    }
   
   public class Test41{//主类
        public static void main(String args[]){
            new MyFrame("窗体");//调用函数MyFrame()
            }
        }
上一篇: 下一篇:

相关主题:

网友评论