2020.5.11第一节课
必须导入以下两个包:
1.import java.awt.;
2.import javax.swing.;
public class Frame extends JFrame {
//创建标签
private JLabel label;
//创建按钮
private JButton button;
private JButton bts[]=new JButton[16];
//创建文本框
private JTextField field;
//标签
private void init() {
label=new JLabel(“姓名”);
//既能设置大小,也能设置位置
// label.setBounds(10, 10, 40, 40);
label.setSize(40, 40);//大小
label.setLocation(10,10);//位置
//label.setHorizontalAlignment(JLabel.CENTER);//居中显示
//label.setForeground(Color.red);//设置字体颜色
// label.setBackground(Color.ORANGE);//设置背景
button=new JButton("按钮");//按钮
button.setSize(40, 40);//大小
button.setLocation(50,10);//位置
//按钮是否可点击
//button.setEnabled(false);
//设置按钮
for(int i=0;i<bts.length;++i) {
bts[i]=new JButton(“按钮”+i);
}
//文本框的使用
field=new JTextField(“aaa”,20);
}
public Frame() {
init();//调用标签
//this.add(label);
//this.add(button);
//this.add(filed);在布局中显示文本框
//for(int i=0;i<bts.length;++i) {//调用按钮
//this.add(bts[i]);
//}
//1.流式布局(默认居中)
// setLayout(new FlowLayout(FlowLayout.LEFT));
//for(int i=0;i<bts.length;++i) {//调用按钮
//this.add(bts[i]);
//}
//2.网格布局
//setLayout(new GridLayout(4,4));
// for(int i=0;i<bts.length;++i) {//调用按钮
//this.add(bts[i]);
// }
//3.边界布局)
//setLayout(new BorderLayout());
//add(bts[0],BorderLayout.EAST);
//add(bts[1],BorderLayout.WEST);
//add(bts[2],BorderLayout.NORTH);
//add(bts[3],BorderLayout.SOUTH);
//add(bts[4],BorderLayout.CENTER);
//4.空布局:添加组件的时候,组件没有大小和位置,创建组件的时候给定大小和位置
setLayout(null);
this.add(label);
this.add(button);
//设置标题 大小 位置 可见性 窗口事件
//this.setResizable(false);//不能更改窗口大小
//获取屏幕大小和设置窗口在屏幕正中心
//Dimension dim=getToolkit().getScreenSize() ;
//int w=dim.width/2;
//int h=dim.height/2;
//this.setTitle("这是一个窗口");
//this.setSize(300, 300);
//this.setLocation(w-300/4, h-300/4);
this.setTitle("这是一个窗口");
this.setSize(300, 300);
this.setLocation(400, 300);
this.setVisible(true);
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
}
//用对象形式创建窗体
public void text() {
JFrame frame=new JFrame();
frame.setTitle(“这是一个窗口”);
frame.setSize(300, 300);
frame.setLocation(400, 300);
frame.setVisible(true);
frame.setDefaultCloseOperation(EXIT_ON_CLOSE);
}
}
最后需要一个测试类,进行运行
//测试类
public class text {
public static void main(String args[]) {
new Frame();
}
}