自学一下设置Frame(画板类)的样式,并在其中添加一些简单的组件:TextField、Label、TextField(下面代码的注释内容为解释学习过程,理解性去记忆)
/*机房代码:*/
import java.awt.*;
public class MyFram extends Frame{
public MyFram(){
super("我的计算器(继承Frame类的实例)");//使用直接父类Frame的构造方法
//框架名字有了,那多大呢?在什么位置呢?所以接下来是设置大小、位置
//由于直接父类里没有设置大小、位置,
//以及后面添加其他组件(标签、文本行)的方法了,所以用this关键字,去找他的父类的父类
this.setSize(500, 100);
this.setLocation(100,100);
//还有一个问题:按钮怎么排版呢?竖着排?横着排?所以还要设置布局管理
//流布局(Panel类默认方式):按行布置,从左到右,当改变容器大小时,组件的相对位置会随容器大小而变化,但是始终会保持组件的尺寸不变
this.setLayout(new FlowLayout());
//当然,设置其他的布局管理器就有不一样的效果,其他就不在这演示了
//大小设置好了,计算器的按键得有吧,什么数字相加?计算框多大?是加法还是减法?计算器得有按钮吧?
//要解决这个问题就得用上组件(Component)中的其他组件:
//文本组件-文本行(TextField)、标签(Label)、按钮(Button)
this.add(new TextField("10", 10));
this.add(new Label("+"));
this.add(new TextField("20", 10));
//其他组件:颜色、字体下面只列举一个实例(字体还不好操作,以后需要了再学吧o_O):
this.setBackground(Color.lightGray);//设置灰色
this.add(new TextField(10));
this.setVisible(true);
}
public static void main(String[] args){
new MyFram();
}
}
//this.setLayout(new BorderLayout()); //这是Window类默认方式
//this.setLayout(new GridLayout(4,4)); 网格布局演示
//this.add(new Button("="));
//this.add(new Button("="));
//this.add(new Button("="));
//this.add(new Button("="));
//
//this.add(new Button("="));
//this.add(new Button("="));
//this.add(new Button("="));
//this.add(new Button("="));
//
//this.add(new Button("="));
//this.add(new Button("="));
//this.add(new Button("="));
//this.add(new Button("="));
//
//this.add(new Button("="));
//this.add(new Button("="));
//this.add(new Button("="));
//this.add(new Button("="));
3199






