GUI:
java为GUI提供的对象都存在java.awt和javax.swing中
java.Awt:Abstract Window ToolKit (抽象窗口工具包),需要调用本地系统方法实现功能。属重量级控件。
javax.Swing:在AWT的基础上,建立的一套图形界面系统,其中提供了更 多的组件,而且完全 由Java实现。增强了移植性,属轻量级控件。
布局:
FlowLayout(流式布局管理器)从左到右的顺序排列。
BorderLayout(边界布局管理器)东,南,西,北,中 。 默认
GridLayout(网格布局管理器)规则的矩阵
CardLayout(卡片布局管理器)选项卡
GridBagLayout(网格包布局管理器)非规则的矩阵
先创建一个Frame f = new Frame("标题");setVisible(true)才可见;每创建一个另外的 按钮等等都必须添加到f中 f.add(组件);
事件监听机制的特点:
1,事件源。2,事件。
3,监听器。
4,事件处理。
WindowListener:
这个接口有7个方法,复写很麻烦,子类WindowAdapter已经实现了WindowListener接口。
并覆盖了其中的所有方法。那么只要继承自Windowadapter覆盖所需要的方法即可。
button的ActionListenner是没有适配器的。
f.addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e)
{
System.out.println("关闭");
System.exit(0);
}
});
添加鼠标事件
b.addMouseListener(new MouseAdapter(){
int i=1;
int j=1;
int k=1;
public void mouseEntered(MouseEvent e)
{
System.out.println("jinru"+i++);
}
public void mouseClicked(MouseEvent e)
{
if (e.getClickCount()==2) {
System.out.println("shaungji"+k++);
}
System.out.println("danji"+j++);
}
});
添加键盘事件
tf.addKeyListener(new KeyAdapter(){
public void keyPressed(KeyEvent e)
{
if (e.getKeyCode()<KeyEvent.VK_0||e.getKeyCode()>KeyEvent.VK_9) {
e.consume();
}
System.out.println(e.getKeyText(e.getKeyCode()));
}
});
菜单事件
MenuBar,Menu,MenuItem先创建菜单条,再创建菜单,每一个菜单 中建立菜单项,也可以菜单添加到菜单中,作为子菜单。
通过setMenuBar()方法,将菜单添加到Frame中。
package MyMenu;
import java.awt.*;
import java.awt.event.*;
import java.io.*;
class MyMenuDemo {
private Frame f;
private Button b;
private TextField tf;
private TextArea ta;
private Dialog d;
private Label lab;
private Button okb;
private Menu m;
private MenuBar mb;
private MenuItem openFile,closeFile,saveFile;
private FileDialog openDia,saveDia;
private File file;
MyMenuDemo()
{
init();
}
public void init()
{
f = new Frame();
b = new Button("我去");
okb=new Button("确定");
lab=new Label();
tf = new TextField(50);
ta = new TextArea(20,80);
d= new Dialog(f,"提示信息",true);
mb = new MenuBar();
m = new Menu("文件");
openFile = new MenuItem("打开");
closeFile = new MenuItem("关闭");
saveFile = new MenuItem("保存");
d.setLayout(new FlowLayout());
d.add(lab);
d.add(okb);
d.setBounds(400,300,250,100);
f.setBounds(300,200,600,500);
f.setLayout(new FlowLayout());
f.add(b);
f.add(tf);
f.add(ta);
f.setMenuBar(mb);
mb.add(m);
m.add(openFile);
m.add(closeFile);
m.add(saveFile);
f.setVisible(true);
openDia = new FileDialog(f,"打开文件",FileDialog.LOAD);
saveDia = new FileDialog(f,"打开文件",FileDialog.SAVE);
event();
}
public void event()
{
f.addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e)
{
System.exit(0);
}
});
openFile.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e)
{
openDia.setVisible(true);
String dirPath=openDia.getDirectory();
String fileName = openDia.getFile();
System.out.println(openDia.getDirectory()+"......"+openDia.getFile());
if (dirPath=="null"||fileName=="") {
return ;
}
try{
file = new File (dirPath,fileName);
tf.setText(dirPath+fileName);
BufferedReader bufr = new BufferedReader(new FileReader(file));
String line = null;
while ((line = bufr.readLine())!=null) {
ta.append(line+"\r\n");
}
}catch(IOException ex){
throw new RuntimeException();
}
}
});
saveFile.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e)
{
try{
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);
}else{
BufferedWriter bufw = new BufferedWriter(new FileWriter(file));
String s = ta.getText();
bufw.write(s);
bufw.close();
}
}catch(IOException ex){
throw new RuntimeException();
}
}
});
}
public void function()
{
ta.setText("");
String dirPath = tf.getText();
file = new File(dirPath);
if (file.exists() && file.isDirectory()) {
for (String name : file.list() ) {
ta.append(name+"\r\n");
}
}else{
lab.setText("目录:"+dirPath+"不存在");
d.setVisible(true);
}
}
public static void main(String[] args) {
new MyMenuDemo();
}
}
制作可以双击执行的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
6,双击执行。