------- android培训、java培训、期待与您交流! ----------
主要内容:一、图形用户界面;二、布局管理器;三、事件监听机制;四、jar包双击执行
一、图形用户界面:GUI概述
GUI:
Graphical User Interface(图形用户接口).
用图形的方式,来显示计算机操作的界面,这样更方便更直观。
CLI:
Command Line Interface(命令行用户接口)
就是常见的多Dos命令操作。
需要记忆一些常用的命令,操作不直观。
Java为GUI提供的对象都存在java.Awt和javax.Swing两个包中。
Awt与Swing
java.Awt:Abstract Window ToolKit(抽象窗口工具包),需要调用本地系统方法实现功能。属于重量级控件。
理论上说:结果程序可以运行在任何平台上,但是观感的效果却依赖于目标平台。
javax.Swing:在Awt的基础上,建立的一套图形界面系统,其中提供了更多的组件,而且完全由Java实现。增强了移植性,属轻量级控件。
插入图
Component:组件;
|--Button:按钮
|--Label: 标签
|--Checkbox:复选框
|--TextComponent:文本组件
|--TextArea:文本区域
|--TextField:文本框
|--Container:容器,特殊组件
|--Window:窗口
|--Frame:框架
|--Dialog:对话框
|--FileDialog:文件对话框
|--Panel:面板
二、布局管理器
容器中的组件的排放方式,就是布局。
常见的布局管理器:
1,FlowLayout(流式布局管理器)
从左到右的顺序排列。
Panel默认的布局管理器。
2,BorderLayout(边界布局管理器)
东、南、西、北、中
Frame默认的布局管理器。
3,GridLayout(网格布局管理器)
规则的矩阵
4,CardLayout(卡片布局管理器)
选项卡
5,GridBagLayout(网格包布局管理器)
非规则的矩阵
三、事件监听机制
事件监听机制的特点:
1,事件源。
2,事件。
3,监听器。
4,事件处理。
事件源:就是awt包或者swing包中的那些图形界面组件。
事件:每一个事件源都是自己特有的对应事件和共性事件。
监听器:将可以触发某一个事件的动作(不只一个动作)都已经封装到了监听器中。
想要知道哪个组建具备什么样的特有监听器,需要查看该组件对象的功能。
以上三者,在java中都已经定义好了。
直接获取其对象来用就可以了。
我们要做的事情是,就是对产生的动作进行处理。
addWindowListener();窗体监听器,有适配器:WindowAdapter
addActionListener();动作监听器,无适配器。
addKeyListener(); 键盘监听 有适配器:KeyAdapter
addMouseListener(); 鼠标监听 有适配器:MouseAdapter
窗体监听和按钮监听例:
import java.awt.*;
import java.awt.event.*;
class FrameDemo
{
//定义该图形中所需的组件的引用。
private Frame f;
private Button but;
FrameDemo()
{
init();
}
public void init()
{
f = new Frame("my frame");
//对Frame进行基本设置
f.setBounds(300,100,600,500);
f.setLayout(new FlowLayout());
but = new Button("my button");
//将组件添加到Frame中
f.add(but);
//加载一下窗体上事件
myEvent();
//显示窗体
f.setVisible(true);
}
private void myEvent()
{
f.addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{
System.exit(0);
}
});
//上按钮具备退出程序的功能
/*
按钮就是事件源。
那么选择哪个监听器呢?
通过关闭窗体示例了解到,想要知道那个组件具备什么样的特有监听器。
需要查看该组件对象的功能。
通过查阅Button的描述,发现按钮支持一个特有监听addActionListener
*/
but.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
System.exit(0);
}
});
}
public static void main(String[] args)
{
new FrameDemo();
}
}
键盘监听和鼠标监听 例:
import java.awt.*;
import java.awt.event.*;
class MouseAndKeyEvent
{
private Frame f;
private Button but;
private TextField tf;
MouseAndKeyEvent()
{
init();
}
public void init()
{
f = new Frame("my frame");
f.setBounds(200,100,600,500);
f.setLayout(new FlowLayout());
but = new Button("my button");
tf = new TextField(20);
f.add(tf);
f.add(but);
myEvent();
f.setVisible(true);
}
private void myEvent()
{
f.addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{
System.exit(0);
}
});
tf.addKeyListener(new KeyAdapter()
{
public void keyPressed(KeyEvent e)
{
int code = e.getKeyCode();
if(!(code>=KeyEvent.VK_0 && code<=KeyEvent.VK_9))
{
e.consume();
System.out.println(code+"是非法的");
}
}
});
but.addKeyListener(new KeyAdapter()
{
public void keyPressed(KeyEvent e)
{
if(e.isControlDown()&&e.getKeyCode()==KeyEvent.VK_ENTER)
System.out.println("Ctrl+Enter");
//System.out.println(KeyEvent.getKeyText(e.getKeyCode())+"...."+e.getKeyCode());
}
});
but.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
System.out.println("Action 事件");
}
});
but.addMouseListener(new MouseAdapter()
{
private int count=1;
private int clickCount = 1;
public void mouseEntered(MouseEvent e)
{
System.out.println("鼠标进入事件"+count++);
}
public void mouseClicked(MouseEvent e)
{
if(e.getClickCount()==2)
System.out.println("鼠标双击动作"+clickCount++);
}
});
}
public static void main(String[] args)
{
new MouseAndKeyEvent();
}
}
练习:
在文本框中输入目录,点击“转到”按钮,将该目录中的文件
与文件夹名称列在下面的文本区域中。
例:
import java.awt.*;
import java.awt.event.*;
import java.io.*;
class MyWindowDemo
{
private Frame f;
private Button but;
private TextField tf;
private TextArea ta;
private Dialog d;
private Label lab;
private Button okBut;
MyWindowDemo()
{
init();
}
public void init()
{
f = new Frame("my window");
f.setBounds(100,100,600,500);
f.setLayout(new FlowLayout());
tf = new TextField(65);
but = new Button("转到");
ta = new TextArea(25,70);
d = new Dialog(f,"提示信息-self",true);
d.setBounds(300,200,300,100);
d.setLayout(new FlowLayout());
lab = new Label();
okBut = new Button("确定");
d.add(lab);
d.add(okBut);
f.add(tf);
f.add(but);
f.add(ta);
myEvent();
f.setVisible(true);
}
public void myEvent()
{
d.addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{
d.setVisible(false);
}
});
okBut.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
d.setVisible(false);
}
});
tf.addKeyListener(new KeyAdapter()
{
public void keyPressed(KeyEvent e)
{
if(e.getKeyCode()==KeyEvent.VK_ENTER)
showDir();
}
});
but.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
showDir();
}
});
f.addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{
System.exit(0);
}
});
}
private void showDir()
{
String dirPath = tf.getText();
File dir = new File(dirPath);
if(dir.exists() && dir.isDirectory())//判断dir存在,并且是目录
{
ta.setText("");
String[] names = dir.list();
for(String name : names)
{
ta.append(name+"\r\n");
}
}
else
{
String info = "找不到:"+dirPath+"。请检查拼写并重试";
lab.setText(info);
d.setVisible(true);
}
}
public static void main(String[] args)
{
new MyWindowDemo();
}
}
练习:做一个记事本小程序:能打开,保存,退出
例:
import java.awt.*;
import java.awt.event.*;
import java.io.*;
class MyMenuTest
{
private Frame f;
private MenuBar bar;
private TextArea ta;
private Menu fileMenu,bianjiMenu;
private MenuItem openItem,saveItem,closeItem;
private FileDialog openDia,saveDia;
private File file;
MyMenuTest()
{
init();
}
public void init()
{
f = new Frame("my window");
f.setBounds(200,100,600,500);
//f.setLayout(new FlowLayout());
bar = new MenuBar();
ta = new TextArea();
fileMenu = new Menu("文件");
bianjiMenu = new Menu("编辑");
openItem = new MenuItem("打开");
saveItem = new MenuItem("保存");
closeItem = new MenuItem("退出");
fileMenu.add(openItem);
fileMenu.add(saveItem);
fileMenu.add(closeItem);
bar.add(fileMenu);
bar.add(bianjiMenu);
f.setMenuBar(bar);
f.add(ta);
openDia = new FileDialog(f,"我要打开",FileDialog.LOAD);//模式默认页是加载文件。
saveDia = new FileDialog(f,"我要保存",FileDialog.SAVE);
myEvent();
f.setVisible(true);
}
public void myEvent()
{
saveItem.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
if(file==null)
{
saveDia.setVisible(true);
String dirPath = saveDia.getDirectory();
String fileName = saveDia.getFile();
if(dirPath==null||fileName==null)
return;
file = new File(dirPath,fileName);
}
try
{
BufferedWriter bufw = new BufferedWriter(new FileWriter(file));
String text = ta.getText();
bufw.write(text);
//bufw.flush();
bufw.close();
}
catch (IOException ex)
{
throw new RuntimeException("写入失败");
}
}
});
openItem.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
openDia.setVisible(true);
String dirPath = openDia.getDirectory();
String fileName = openDia.getFile();
//System.out.println(dirPath+"..."+fileName);
if(dirPath==null||fileName==null)
return;
ta.setText("");
file = new File(dirPath,fileName);
try
{
BufferedReader bufr = new BufferedReader(new FileReader(file));
String line = null;
while((line=bufr.readLine())!=null)
{
ta.append(line+"\r\n");
}
bufr.close();
}
catch (IOException ex)
{
throw new RuntimeException("读取失败");
}
}
});
closeItem.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
System.exit(0);
}
});
f.addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{
System.exit(0);
}
});
}
public static void main(String[] args)
{
new MyMenuTest();
}
}
四、jar包双击执行
编辑格式:jar -cvfm 目的文件名.jar 配置文件 源文件夹
例:jar -cvfm my.jar 1.txt mymenu
需要配置文件:
格式:Main-Class:<此处必须有空格>主函数类名<此处必须有回车>【注意:以上为固定格式】
例:Main-Class: mymenu.MyMenuTest
如何制作可以双击执行的jar包呢?
1,将多个类封装到了一个包(package)中。
2,定义一个jar包的配置信息。
定义一个文件a.txt 。文件内容内容为:
Main-Class:(空格)包名.类名(回车)
3,打jar包。
jar -cvfm my.jar a.txt 包名
4,通过winrar程序进行验证,查看该jar的配置文件中是否有自定义的配置信息。
5,通过工具--文件夹选项--文件类型--jar类型文件,通过高级,定义该jar类型文件的打开动作的关联程序。
jdk\bin\javaw.exe -jar