您的位置:首页 > 职场人生

黑马程序员——Java基础--GUI图形化界面

2015-07-07 00:46 555 查看
------Java培训、Android培训、iOS培训、.Net培训、期待与您交流! -------

GUI图像化界面

一、概述
  GUI
Graphical User Interface(图形用户接口)
CLI
Command line User Interface(命令行用户接口)】
就是常用的DOS命令行操作。
需要记忆一些常用的有命令,操作不直观。
举例:
比如:创建文件夹、或删除文件夹等
Java给GUI提供的对象都存在java.Awt和javax.Swing两个包中Awt和Swing
java.Awt:Abstract Window ToolKit(抽象窗口工具包),需要调用本地系统方法实现功能,属于重量级控件
javax.Swing:在AWT的基础上,建立的一套图形界面系统,其中提供了共多的组件,而且完全有JAVA实现,增强了移植性。    属于轻量级组件。
继承关系
Component
|--Button按钮
|--Label标签
|--Checkbox复选框
|--TextComponet
|--TextArea文本区域
|--Textfield文本框
|--Container容器(可以添加组件)
|--Window窗口
|--Frame框架
|--Dialog对话框
|--FileDialog文件对话框
|--Panel面板
布局管理器
容器中的组件的排放方式,就是布局
常见的布局管理器:
FlowLayout(流式布局管理器)
从左到右的顺序排列。
Panel默认的布局管理器
BorderLayout(边界布局管理器)
东,南,西,北,中
Frame默认的布局管理器
GridLayout(网格布局管理器)
规则的矩阵
GardLayout(卡片布局管理器)
选项卡
GridBagLayout(网格包布局管理器)
非规则的矩阵
二、应用
1、创建图形化界面
Frame 是带有标题和边框的顶层窗口。
创建图形化界面:
a.创建frame窗体。
b.对窗体进行基本设置。比如大小。位置。布局
c.定义组建。
d.将组建通过窗体的add添加到窗体中。
e.让窗体显示,通过setVisible(true).
事件监听机制的特点:
a.事件源
b.事件
c.监听器
d.事件处理
事件源:就是awt包或者swing包中的那些图形界面组件。
事件:每一个事件源都有自己特有的对应事件和共性事件。
监听器:将可以触发某一个事件的动作(不止一个动作)都已经封装到了监听器中。
以上三者,在java中都已经定义好了。直接获取其对象来用就可以了。
我们要做的事情是,就是对产生的动作进行处理。
import java.awt.*;
import java.awt.event.*;

class AwtDemo
{
public static void main(String[] args)
{
//创建一个窗体
Frame f = new Frame("my awt");

f.setSize(500,400);
//位置设置
f.setLocation(300,200);
//布局设置
f.setLayout(new FlowLayout());

Button b = new Button("按钮");

f.add(b);

f.addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{
System.out.println("w我关");
System.exit(0);
}

public void windowActivated(WindowEvent e)//窗口前置
{
System.out.println("我活了");
}

public void windowOpened(WindowEvent e)
{
System.out.println("我被打开了,哈哈啊");
}
});

//显示隐藏组建
f.setVisible(true);

//System.out.println("Hello World!");
}
}
/*
class MyLis implements WindowListener
{
//覆盖7个方法。可是我只用到了关闭的动作。
//其他动作都没有用到,可是却必须复写。

}
*/

//以为WindowListener的子类WindowAdap已经实现了WindowListener接口。
//并覆盖了其中的所有方法,那么我们只要继承自WindowAdap覆盖我所需要的方法即可。

class MyWin extends WindowAdapter
{
public void windowClosing(WindowEvent e)
{
System.out.println("window closing....."+e.toString());

System.exit(0);
}
}
按钮添加监听器
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进行基本设置
//setSize(300,400)/setLoaction(500,400)
f.setBounds(300,400,500,400);
f.setLayout(new FlowLayout());

but = new Button("my button");

//将组建添加到frame中。

f.add(but);

//加载一下窗体上事件。
myEvent();

//显示窗体;
f.setVisible(true);
}

public 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.out.println("按钮干的");
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");

//对frame进行基本设置

f.setBounds(300,400,500,400);
f.setLayout(new FlowLayout());

tf = new TextField(10);

but = new Button("my button");

//将组建添加到frame中。

f.add(tf);
f.add(but);

//加载一下窗体上事件。
myEvent();

//显示窗体;
f.setVisible(true);
}

public void myEvent()
{
f.addW
4000
indowListener(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))
{
System.out.println(code+"..."+"非法");
//使用此事件,以便不会按照默认的方式由产生此事件的源代码来处理此事件。
e.consume();

}

}
});

