JAVA自学笔记25
JAVA自学笔记25
1、GUI
1)图形用户接口,以图形的方式,来显示计算机操作的界面,更方便更直观
2)CLI
命令行用户接口,就是常见的Dos,操作不直观
3)
类Dimension
类内封装单个对象组件中组件的宽度和高度(精确到整数)
Dimension(int width,int height);
类Point
//窗体案例
Frame f=new Frame();//也可通过带参构造设置标题String title,
//设置窗体标题
f.setTitle("dd");
//设置窗体大小
//f.setSize(100,200);
Dimension d=new Dimension(100,200);
//设置窗体位置
//f.setLocation(400,200);
Point p=new Point(400,200);
f.location(p);
//f.setBounds(int x.int y,int width,int height);//同时设置坐标及长宽
Thread.sleep(3000);//休眠3秒再弹出
f.setVisible(true);
2、事件监听器
1)把事件源和事件关联起来
2)
//窗体关闭案例
Frame f=new Frame();
f.setTitle("dd");
f.setBounds(100,200,300,400);/
Thread.sleep(3000);
//事件监听
f.addWindowListener(new WindowListener()
{
public void WindowClosing(WindowEvent e){
System.exit(0);
}
});
f.setVisible(true);
3)适配器设计模式
接口须重写方法较多且不需都实现时,可提供一个适配器类(仅仅空实现即可),在实现类重写即可
接口–适配器–实现类
WindowAdapter
适配器类
//窗体关闭案例
Frame f=new Frame();
f.setTitle("dd");
f.setBounds(100,200,300,400);
Thread.sleep(3000);
f.addWindowListener(new WindowAdapte(){
public void windowClosing(WindowEvent e);{
System.exit(0);
}
});
f.setVisible(true);
4)布局
//窗体添加按钮并对按钮添加事件
//使用流式布局
Frame f=new Frame();
f.setTitle("dd");
f.setBounds(100,200,300,400);
Thread.sleep(3000);
//设置布局为流式布局
f.setLayout(new FlowLayout());
//创建按钮对象
Button bu=new Button("按钮");
bu.setSize(10,20);
//把按钮添加到窗体
//设置窗体可以关闭
f.addWindowListener(new WindowAdapte(){
public void windowClosing(WindowEvent e);{
System.exit(0);
}
});
//设置点击事件
bu.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
System.out.println("success");
}
});
f.setVisible(true);
//把文本框的值转移到文本域
//创建窗体对象
Frame f=new Frame("数据转移");
//设置窗体属性和布局
f.setBounds(100,500,633,100);
f.setLayout(new FlowLayout());
//创建文本框
final TextField tf=new TextField(20);//文本框内可放20个字符
//创建按钮
Button bu=new Button("数据转移");
//创建文本域
final TextAreas ta=new TextArea(10,40);
//
//对按钮添加事件
bu.addActionListener(new ActionListener)(){
ppublic void actionPerformed(ActionEvent e){
//获取文本框的值
String tf_str=tf.getText().trim();
//设置给文本域并换行
ta.append(tf_stf+"\r\n");
}
//获取光标
tf.requestFocus();
//清空文本框数据
tf.setText("");
});
//把组件添加到窗体
f.add(tf);
f.add(bu);
f.add(ta);
//设置窗体关闭
f.addWindowListener(new WindowAdapte(){
public void windowClosing(WindowEvent e);{
System.exit(0);
}
});
//设置窗体显示
f.setVisible(true);
//更改背景色
//创建窗体对象
final Frame f=new Frame("更改背景色");
//设置窗体属性和布局
f.setBounds(100,500,633,100);
f.setLayout(new FlowLayout());
//创建四个按钮
Button redButton=new Button("red");
f.add(redButton);
//设置窗体关闭
f.addWindowListener(new WindowAdapte(){
public void windowClosing(WindowEvent e);{
System.exit(0);
}
});
//对按钮添加动作事件
/*redButton.addActionLostener(new ActionLister(){
public void actionPerformed(ActionEvent e){
f.setBackground(Color.RED);
}
});*/
//对按钮添加鼠标点击事件
/*redButton.addMouseListener(new MouseAdapter(){
public mouseClicked(MouseEvent e){
f.setBackground(color.RED);
}
});*/
//对按钮添加鼠标的进入事件
redButton.addMouseListener(new MouseAdapter)(){
public void mouseEntered(MouseEvent e){
f.setBackground(color.RED);
}
});
//对按钮添加鼠标的离开事件
redButton.addMouseListener(new MouseAdapter)(){
public void mouseExited(MouseEvent e){
f.setBackground(color.WHITE);
}
});
//设置窗体显示
f.setVisible(true);
//控制文本框只能输入数字字符
//创建窗体对象
final Frame f=new Frame("只能输入数字字符");
//设置窗体属性和布局
f.setBounds(100,500,633,100);
f.setLayout(new FlowLayout());
//创建Label标签对象
Label label=new Label("请输入QQ号码仅为数字");
TextFiled tf=new TextField(40);
//为文本框添加事件
tf.addKeyListener((new) KeyAdapter({
public void KeyPressed(KeyEvent e){
char ch=e.getKeyChar();
if(!(ch>='0'&&ch<='9')){}
e.consume();//使用此事件,以便不会按照默认的方式由产生的源代码来处理此事件
}
});
//添加到窗体上
f.add(label);
f.add(tf);
//设置窗体显示
f.setVisible(true);
//设置窗体关闭
f.addWindowListener(new WindowAdapte(){
public void windowClosing(WindowEvent e);{
System.exit(0);
}
});
菜单组件:
//一级菜单案例
//创建窗体对象
final Frame f=new Frame("只能输入数字字符");
//设置窗体属性和布局
f.setBounds(100,500,633,100);
f.setLayout(new FlowLayout());
//创建菜单栏
MenuBar mb=new MenuBar();
//创建菜单
Menu m=new Menu("文件");
//创建菜单项
MenuItem mi=new MenuItem("退出系统");
//
m.add(mi);
mb.add(m);
//设置菜单栏
f.setMenuBar(mb);
mi.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
System.exit(0);
}
});
//设置窗体显示
f.setVisible(true);
//设置窗体关闭
f.addWindowListener(new WindowAdapte(){
public void windowClosing(WindowEvent e);{
System.exit(0);
}
});
//设置多级别菜单
final Frame f=new Frame("设置多级菜单");
//设置窗体属性和布局
f.setBounds(100,500,633,100);
f.setLayout(new FlowLayout());
//创建菜单栏
MenuBar mb=new MenuBar();
//创建菜单
Menu m1=new Menu("文件");
Menu m2=new Menu("更改名称");
//创建菜单项
MenuItem mi1=new MenuItem("退好好学习");
MenuItem mi2=new MenuItem("天天向上");
MenuItem mi3=new MenuItem("恢复标题");
MenuItem mi4=new MenuItem("退打开记事本");
MenuItem mi5=new MenuItem("退出系统");
//
m2.add(mi1);
m2.add(mi2);
m2.add(mi3);
m1.add(m2);
m1.add(mi4);
m1.add(mi5);
//设置菜单栏
f.setMenuBar(mb);
//设置事件
mi5.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
System.exit(0);
}
});
//设置事件
mi4.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
Runtime r=Runtime.getRuntime();
r.exec("notepad");
}
});
//设置事件
mi1.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
f.setTitle(mi1.getLabel());
}
});
//设置事件
mi2.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
f.setTitle("多级菜单");
}
});
//设置事件
mi3.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
f.setTitle(mi1.getLabel());
}
});
//设置窗体关闭
f.addWindowListener(new WindowAdapte(){
public void windowClosing(WindowEvent e);{
System.exit(0);
}
});
//设置窗体显示
f.setVisible(true);
4、Netbeans
1)界面开发常用
//修改窗体图标
public class UiUtill{
private Uiutil(){}
public static void setFrameImage(JFrame jf){
//获取工具类对象
Toolkit tk=ToolKit.getDefaultToolKit();
//根据路径获取图片
Image i=tk.getImage("src\\cn\\itcast\\resource\\a.jpg");
//给窗体设置图片
jf.setIconImage(i);
}
}
//设置窗体居中
//获取工具对象
Toolkit tk=Toolkit.getDefaultTookit();
//获取屏幕宽高
Dimension d=tk.getScreenSize();
double screenWidth=d.getWidth();
double screenHeight=d.getHeight();
//获取窗体宽高
int frameWidth=jf.getWidth();
int frameHeight=jf.getHeight();
//获取新的宽高
int width=(int)(screenWidth-frameWidth)/2;
int height=(int)(screenHeight-frameHeight)/2;
JAVA自学笔记25的更多相关文章
- JAVA自学笔记09
JAVA自学笔记09 1.子类的方法会把父类的同名方法覆盖(重写) 2.final: 1)可修饰类.方法.变量 2)修饰类时:此时该类变为最终类,它将无法成为父类而被继承 3)修饰方法时:该方法将无法 ...
- JAVA自学笔记05
JAVA自学笔记05 1.方法 1)方法就是完成特定功能的代码块,类似C语言中的函数. 2)格式: 修饰符 返回值类型 方法名(参数类型 参数名1,参数类型 参数名2,-){ 函数体; return ...
- JAVA自学笔记06
JAVA自学笔记06 1.二维数组 1)格式: ①数据类型[][]数组名 = new 数据类型[m][n]; 或 数据类型[]数组名[]=new 数据类型[m][n]; m表示这个二维数组有多少个一维 ...
- JAVA自学笔记04
JAVA自学笔记04 1.switch语句 1)格式:switch(表达式){ case 值1: 语句体1; break; case 值2: 语句体2; break; - default: 语句体n+ ...
- JAVA自学笔记07
JAVA自学笔记07 1.构造方法 1) 例如:Student s = new Student();//构造方法 System.out.println(s);// Student@e5bbd6 2)功 ...
- JAVA自学笔记10
JAVA自学笔记10 1.形式参数与返回值 1)类名作为形式参数(基本类型.引用类型) 作形参必须是类的对象 2)抽象类名作形参 需要该抽象类的子类对象,通过多态实现 3)接口名为形参 需要的是该接口 ...
- JAVA自学笔记13
JAVA自学笔记13 1.StringBuffer类 1)线程安全的可变字符序列 线程安全(即同步) 2)StringBuffer与String的区别:一个可变一个不可变 3)构造方法: ①publi ...
- JAVA自学笔记11
JAVA自学笔记11 1:Eclipse的安装 2:用Eclipse写一个HelloWorld案例,最终在控制台输出你的名字 A:创建项目 B:在src目录下创建包.cn.itcast C:在cn.i ...
- JAVA自学笔记14
JAVA自学笔记14 1.正则表达式 1)是指一个用来描述或者匹配一系列符合某个句法规则的字符串的单个字符串.其实就是一种规则.有自己的特殊应用 2)组成规则: 规则字符在java.util.rege ...
随机推荐
- EF连接MySql数据库
Windows要想EF连接MySql,首先要安装两个应用程序 mysql-connector-net-6.8.8.msimysql-for-visualstudio-1.2.7.msi 项目还需要两个 ...
- Visual Studio 中使用万能头文件 #include <bits/stdc++.h>
最近开始使用VS,之前用的DEV C++软件可直接使用 #include <bits/stdc++.h> ,但VS中并没有,为了使用方便,可直接在VS中添加此头文件,方法如下: 1.在安 ...
- 一起学Hadoop——TotalOrderPartitioner类实现全局排序
Hadoop排序,从大的范围来说有两种排序,一种是按照key排序,一种是按照value排序.如果按照value排序,只需在map函数中将key和value对调,然后在reduce函数中在对调回去.从小 ...
- md5爆破工具
http://www.myhack58.com/Article/html/3/8/2015/65021.htm http://xlixli.net/?p=410 http://blog.csdn.ne ...
- nginx配置2
第四节 nginx 配置文件 1 keepalive_timeout 65; 设定保存长久连接时长 0代表禁止, 若不设置默认是75s 2keepalive_requests nu; 在一 ...
- Linux read line
cat ./port_list|while read linedo done
- P1026 统计单词个数 区间dp
题目描述 给出一个长度不超过200200的由小写英文字母组成的字母串(约定;该字串以每行2020个字母的方式输入,且保证每行一定为2020个).要求将此字母串分成kk份(1<k \le 401& ...
- spring框架等web程序在tomcat下的启动顺序
http://www.cnblogs.com/panxuejun/p/5847774.html
- jquery模拟form表单提交并新打开页面
/** * form表单提交本页面打开 * @param url * @param params */ function postCurrent(url,params){ var form = $(& ...
- 爬虫3 requests基础
import requests # get实例 # res = requests.get('http://httpbin.org/get') # # res.encoding='utf-8' # pr ...