Frame案例
package cn.itcast_03;
import java.awt.Frame;
public class Frame案例 {
public static void main(String[] args) {
//创建窗体对象
Frame f = new Frame();
//但是这个窗体是不可见的
//所以,调用一个方法,设置让窗体课件
//f.show();过时方法
f.setVisible(true);
//设置窗体大小
f.setSize(400,300);//默认:像素
//设置窗体位置
f.setLocation(400, 200);
//设置窗体标题
f.setTitle("helloworld");
//窗体的标题可以通过构造给出
//Frame f = new Frame("helloworld");
}
}
优化Frame案例
package cn.itcast_03;
import java.awt.Frame;
public class 优化Frame案例 {
public static void main(String[] args) {
// 创建对象
Frame f = new Frame ("方法调用的前后关系");
f.setSize(400,300);
//设置长宽的第二种方法
//Dimension d = new Dimension();
//f.setSize(d);
f.setLocation(400, 200);
//设置窗口位置的第二种方法
//Point p = new Point(400,200);
//f.setLocation(p);
//一个方法搞定窗口位置和长宽
f.setBounds(400,200,400,300);
f.setVisible(true);
}
}
这篇博客介绍了Java AWT库中的Frame类的使用,包括如何创建并显示窗体,设置窗体的大小、位置和标题。通过示例代码展示了`setVisible()`, `setSize()`和`setLocation()`等方法的用法,并讨论了`bounds`属性的综合应用,以实现更高效的窗口管理。
714





