第二十五天????399
1:如何让Netbeans的东西Eclipse能访问。????399
2:GUI(了解)????399
(1)用户图形界面????399
(2)两个包:????399
(3)主要方法:????399
A:javax.swing包下的方法????399
B:监听机制的应用????400
(4)GUI的继承体系????400
(5)事件监听机制(理解)????401
(6)适配器模式(理解)????401
案例解释????401
1. UserDao(顶层接口)????401
2. UserAdapter(适配器类,实现顶层接口)????401
3. UserDaoImpl(具体实现类,需继承UserAdapter适配器类)????402
(7)案例:????402
A:创建窗体案例????402
B:窗体关闭案例????403
C:窗体添加按钮并对按钮添加事件案例。????404
界面中的组件布局。????404
D:把文本框里面的数据转移到文本域????405
E:更改背景色????407
F:设置文本框里面不能输入非数字字符????409
G:一级菜单????410
H:多级菜单????411
(8)Netbeans的概述和使用????414
?
第二十五天
1:如何让Netbeans的东西Eclipse能访问。
????在Eclipse中创建项目,把Netbeans项目的src下的东西给拿过来即可。
????注意:修改项目编码为UTF-8
????
2:GUI(了解)
????(1)用户图形界面
????????GUI:方便直观
????????CLI:需要记忆一下命令,麻烦
????(2)两个包:
????????java.awt:和系统关联较强(重量级)
????????javax.swing:纯Java编写(轻量级)
????(3)主要方法:
????????A:javax.swing包下的方法????
?
????????????构造方法:
????????????public JFrame(String title):创建对象的同时,设置标题名称
????????????public JFrame():无参构造
?
????????????普通方法:
????????????public
void setTitle(String title):设置窗体的标题
????????????public
void setSize(int
width, int
height):设置窗体的大小
public
void setSize(Dimension d):通过传入Dimension对象设置窗体大小
????????????public Dimension(int
width, int
height):Dimension类的构造方法
????????????public
void setLocation(int
x, int
y):设置窗体的坐标
public
void setLocation(Point p):通过传入一个Point对象,来设置窗体坐标
public Point(int
x, int
y):Point类的构造方法
????????????public
void setVisible(boolean
b):设置让窗体可见
?
????????????用一个方法完成窗体大小和坐标的设置
????????????public
void setBounds(int
x, int
y, int
width, int
height):
????????????参数x、y设置窗体坐标。Width、height设置窗体大小
?
????????????public
void setLayout(LayoutManager mgr):设置窗体的布局模式
????????????????????????常用的为流式布局:FlowLayout
?
????????B:监听机制的应用
????????????案例:关闭窗体
????????????
import java.awt.Frame; import java.awt.event.WindowAdapter; import java.awt.event.WindowEvent; ? public?class FrameDemo { ????public?static?void main(String[] args) { ????????// 创建窗体对象 ????????JFrame f = new JFrame("窗体关闭案例"); ? ????????// 设置窗体属性 ????????f.setBounds(400, 200, 400, 300); ? ????????//用适配器类改进 ????????f.addWindowListener(new?WindowAdapter() { ????????????@Override ????????????public?void?windowClosing(WindowEvent e) { ????????????????System.exit(0); ????????????} ????????}); ? ????????// 设置窗体可见 ????????f.setVisible(true); ????} } |
?
????(4)GUI的继承体系
????????组件:组件就是对象
????????????容器组件:是可以存储基本组件和容器组件的组件。
????????????基本组件:是可以使用的组件,但是必须依赖容器。
????(5)事件监听机制(理解)
????????A:事件源
????????B:事件
????????C:事件处理
????????D:事件监听
????(6)适配器模式(理解)
????????应用场景:当一个接口中有蛮多方法的时候,而我们只需要用其中某一个方法,但是呢?
????????????????用其中的一个方法,就要实现该接口中的所有方法,有点不划算,所以呢?
????????????????适配器模式就应运而生啦
????????A:接口
????????????????定义一个顶层的接口
????????B:抽象适配器类
????????????????用一个抽象类实现该接口,当然是空实现(不提供具体的方法体)
????????C:实现类
????????????用具体的类继承抽象适配器类,想用哪个方法,就重写哪个方法,不用重写所有方法
????????案例解释
????????????1. UserDao(顶层接口)
/* * 顶层接口:针对用户操作的四种功能 */ public?interface UserDao { ????public?abstract?void add(); ? ????public?abstract?void delete(); ? ????public?abstract?void update(); ? ????public?abstract?void find(); } |
?
????????????2. UserAdapter(适配器类,实现顶层接口)
/* * 定义一个类实现UserDao接口:当然啦,是空实现 */ public?abstract?class UserAdapter implements UserDao { ? ????@Override ????public?void add() { ????} ? ????@Override ????public?void delete() { ????} ? ????@Override ????public?void update() { ????} ? ????@Override ????public?void find() { ????} } |
?
????????????3. UserDaoImpl(具体实现类,需继承UserAdapter适配器类)
/* * 具体实现类:继承UserAdapter适配器类,重写里面 * ????????????自己想要使用的方法 */ public?class UserDaoImpl extends UserAdapter { ????@Override ????public?void add() { ????????System.out.println("添加功能"); ????} } |
?
????????????4. UserDaoDemo(测试类)
/* * 问题: * ????????接口(方法比较多) -- 实现类(仅仅使用一个,也得把其他的实现给提供了,哪怕是空实现) * ????????太麻烦了。 * 解决方案: * ????????接口(方法比较多) -- 适配器类(实现接口,仅仅空实现) -- 实现类(用哪个重写哪个) */ public?class UserDaoDemo { ????public?static?void main(String[] args) { ????????UserDao ud = new UserDaoImpl(); ????????ud.add(); ????????// 我没有说我们需要四种功能都实现啊。 ????????UserDao ud2 = new UserDaoImpl2(); ????????ud2.add(); ????} } |
?
????
????(7)案例:
????????A:创建窗体案例
import javax.swing.JFrame; /* * 需求:创建一个窗体(该窗体默认情况下是不能关闭的) */ public?class FrameDemo { ????public?static?void main(String[] args) { ????????// 创建窗体对象 ????????// Frame f = new Frame(); ????????// Frame(String title) ????????JFrame f = new JFrame("林青霞"); ? ????????// 设置窗体标题 ????????f.setTitle("HelloWorld"); ????????// 设置窗体大小 ????????f.setSize(400, 300); // 单位:像素 ????????// 设置窗体位置 ????????f.setLocation(400, 200); ? ????????// 调用一个方法,设置让窗体可见 ????????// f.show(); ????????f.setVisible(true); ? ????????// System.out.println("helloworld"); ????} } |
?
????????B:窗体关闭案例
????????
import java.awt.event.WindowAdapter; import java.awt.event.WindowEvent; import javax.swing.JFrame; /* * 需求:创建一个窗体(该窗体可以直接被关闭的) */ public?class FrameDemo { ????public?static?void main(String[] args) { ????????// 创建窗体对象 ????????JFrame f = new JFrame("窗体关闭案例"); ? ????????// 设置窗体属性 ????????f.setBounds(400, 200, 400, 300); ???????? ????????//用适配器类改进,为窗体添加监听机制 ????????f.addWindowListener(new WindowAdapter() { ????????????@Override ????????????public?void windowClosing(WindowEvent e) { ????????????????System.exit(0); ????????????} ????????}); ? ????????// 设置窗体可见 ????????f.setVisible(true); ????} } |
?
????????C:窗体添加按钮并对按钮添加事件案例。
????????????界面中的组件布局。
import java.awt.Button; import java.awt.FlowLayout; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.event.WindowAdapter; import java.awt.event.WindowEvent; ? import?javax.swing.JFrame; ? /* * 需求:把按钮添加到窗体,并对按钮添加一个点击事件。 * A:创建窗体对象 * B:创建按钮对象 * C:把按钮添加到窗体 * D:窗体显示 */ public?class FrameDemo { ????public?static?void main(String[] args) { ????????// 创建窗体对象 ????????JFrame?f = new?JFrame("添加按钮"); ????????// 设置属性 ????????f.setBounds(400, 200, 400, 300); ????????// 设置布局为流式布局 ????????f.setLayout(new FlowLayout()); ? ????????// 创建按钮对象 ????????Button bu = new Button("点我啊"); ????????// bu.setSize(20, 10); ? ????????// 把按钮添加到窗体 ????????f.add(bu); ? ????????// 设置窗体可以关闭 ????????f.addWindowListener(new WindowAdapter() { ????????????@Override ????????????public?void windowClosing(WindowEvent e) { ????????????????System.exit(0); ????????????} ????????}); ? ????????bu.addActionListener(new ActionListener() { ????????????@Override ????????????public?void actionPerformed(ActionEvent e) { ????????????????System.out.println("你再点试试"); ????????????} ????????}); ? ????????// 窗体显示 ????????f.setVisible(true); ????} } |
?
????????D:把文本框里面的数据转移到文本域
import java.awt.Button; import java.awt.FlowLayout; import java.awt.TextArea; import java.awt.TextField; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.event.WindowAdapter; import java.awt.event.WindowEvent; ? import javax.swing.JFrame; /* * 需求:将文本框中的数据转移到文本域中 */ public?class FrameDemo { ????public?static?void main(String[] args) { ????????// 创建窗体对象 ????????JFrame f = new JFrame("数据转移"); ????????// 设置窗体属性和布局 ????????f.setBounds(400, 200, 400, 300); ????????f.setLayout(new FlowLayout()); ? ????????// 创建文本框 ????????final TextField tf = new TextField(20); ????????// 创建按钮 ????????Button bu = new Button("数据转移"); ????????// 创建文本域 ????????final TextArea ta = new TextArea(10, 40); ? ????????// 把组件添加到窗体 ????????f.add(tf); ????????f.add(bu); ????????f.add(ta); ? ????????// 设置窗体关闭 ????????f.addWindowListener(new WindowAdapter() { ????????????@Override ????????????public?void windowClosing(WindowEvent e) { ????????????????System.exit(0); ????????????} ????????}); ? ????????// 对按钮添加事件 ????????bu.addActionListener(new ActionListener() { ????????????@Override ????????????public?void actionPerformed(ActionEvent e) { ????????????????// 获取文本框的值 ????????????????String tf_str = tf.getText().trim(); ????????????????// 清空数据 ????????????????tf.setText(""); ? ????????????????// 设置给文本域 ????????????????// ta.setText(tf_str); ????????????????// 追加和换行 ????????????????ta.append(tf_str + "\r\n"); ???????????????? ????????????????//获取光标 ????????????????tf.requestFocus(); ????????????} ????????}); ? ????????// 设置窗体显示 ????????f.setVisible(true); ????} } |
?
????????E:更改背景色
import java.awt.Button; import java.awt.Color; import java.awt.FlowLayout; import?java.awt.Frame; import java.awt.event.MouseAdapter; import java.awt.event.MouseEvent; import java.awt.event.WindowAdapter; import java.awt.event.WindowEvent; /* * 需求:更改背景色 */ public?class FrameDemo { ????public?static?void main(String[] args) { ????????// 创建窗体对象,局部内部类访问局部变量,局部变量必须用final修饰 ????????final?Frame?f = new?Frame("更改背景色"); ????????// 设置窗体属性和布局 ????????f.setBounds(400, 200, 400, 300); ????????f.setLayout(new FlowLayout()); ? ????????// 创建四个按钮 ????????Button redButton = new Button("红色"); ????????Button greenButton = new Button("绿色"); ????????Button buleButton = new Button("蓝色"); ? ????????// 添加按钮 ????????f.add(redButton); ????????f.add(greenButton); ????????f.add(buleButton); ? ????????// 设置窗体关闭 ????????f.addWindowListener(new WindowAdapter() { ????????????@Override ????????????public?void windowClosing(WindowEvent e) { ????????????????System.exit(0); ????????????} ????????}); ? ????????// 对按钮添加动作事件 ????????// redButton.addActionListener(new ActionListener() { ????????// @Override ????????// public void actionPerformed(ActionEvent e) { ????????// f.setBackground(Color.RED); ????????// } ????????// }); ? ????????// 对按钮添加鼠标点击事件 ????????// redButton.addMouseListener(new MouseAdapter() { ????????// @Override ????????// public void mouseClicked(MouseEvent e) { ????????// f.setBackground(Color.RED); ????????// } ????????// }); ? ????????// 对按钮添加鼠标的进入事件 ????????redButton.addMouseListener(new MouseAdapter() { ????????????@Override ????????????public?void mouseEntered(MouseEvent e) { ????????????????f.setBackground(Color.RED); ????????????} ????????}); ????????// 对按钮添加鼠标的离开事件 ????????redButton.addMouseListener(new MouseAdapter() { ????????????@Override ????????????public?void mouseExited(MouseEvent e) { ????????????????f.setBackground(Color.WHITE); ????????????} ????????}); ? ????????greenButton.addMouseListener(new MouseAdapter() { ????????????@Override ????????????public?void mouseEntered(MouseEvent e) { ????????????????f.setBackground(Color.GREEN); ????????????} ????????}); ? ????????greenButton.addMouseListener(new MouseAdapter() { ????????????@Override ????????????public?void mouseExited(MouseEvent e) { ????????????????f.setBackground(Color.WHITE); ????????????} ????????}); ? ????????buleButton.addMouseListener(new MouseAdapter() { ????????????@Override ????????????public?void mouseEntered(MouseEvent e) { ????????????????f.setBackground(Color.BLUE); ????????????} ????????}); ? ????????buleButton.addMouseListener(new MouseAdapter() { ????????????@Override ????????????public?void mouseExited(MouseEvent e) { ????????????????f.setBackground(Color.WHITE); ????????????} ????????}); ? ????????// 设置窗体显示 ????????f.setVisible(true); ????} } |
?
????????F:设置文本框里面不能输入非数字字符
import java.awt.FlowLayout; import?java.awt.Frame; import java.awt.Label; import java.awt.TextField; import java.awt.event.KeyAdapter; import java.awt.event.KeyEvent; import java.awt.event.WindowAdapter; import java.awt.event.WindowEvent; ? /* * 需求:你输入的如果是非数字字符,就取消你键盘录入的效果。 */ public?class FrameDemo { ????public?static?void main(String[] args) { ????????// 创建窗体对象并设置属性 ????????Frame?f = new?Frame("不能输入非数字字符"); ????????f.setBounds(400, 200, 400, 300); ????????f.setLayout(new FlowLayout()); ? ????????// 创建Label标签对象 ????????Label label = new Label("请输入你的QQ号码,不能是非数字,不信你试试"); ????????TextField tf = new TextField(40); ? ????????// 添加到窗体上 ????????f.add(label); ????????f.add(tf); ? ????????// 设置窗体关闭 ????????f.addWindowListener(new WindowAdapter() { ????????????@Override ????????????public?void windowClosing(WindowEvent e) { ????????????????System.exit(0); ????????????} ????????}); ? ????????// 给文本框添加事件 ????????tf.addKeyListener(new KeyAdapter() { ????????????@Override ????????????public?void keyPressed(KeyEvent e) { ????????????????// 如果你取得的字符不是数字字符就取消事件 ????????????????// 思路:先获取字符,判断字符,取消事件 ????????????????// char getKeyChar() ????????????????char?ch = e.getKeyChar(); ????????????????// System.out.println(ch); ????????????????if (!(ch >= ‘0‘ && ch <= ‘9‘)) { ????????????????????e.consume(); ????????????????} ????????????} ????????}); ? ????????// 设置窗体可见 ????????f.setVisible(true); ????} } |
?
????????G:一级菜单
import java.awt.FlowLayout; import java.awt.Frame; import java.awt.Menu; import java.awt.MenuBar; import java.awt.MenuItem; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.event.WindowAdapter; import java.awt.event.WindowEvent; ? /* * 需求:制作一级菜单 */ public?class FrameDemo { ????public?static?void main(String[] args) { ????????// 创建窗体对象并设置属性 ????????Frame f = new Frame("一级菜单"); ????????f.setBounds(400, 200, 400, 300); ????????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); ? ????????// 设置窗体关闭 ????????f.addWindowListener(new WindowAdapter() { ????????????@Override ????????????public?void windowClosing(WindowEvent e) { ????????????????System.exit(0); ????????????} ????????}); ? ????????mi.addActionListener(new ActionListener() { ????????????@Override ????????????public?void actionPerformed(ActionEvent e) { ????????????????System.exit(0); ????????????} ????????}); ? ????????// 设置窗体可见 ????????f.setVisible(true); ????} } |
?
????????H:多级菜单
import java.awt.FlowLayout; import java.awt.Frame; import java.awt.Menu; import java.awt.MenuBar; import java.awt.MenuItem; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.event.WindowAdapter; import java.awt.event.WindowEvent; import java.io.IOException; ? /* * 需求:制作多级菜单 */ public?class FrameDemo { ????public?static?void main(String[] args) { ????????// 创建窗体对象并设置属性 ????????final Frame f = new Frame("多级菜单"); ????????f.setBounds(400, 200, 400, 300); ????????f.setLayout(new FlowLayout()); ???????? ????????final String name = f.getTitle(); ? ????????// 创建菜单栏 ????????MenuBar mb = new MenuBar(); ????????// 创建菜单 ????????Menu m1 = new Menu("文件"); ????????Menu m2 = new Menu("更改名称"); ????????// 创建菜单项 ????????final MenuItem mi1 = new MenuItem("好好学习"); ????????final 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); ???????? ????????mb.add(m1); ? ????????// 设置菜单栏 ????????f.setMenuBar(mb); ? ????????// 设置窗体关闭 ????????f.addWindowListener(new WindowAdapter() { ????????????@Override ????????????public?void windowClosing(WindowEvent e) { ????????????????System.exit(0); ????????????} ????????}); ???????? ????????mi1.addActionListener(new ActionListener() { ????????????@Override ????????????public?void actionPerformed(ActionEvent e) { ????????????????f.setTitle(mi1.getLabel()); ????????????} ????????}); ???????? ????????mi2.addActionListener(new ActionListener() { ????????????@Override ????????????public?void actionPerformed(ActionEvent e) { ????????????????f.setTitle(mi2.getLabel()); ????????????} ????????}); ???????? ????????mi3.addActionListener(new ActionListener() { ????????????@Override ????????????public?void actionPerformed(ActionEvent e) { ????????????????f.setTitle(name); ????????????} ????????}); ???????? ????????mi4.addActionListener(new ActionListener() { ????????????@Override ????????????public?void actionPerformed(ActionEvent e) { ????????????????Runtime r = Runtime.getRuntime(); ????????????????try { ????????????????????r.exec("notepad"); ????????????????} catch (IOException e1) { ????????????????????e1.printStackTrace(); ????????????????} ????????????} ????????}); ? ????????mi5.addActionListener(new ActionListener() { ????????????@Override ????????????public?void actionPerformed(ActionEvent e) { ????????????????System.exit(0); ????????????} ????????}); ? ????????// 设置窗体可见 ????????f.setVisible(true); ????} } |
?
????(8)Netbeans的概述和使用
????????A:是可以做Java开发的另一个IDE工具,做GUI界面开发特别方便。
????????B:使用
????????????A:四则运算
????????????????a:修改图标????????b:设置皮肤
????????????????c:设置居中????????d:数据校验
????????????B:登录注册
?