1. JFrame 框架
注:一般情况下,设置框架的布局管理器,然后制作面板,将面板加入框架中,并不直接将组件加入框架上,因为后者有时会造成显示混乱。
常用方法:
1. Pack() //调整窗口大小以适合子组件的首选尺寸。
2. Settitle()//设置标题
3. Setbackground()//设置背景颜色
4. Setforeground()//设置前景颜色
5. getContentPane()//获取容器
6. setLocation() //设置窗口位置
7. setResizable() //设置窗口是否可变
注:setVisible()方法应放在最后调用,否则会发生不可知的错误。
eg:
setVisible(true);
setSize(20,,30);//这样设置尺寸无效果
代码如下:
<span style="color:#000099;">public static void main(String[] args) {
JFrame frame=new JFrame("shax"); //创建JFrame框架,并设置标题
frame.setSize(400, 500); //设置框架尺寸
Container containner=frame.getContentPane(); //获得容器
containner.setLayout(new FlowLayout(FlowLayout.LEFT)); //设置布局管理器
containner.add(new JButton("OK")); //加入组件
Dimension screenSize= Toolkit.getDefaultToolkit().getScreenSize(); //获取屏幕监视器尺寸
int height=screenSize.height;
int wight=screenSize.width;
Dimension frameSize=frame.getSize(); //获取框架尺寸
int height1=frameSize.height;
int width1=frameSize.width;
int x=(wight-width1)/2; //设置框架居中位置
int y=(height-height1)/2;
frame.setLocation(x, y); //设置框架位置
<span style="white-space: pre; "> </span>frame.setBackground(Color.white); //设置背景颜色
frame.setVisible(true); //设置框架的可见性
frame.setDefaultCloseOperation(javax.swing.JFrame.EXIT_ON_CLOSE); //设置关闭程序时是否终止程序
</span><span style="color:#00b0f0;">
}
</span>
以后会随时更新