GUI可以说在java中并不能称为十分重要的一步,但是,我们在学习的时候由于需要记忆大量的东西,这些也十分不利于我们后来的学习,但是有的知识又是在后来的学习中不可或缺的,所以,现在在回顾的时候希望可以把它形成文字,为自己也为他人创造一点方便,将零碎的知识点记下来,时常翻看,也可以收获良多,ps:似乎也作为一个自己的学习记录呢~
1. 列表内容
AWT:依赖底层,重量级
Swing:跨平台,轻量级,依赖低,任何系统均可使用
2. 整体架构
组件排列的方式(也称布局)
流式:默认居中,从左到右
边界:上北下南左西右东中 默认的是最大填充窗体 (Frame默认的是边界布局)
网格:组件是在格子内 (计算器)
网格包式:占两格
卡片式:常见的选项卡
3. 简单的小例子
F.setsize( 横,纵) 横——距离左边 纵——距离右边
在实际的工作中
① 设置frame布局
② 设置按钮等组件并加入到面板panel中
③ 设置面板的布局管理器
④ 设置frame的布局管理器
⑤ 将panel加入到frame
public static void main(String[] args) {
Frame f=new Frame();
f.setSize(500, 100);
f.setLocation(200, 500);
// f.setLayout(new FlowLayout()); //默认居中
f.setLayout(new BorderLayout()); //默认最大填充窗体
Button b=new Button("按钮1");
f.add(b); //向里面添加组件
f.setVisible(true);
// System.out.println("h1"); 仍然会执行
}
4. 事件监听机制
5. 窗口关闭
方法一:使用windowAdapter 可以只需要复写需要重写 的类,不需要全部重写
public class FrameDemo05_01 {
public static void main(String[] args) {
Frame f=new Frame();
f.setSize(500, 100);
f.setLocation(200, 500);
f.setLayout(new FlowLayout());
Button b=new Button("按钮1");
f.add(b);
//1.为事件源添加监听 采用继承extends WindowAdapter 只用复写windowClosing()
f.addWindowListener(new MyWin1());
f.setVisible(true);
}
}
class MyWin1 extends WindowAdapter{
//2.有监听器监听的动作发生 点击关闭
//3.产生事件对象
//4.将事件对象传递给事件处理方式
@Override
public void windowClosing(WindowEvent e) {
// TODO 自动生成的方法存根
System.exit(0);
// System.out.println("窗口关闭"+e.toString());
}
}
方法二:采用匿名类的方法
public class FrameDemo05_02 {
public static void main(String[] args) {
Frame f=new Frame();
f.setSize(500, 100);
f.setLocation(200, 500);
f.setLayout(new FlowLayout());
Button b=new Button("按钮1");
f.add(b);
//为窗口添加监听 addWindowListener 传递 new WindowAdapter() 匿名对象
f.addWindowListener(new WindowAdapter() {
//1.重写窗口关闭事件
public void windowClosing(WindowEvent e) {
// TODO 自动生成的方法存根
System.exit(0);
// System.out.println("窗口关闭"+e.toString());
}
//2.重写窗口打开事件
@Override
public void windowOpened(WindowEvent e) {
// TODO 自动生成的方法存根
System.out.println("窗口打开");
}
//3.重写窗口活动事件
@Override
public void windowActivated(WindowEvent e) {
// TODO 自动生成的方法存根
System.out.println("窗口活动");
}
});
f.setVisible(true);
}
}
6. 实现事件和界面的分离
/**
* 事件和界面分开了
*
* //1.为窗口添加监听 addWindowListener
* //2.为按钮添加点击监听 addActionListener ActionListener是一个接口,里面的方法必须实现
* @author helen
* 上午11:32:07
*/
public class FrameDemo06 {
private Frame f;
private Button btn;
FrameDemo06(){
init();
}
public void init(){
f=new Frame("my frame");
f.setBounds(300, 100, 500, 400);
f.setLayout(new FlowLayout());
btn=new Button("我是按钮。");
f.add(btn);
//加载一下窗体上事件。
event(); //监听事件
f.setVisible(true);
}
public void event(){
//1.为窗口添加监听 addWindowListener
f.addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
// TODO 自动生成的方法存根
System.exit(0);
}
});
/*
按钮就是事件源。
那么选择哪个监听器呢?
通过关闭窗体示例了解到,想要知道哪个组件具备什么样的特有监听器。
需要查看该组件对象的功能。
通过查阅button的描述。发现按钮支持一个特有监听addActionListener。
*/
//2.为按钮添加点击监听 addActionListener ActionListener是一个接口,里面的方法必须实现
btn.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
// TODO 自动生成的方法存根
System.out.println("按钮事件");
// System.exit(0);
}
});
}
public static void main(String[] args) {
new FrameDemo06();
}
}
7. 各种监听事件
监听也分为多种: 活动监听 窗体监听 鼠标监听
活动监听—>返回活动事件 (ActionListener 没有适配器) 一般是button按钮的点击
btn.addActionListener
窗体监听—>返回窗体事件 (实现WindowListener 或者是 继承 WindowAdapter )
f.addWindowListener
鼠标监听—>返回鼠标事件 (匿名对象MouseAdapter MouseAdapter 实现了 MouseListener )
btn.addMouseListener
键盘监听—>返回键盘事件 (匿名对象KeyAdapter KeyAdapter实现了 KeyListener)
btn.addKeyListener
继承 WindowAdapter :可以只用实现自己需要的
实现WindowListener :需要写出所有的
键盘事件
/**
* //1.添加鼠标监听事件addMouseListener 匿名对象MouseAdapter MouseAdapter 实现了 MouseListener
* @author helen
* 下午12:30:26
*/
public class frameDemo07 {
private Frame f;
private Button btn;
frameDemo07(){
init();
}
public void init(){
f=new Frame("我是标题");
f.setBounds(300, 100, 500, 400);
f.setLayout(new FlowLayout());
btn=new Button("我是按钮。");
f.add(btn);
event();
f.setVisible(true);
}
public void event(){
f.addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
// TODO 自动生成的方法存根
System.exit(0);
}
});
btn.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
// TODO 自动生成的方法存根
System.out.println("action ok!");
}
});
//1.添加鼠标监听事件addMouseListener 匿名对象MouseAdapter MouseAdapter 实现了 MouseListener
btn.addMouseListener(new MouseAdapter() {
private int count=0;
private int clickcount=0;
@Override
public void mouseEntered(MouseEvent e) {
System.out.println("鼠标进入到组件"+(count++));
}
/*@Override
public void mouseClicked(MouseEvent e) {
// TODO 自动生成的方法存根
System.out.println("鼠标点击组件"+(clickcount++));
}
*/
@Override
public void mouseClicked(MouseEvent e) {
// TODO 自动生成的方法存根
if(e.getClickCount()==2){
System.out.println("鼠标双击组件"+(clickcount++));
}
}
});
}
public static void main(String[] args) {
new frameDemo07();
}
}
鼠标事件
public class FrameDemo08 {
private Frame f;
private Button btn;
private TextField tf;
FrameDemo08(){
init();
}
public void init(){
f=new Frame("我是标题");
f.setBounds(300, 100, 500, 400);
f.setLayout(new FlowLayout());
btn=new Button("我是按钮。");
tf=new TextField(20);
f.add(tf);
f.add(btn);
event();
f.setVisible(true);
}
public void event(){
//添加窗口监听
f.addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
// TODO 自动生成的方法存根
System.exit(0);
}
});
//添加键盘监听
tf.addKeyListener(new KeyAdapter() {
@Override
public void keyPressed(KeyEvent e) {
// TODO 自动生成的方法存根
int code=e.getKeyCode();
if(!(code>=KeyEvent.VK_0&&code<=KeyEvent.VK_9)){
System.out.println(code+"非法");
e.consume();
}
}
});
//添加键盘监听
btn.addKeyListener(new KeyAdapter() {
@Override
public void keyPressed(KeyEvent e) {
// TODO 自动生成的方法存根
if(e.getKeyCode()==KeyEvent.VK_ESCAPE){
System.exit(0);
}
/*if(e.getKeyCode()==KeyEvent.VK_ENTER){
System.exit(0);
}*/
if(e.isControlDown()&&e.getKeyCode()==KeyEvent.VK_ENTER){
System.out.println("ctrl enter are run");
}
System.out.println(e.getKeyChar()+"--"+e.getKeyCode());
System.out.println(KeyEvent.getKeyText(e.getKeyCode())+"**"+e.getKeyCode());
}
});
}
public static void main(String[] args) {
new FrameDemo08();
}
}
总结:
① 添加事件源
② 确定事件
③ 确定监听器
④ 确定操作谁
8. 监听小例子
内容转移
/**
* 内容转移
* @author helen
* 下午12:41:24
*/
public class FrameDemo09 {
Frame frame;
TextField tf;
Button btn;
TextArea ta;
public FrameDemo09(){
MyInit();
}
public void MyInit(){
frame=new Frame("我的窗口");
frame.setBounds(100, 100,600, 500);
frame.setLayout(new FlowLayout());
tf=new TextField(60);
btn=new Button("login ");
ta=new TextArea(25,70);
frame.add(tf);
frame.add(btn);
frame.add(ta);
MyEvent();
frame.setVisible(true);
}
private void MyEvent() {
// TODO 自动生成的方法存根
frame.addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
// TODO 自动生成的方法存根
System.out.println("关闭窗口");
System.exit(0);
}
});
//点击button,内容转移
btn.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
// TODO 自动生成的方法存根
String text=tf.getText();
ta.setText(text);
tf.setText("");
System.out.println("text:"+text);
}
});
}
public static void main(String[] args) {
new FrameDemo09();
}
}
输入路径,打印下面的所有文件和文件夹 (没有做错误处理)
public class FrameDemo09_2 {
Frame frame;
TextField tf;
Button btn;
TextArea ta;
public FrameDemo09_2(){
MyInit();
}
public void MyInit(){
frame=new Frame("我的窗口");
frame.setBounds(100, 100,600, 500);
frame.setLayout(new FlowLayout());
tf=new TextField(60);
btn=new Button("login ");
ta=new TextArea(25,70);
frame.add(tf);
frame.add(btn);
frame.add(ta);
MyEvent();
frame.setVisible(true);
}
private void MyEvent() {
// TODO 自动生成的方法存根
frame.addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
// TODO 自动生成的方法存根
System.out.println("关闭窗口");
System.exit(0);
}
});
btn.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
// TODO 自动生成的方法存根
String dirname=tf.getText();
File dir=new File(dirname);
if(dir.exists()&&dir.isDirectory()){
ta.setText("");
String[] names=dir.list();
for(String name:names){
ta.append(name+"\r\n");
}
}
}
});
}
public static void main(String[] args) {
new FrameDemo09_2();
}
}
9. 对话框的使用
在上边的例子中,做出了当输入路径错误时候的判断 (如果设置了模式为true 表示:Dialog 点击后才能继续操作)
/**
* 打印路径下的文件的目录
* @author helen
* 下午12:41:57
*/
public class FrameDemo10 {
private Frame frame;
private TextField tf;
private Button btn;
private TextArea ta;
private Dialog d;
private Label l;
private Button btnmessage;
public FrameDemo10() {
MyInit();
}
public void MyInit() {
frame = new Frame("我的窗口");
frame.setBounds(100, 100, 600, 500);
frame.setLayout(new FlowLayout());
tf = new TextField(60);
btn = new Button("login ");
ta = new TextArea(25, 70);
//此时的d是作为一个独立的窗体 设置布局
d = new Dialog(frame, "提示信息", true);
d.setBounds(400, 200, 240, 150); //仍然是相对于整个电脑的左上角
d.setLayout(new FlowLayout());
l = new Label();
btnmessage = new Button("确定");
d.add(l);
d.add(btnmessage);
frame.add(tf);
frame.add(btn);
frame.add(ta);
MyEvent();
frame.setVisible(true);
}
private void MyEvent() {
//给frame加上窗口监听器
frame.addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
// TODO 自动生成的方法存根
System.out.println("关闭窗口");
System.exit(0);
}
});
//给d加上窗口监听器 关闭窗口d
d.addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
// TODO 自动生成的方法存根
d.setVisible(false);
// System.out.println("关闭窗口d");
// System.exit(0);
}
});
//tf加上监听 输入换行结束输入,并且显示所有路径
tf.addKeyListener(new KeyAdapter() {
@Override
public void keyPressed(KeyEvent e) {
// TODO 自动生成的方法存根
if(e.getKeyCode()==KeyEvent.VK_ENTER){
showDir();
}
}
});
//btn加上监听 显示所有路径
btn.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
showDir();
}
});
//给对话框 中的按钮btnmessage加上监听
btnmessage.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
d.setVisible(false);
}
});
}
public void showDir(){
// TODO 自动生成的方法存根
String dirname = tf.getText();
File dir = new File(dirname);
if (dir.exists() && dir.isDirectory()) {//正确获得路径信息
ta.setText("");
String[] names = dir.list();
for (String name : names) {
ta.append(name + "\r\n");
}
}else{//不正确获得信息
String info="输入的信息:"+dirname+"错误,重新输入";
l.setText(info);
d.setVisible(true);
}
}
public static void main(String[] args) {
new FrameDemo10();
}
}
10. 简单的记事本
/**
* 记事本
* @author helen
* 下午12:43:17
*/
public class FrameDemo11 {
private Frame frame;
private MenuBar mb;
private Menu menu, submenu;
private MenuItem mitem, subitem;
private MenuItem openitem, saveitem;
private FileDialog opendia, savedia;
private File file;
private TextArea ta;
public FrameDemo11() {
MyInit();
}
public void MyInit() {
frame = new Frame("我的窗口");
frame.setBounds(100, 100, 650, 500);
// frame.setLayout(new FlowLayout());
mb = new MenuBar();
menu = new Menu("文件");
submenu = new Menu("子菜单");
mitem = new MenuItem("退出");
subitem = new MenuItem("子条目");
openitem = new MenuItem("打开");
saveitem = new MenuItem("保存");
ta = new TextArea();
submenu.add(subitem);
menu.add(submenu);
menu.add(saveitem);
menu.add(openitem);
menu.add(mitem);
mb.add(menu);
frame.setMenuBar(mb);
opendia = new FileDialog(frame, "我是打开", FileDialog.LOAD);
savedia = new FileDialog(frame, "我是保存", FileDialog.SAVE);
frame.add(ta);
MyEvent();
frame.setVisible(true);
}
private void MyEvent() {
//窗体事件
frame.addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
// TODO 自动生成的方法存根
System.out.println("关闭窗口");
System.exit(0);
}
});
//动作事件
mitem.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
// TODO 自动生成的方法存根
System.out.println("关闭窗口");
System.exit(0);
}
});
//动作事件
openitem.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
// TODO 自动生成的方法存根
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 file = new File(dirPath, fileName);
try {
FileReader fr = new FileReader(file);
BufferedReader bufr = new BufferedReader(fr);
String line = null;
while ((line = bufr.readLine()) != null) {
ta.append(line + "\r\n");
}
bufr.close();
} catch (IOException e1) {
throw new RuntimeException("读取失败");
}
}
});
saveitem.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
// TODO 自动生成的方法存根
if (file==null) {
savedia.setVisible(true);
String dirPath = savedia.getDirectory();
String fileName = savedia.getFile();
System.out.println(dirPath + "--" + fileName);
if (dirPath == null || fileName == null)
return;
file = new File(dirPath, fileName);
}
try{
FileWriter fw=new FileWriter(file);
BufferedWriter bufw=new BufferedWriter(fw);
String text=ta.getText();
bufw.write(text);
bufw.flush();
bufw.close();
}catch(IOException eq){
throw new RuntimeException("保存失败");
}
}
});
}
public static void main(String[] args) {
new FrameDemo11();
}
}