but.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
System.out.println("action ok");
}
});
//在按钮上添加鼠标监听器
but.addMouseListener(new MouseAdapter()
{
public void mouseEntered(MouseEvent e)
{
System.out.println("s鼠标进入");

}
public void mouseClicked(MouseEvent e)
{
//返回与此事件关联的鼠标单击次数。
if(e.getClickCount()==2)
System.out.println("s鼠标双击");
}

});

//给but添加键盘事件监听
but.addKeyListener(new KeyAdapter()
{
public void keyPressed(KeyEvent e)
{
if(e.isControlDown()&&e.getKeyCode()==KeyEvent.VK_ENTER)
//System.exit(0);

System.out.println("是否为control组合键");

// 返回描述 keyCode 的 String,如 "HOME"、"F1" 或 "A"。
//System.out.println(KeyEvent.getKeyText(e.getKeyCode())+""+e.getKeyCode());

}
});

}

public static void main(String[] args)
{
new MouseAndKeyEvent();
}

}
创建文本框,文本区域,并显示输入路径下的文件名称
import java.awt.*;
import java.awt.event.*;
import java.io.*;

class MyWindowDemo
{

private Frame f;
private TextField tf;
private Button but;
private TextArea ta;

private Dialog d;//对话框
private Label lab;//可在容器中放置文本的组件
private Button okbut;

MyWindowDemo()
{
init();
}

public void init()
{
f = new Frame("my frame");

f.setBounds(300,200,500,400);

f.setLayout(new FlowLayout());

tf = new TextField(30);

but = new Button("转到");

ta = new TextArea(10,40);

d = new Dialog(f,"提示信息-self",true);

d.setBounds(300,200,200,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()
{
okbut.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
d.setVisible(false);
}
});

d.addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{
d.setVisible(false);
}

});

f.addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{
System.exit(0);
}

});

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();
}
});
}
private void showDir()
{
String dirPath = tf.getText();

//System.out.println(text);

//ta.setText(text);

File dir = new File(dirPath);
if(dir.exists() && dir.isDirectory())
{

ta.setText("");
String[] str = dir.list();

for(String name : str)
{
ta.append(name+"\r\n");

}
}
else
{
String info = "输入信息:"+dirPath+"是错误的,请重新输入";
lab.setText(info);
d.setVisible(true);

}
tf.setText("");
}

public static void main(String[] args)
{
new MyWindowDemo();
}
}
制作菜单栏:
import java.awt.*;
import java.awt.event.*;

class MyMenuDemo
{
private Frame f;
private MenuBar mb;
private Menu m,subMenu;
private MenuItem closeItem,subItem;

MyMenuDemo()
{
init();
}

public void init()
{
f = new Frame("my frame");

f.setBounds(300,200,500,400);

f.setLayout(new FlowLayout());

mb = new MenuBar();

m = new Menu("文件");
subMenu = new Menu("子菜单");

subItem = new MenuItem("子条目");

closeItem = new MenuItem("退出");

subMenu.add(subItem);

m.add(subMenu);

m.add(closeItem);

mb.add(m);

f.setMenuBar(mb);

myEvent();

f.setVisible(true);

}

public void myEvent()
{
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 MyMenuDemo();
}
}
练习:
package mymenu;

import java.awt.*;
import java.awt.event.*;
import java.io.*;

class MyMenuTest
{
private Frame f;
private MenuBar bar;
private Menu fileMenu;
private MenuItem closeItem,saveItem,openItem;
private TextArea ta;
private FileDialog openDia,saveDia;

private File file;

MyMenuTest()
{
init();
}

public void init()
{
f = new Frame("my frame");

f.setBounds(300,200,500,400);

//f.setLayout(new FlowLayout());

bar = new MenuBar();

fileMenu = new Menu("文件");
saveItem = new MenuItem("保存");

openItem = new MenuItem("打开");

closeItem = new MenuItem("退出");

ta = new TextArea();

fileMenu.add(openItem);

fileMenu.add(saveItem);

fileMenu.add(closeItem);

bar.add(fileMenu);

f.setMenuBar(bar);

openDia = new FileDialog(f,"我要打开",FileDialog.LOAD);
saveDia = new FileDialog(f,"我要保存",FileDialog.SAVE);

f.add(ta);

myEvent();

f.setVisible(true);

}

public void myEvent()
{

openItem.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
openDia.setVisible(true);

String dirPath = openDia.getDirectory();
String fileName = openDia.getFile();
if(dirPath==null || fileName==null)
return;

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("读取失败");
}

}
});

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.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();
}
}

                                            
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息