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(100200300,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(100200300,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(100200300,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+"
");
}
//获取光标
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;
原文地址:https://www.cnblogs.com/Tanqurey/p/10485337.html