您当前的位置: 首页 > 学无止境 > 心得笔记 网站首页心得笔记
javase第三季学习笔记-GUI组件介绍
发布时间:2017-08-10 13:47:12编辑:雪饮阅读()
GUI概述
• GUI编程(Graphic User Interface,图形用户接口)
• GUI的各种元素,如:容器、按钮、文本框等
Frame类
• Frame类是抽象类Window的子类,Frame对象显示效果是一个“窗体”,带有标题和尺寸重置角标。
• Frame的常用构造方法:
– Frame()
– Frame(String s) 创建指定标题的 Frame 对象
• void setTitle(String title)
将此窗体的标题设置为指定的字符串。
• void dispose()
释放由此 Window、其子组件及其拥有的所有子组件所使用的所有本机屏幕资源。
• void setSize(int width, int height)
调整组件的大小,使其宽度为 width,高度为 height。
• void setResizable(boolean resizable)
设置此窗体是否可由用户调整大小。
• void setVisible(boolean b)
根据参数 b 的值显示或隐藏此 Window。
• void setLocation(int x, int y)
将组件移到新位置。
• void setBackground(Color c)
设置组件的背景色。
代码示例:
package com.vince.gui;
import java.awt.Color;
import java.awt.Frame;
//窗体类
public class FrameDemo {
public static void main(String[] args) {
//创建一个窗体对象
Frame f=new Frame();
//设置标题
f.setTitle("我的第一个窗体程序");
//设置窗体大小
f.setSize(300,200);
//设置背景色
f.setBackground(Color.blue);
//设置不可调整大小
f.setResizable(false);
//设置窗体位置
f.setLocation(300,300);
//显示窗体
f.setVisible(true);
}
}
Button类
• Button类:
• 此类创建一个标签按钮。当按下该按钮时,应用程序能执行某项动作。
• Button()
构造一个标签字符串为空的按钮。
• Button(String label)
构造一个带指定标签的按钮。
示例代码:
package com.vince.gui;
import java.awt.Button;
import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.Frame;
public class MyFrame extends Frame {
public MyFrame(){
//设置标题
this.setTitle("我的第一个窗体程序");
//设置窗体大小
this.setSize(300,200);
//设置背景色
this.setBackground(Color.blue);
//设置不可调整大小
this.setResizable(false);
//把窗体默认布局设置为null
/*
如果按钮没有设置位置,则按照默认布局会将按钮显示到正中间,并且因为只有一个元素,所以按钮大小占满窗体
这是默认布局,如果将默认布局设置为null,此时按钮没有设置位置,则窗体中看不到按钮
默认布局采用东南西北中布局方式
*/
//this.setLayout(null);
//设置布局方式为流水布局
this.setLayout(new FlowLayout());
//在窗体上添加按钮
//创建一个按钮对象
Button b1=new Button("点我一下");
this.add(b1);
Button b2=new Button("点我一下2");
this.add(b2);
this.add(new Button("点我一下3"));
this.add(new Button("点我一下4"));
this.add(new Button("点我一下5"));
//设置窗体位置
this.setLocation(300,300);
//显示窗体
this.setVisible(true);
}
public static void main(String[] args){
new MyFrame();
}
}
Panel类
• Panel 是最简单的容器类。应用程序可以将其他组件放在面板提供的空间内,这些组件包括其他面板。
• 面板的默认布局管理器是 FlowLayout 布局管理器。
• setSize(int width, int height)
• 设置大小
• setLocation(int x, int y)
• 设置窗体的位置,x、y是左上角的坐标
• setBounds(int x, int y, int width, int height)
• 设置位置、宽度和高度
• setBackground(Color c)
• 设置背景颜色,参数为Color对象
• setLayout(LayoutManager mgr)
• 设置布局管理器
示例代码:
package com.vince.gui;
import java.awt.Button;
import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.Frame;
import java.awt.Panel;
public class PanelDemo extends Frame{
public PanelDemo(){
//设置标题
this.setTitle("面板程序");
//设置窗体大小
this.setSize(300,200);
//设置背景色
this.setBackground(Color.blue);
//设置不可调整大小
//this.setResizable(false);
//设置窗体布局方式为流水布局
this.setLayout(new FlowLayout());
this.addPanel();
//显示窗体
this.setVisible(true);
}
//添加面板
public void addPanel(){
//Panel默认布局方式就是流水布局
Panel p1=new Panel();
//设置面板颜色
p1.setBackground(Color.GREEN);
p1.add(new Button("点我一下"));
p1.add(new Button("点我一下"));
p1.add(new Button("点我一下"));
//将面板添加到窗体中
this.add(p1);
Panel p2=new Panel();
//设置面板颜色
p2.setBackground(Color.GREEN);
p2.add(new Button("看我一下"));
p2.add(new Button("看我一下"));
p2.add(new Button("看我一下"));
//将面板添加到窗体中
this.add(p2);
}
public static void main(String[] args) {
new PanelDemo();
}
}
Toolkit类
• Toolkit抽象类是用于将各种组件绑定到本地系统的工具包。
• getDefalutToolkit()
– 静态方法可以得到一个Toolkit的子类实例
• Dimension getScreenSize()
– 把屏幕尺寸作为一个Dimension实例返回
• Image getImage(URL url)
– 返回一幅图像,该图像从指定文件中获取像素数据,
– 图像格式可以是 GIF、JPEG 或 PNG
– 在eclipse中配置GUI可视化编辑插件
• VE插件:
• http://wiki.eclipse.org/VE/Update
• 目前最新1.5版 本只支持 eclipse 3.6
示例:设置窗体水平、垂直居中以及窗体icon图标
首先在eclispe中当前项目下src上新建一个包,用来存储图片资源,如:
然后将本地的随便一个图片(图像格式可以是 GIF、JPEG 或 PNG
)复制后,点击刚才新建的包名上ctrl+v就进去了
然后结构如下:
示例代码:
package com.vince.gui;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Frame;
import java.awt.Image;
import java.awt.Toolkit;
import java.net.URL;
public class ToolketDemo extends Frame {
public ToolketDemo(){
this.setTitle("Toolket工具类的使用");
this.setSize(600,400);
this.setBackground(Color.blue);
//-------------使窗体在整个屏幕中水平和垂直都居中开始----------------------
//获取工具对象
Toolkit tool=Toolkit.getDefaultToolkit();
//获取当前屏幕的尺寸
Dimension d= tool.getScreenSize();
double h=d.getHeight();
double w=d.getWidth();
//窗体x轴
int x=(int)(w-600)/2;
//窗体y轴
int y=(int)(h-400)/2;
//设置窗体位置
this.setLocation(x,y);
//-------------使窗体在整个屏幕中水平和垂直都居中结束----------------------
//-------------获取图片----------------------
//通过当前类的类加载器获取图片资源
URL url=this.getClass().getClassLoader().getResource("com/vince/image/notimg.gif");
Image image=tool.getImage(url);
//-------------获取图片结束----------------------
//设置窗体图标
this.setIconImage(image);
this.setLayout(new FlowLayout());
this.setVisible(true);
}
public static void main(String[] args) {
new ToolketDemo();
}
}
关键字词:javase,GUI