javaGUI编程

javaGUI编程



Frame初见

public class TestFrame1 {
    public static void main(String[] args) {
        Frame frame = new Frame();      //通过Frame类创建一个Frame对象
        frame.setVisible(true);         //设置该对象为可见
        frame.setSize(800,800);     	//设置对象的大小
        frame.setBackground(Color.blue);           //设置对象的背景颜色
        frame.setLocation(300,300);         	   //设置对象的初始位置
        frame.setResizable(false);                 //设置对象不可拉伸边框
    }
}

MyFrame的封装

public class TestFrame2 {
    public static void main(String[] args) {
        MyFrame myFrame = new MyFrame(300,300,800,800,Color.blue);
        myFrame.addWindowListener(new WindowAdapter() {
            @Override
            public void windowClosing(WindowEvent e) { 		//设置监听器用于关闭窗口!
                System.exit(0);
            }
        });
    }
}
class MyFrame extends Frame {
    static int id = 0;  //设置id计数器以方便记录窗口数量
    public MyFrame(int x,int y,int w,int h,Color color){ Frame frame = new Frame();
    setSize(w,h);
    setBackground(color);
    setLocation(x,y);
    setResizable(false);
    setVisible(true);

    }
}

panel面板

public class TestFrame3 {
    public static void main(String[] args) {
        Frame frame = new Frame();
        Panel panel = new Panel();
        panel.setBackground(Color.white);
        panel.setBounds(150,150,150,150);
        frame.setResizable(false);
        frame.setLayout(null);      //将frame默认的流式布局关闭
        frame.add(panel);           //将panel面板添加到frame中
        frame.setBounds(300,300,500,500);
        frame.setBackground(new Color(2, 88, 73));  		//使用new Color可以设置颜色!
        frame.setLocation(500,500);
        frame.setVisible(true);
        frame.addWindowListener(new WindowAdapter() {
            @Override
            public void windowClosing(WindowEvent e) {      //窗口关闭的时候执行的命令!
                System.exit(0);
            }
        }
        );
    }
}

三种布局方式

public class TestAllLayout {
    public static void main(String[] args) {
        MyFrame frame = new MyFrame(300,300,900,900,Color.green) ;

        Button button1 = new Button("button1");
        Button button2 = new Button("button2");
        Button button3 = new Button("button3");
        Button button4 = new Button("button4");
        Button button5 = new Button("button5");
        Button button6 = new Button("button6");
//     frame.setLayout(new FlowLayout(FlowLayout.LEFT));     //流式布局!
//     frame.add(button1,BorderLayout.EAST);                         //Border布局!
//     frame.add(button2,BorderLayout.SOUTH);
//     frame.add(button3,BorderLayout.WEST);
//     frame.add(button4,BorderLayout.NORTH);
//     frame.add(button5,BorderLayout.CENTER);
        frame.setLayout(new GridLayout(3,2));
        frame.add(button1);
        frame.add(button2);
        frame.add(button3);
        frame.add(button4);
        frame.add(button5);
        frame.add(button6);
        frame.pack();   //选择最优的布局!
        frame.addWindowListener(new WindowAdapter() {
            @Override
            public void windowClosing(WindowEvent e) {
                System.exit(0);
            }
        });
    }
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值