zoukankan      html  css  js  c++  java
  • java awt学习笔记

         最近这两天,花了些时间温习了java.awt的学习,故今日花些时间写下自己的总结吧。

    1.常见的组件:Button、TextArea、Label、Checkbox、TextField

    Container---Window(Frame,Dialog)、Panel

    布局管理器:

    FlowLayout(流式布局管理器)
    从左到右的顺序排列。

    Panel默认的布局管理器。

    BorderLayout(边界布局管理器)

    东,南,西,北,中

    Frame默认的布局管理器。

    GridLayout(网格布局管理器)

    规则的矩阵

    CardLayout(卡片布局管理器)

    选项卡

    GridBagLayout(网格包布局管理器)

    非规则的矩阵

    2.创建图形化界面:

    <1>创建frame窗体。
    <2>对窗体进行基本设置。比如大小,位置,布局。
    <3>定义组件。
    <4>将组件通过窗体的add方法添加到窗体中。
    <5>让窗体显示,通过setVisible(true)

    3.事件监听机制

    事件监听机制的特点:
    <1>事件源。
    <2>事件。
    <3>监听器。
    <4>事件处理。

    事件源:就是awt包或者swing包中的那些图形界面组件。

    事件:每一个事件源都有自己特有的对应事件和共性事件。

    监听器:将可以触发某一个事件的动作(不只一个动作)都已经封装到了监听器中。

    以上三者,在java中都已经定义好了。
    直接获取其对象来用就可以了。

    我们要做的事情是,就是对产生的动作进行处理。

    4.窗体事件

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

    5.Action事件

    closeItem.addActionListener(new ActionListener()
            {
                public void actionPerformed(ActionEvent e)
                {
                    System.exit(0);
                }
            });

    6.鼠标事件

    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++);
                }
            });

    7.键盘事件

    but.addKeyListener(new KeyAdapter()
            {
                public void keyPressed(KeyEvent e)
                {    
                    if(e.isControlDown()&&e.getKeyCode()==KeyEvent.VK_ENTER)
                        //System.exit(0);
                    System.out.println("ctrl+enter is run");
    
                    //System.out.println(KeyEvent.getKeyText(e.getKeyCode())+"...."+e.getKeyCode());
                }
            });

    8.Dialog的使用

    9.菜单的应用

    MenuBar,Menu,MenuItem
    先创建菜单条,再创建菜单,每一个菜单 中建立菜单项。
    也可以菜单添加到菜单中,作为子菜单。
    通过setMenuBar()方法,将菜单添加到Frame中。

    一个简单的记事本程序代码如下:

    package mymenu ;
    
    import java.awt.*;
    import java.awt.event.*;
    import java.io.*;
    public class menu1 {
        Frame f;
        MenuBar bar;
        Menu m1;
        TextArea ta=new TextArea();
        MenuItem openItem,saveItem,exitItem;
        FileDialog openDialog,saveDialog;
        private File fl;
        public menu1(){
            inite();
        }
        private void inite(){
            f=new Frame("FileList");
            f.setSize(300, 400);
            f.setLocation(100, 200);
            bar=new MenuBar();
            m1=new Menu("file");
            openItem=new MenuItem("open");
            saveItem=new MenuItem("save");
            exitItem=new MenuItem("exit");
            bar.add(m1);
            ta=new TextArea();
            m1.add(openItem);
            m1.add(saveItem);
            m1.add(exitItem);
            openDialog=new FileDialog(f,"openFile",FileDialog.LOAD);
            saveDialog=new FileDialog(f,"saveFile",FileDialog.SAVE);
            f.setMenuBar(bar);
            getEvent();
            f.add(ta);
            f.setVisible(true);
            
        }
    
    
        private void getEvent() {
            saveItem.addActionListener(new ActionListener(){
                public void actionPerformed(ActionEvent e) {
                    if(fl==null){
                        saveDialog.setVisible(true);
                        String dirPath=saveDialog.getDirectory();
                        String fileName=saveDialog.getFile();
                        if(dirPath==null||fileName==null)
                            return;
                        fl=new File(dirPath,fileName);
                    }
                    try {
                        BufferedWriter bfw=new BufferedWriter(new FileWriter(fl));
                        bfw.write(ta.getText());
                        bfw.flush();
                    } catch (IOException e1) {
                        e1.printStackTrace();
                    }
                }    
            });
            openItem.addActionListener(new ActionListener(){
                public void actionPerformed(ActionEvent e) {
                    openDialog.setVisible(true);
                    String dirPath=openDialog.getDirectory();
                    String fileName=openDialog.getFile();
                    if(dirPath==null||fileName==null)
                        return;
                    ta.setText("");
                   fl=new File(dirPath,fileName);
                    try {
                        BufferedReader bfr=new BufferedReader(new FileReader(fl));
                        String str=null;
                        while((str=bfr.readLine())!=null){
                            ta.append(str+"
    ");
                        }
                    } catch (FileNotFoundException e1) {
                        e1.printStackTrace();
                    } catch (IOException e1) {
                        e1.printStackTrace();
                    }
    
                }    
            });
            f.addWindowListener(new WindowAdapter(){
                public void windowClosing(WindowEvent e){
                    System.exit(0);
                }
            });
            exitItem.addActionListener(new ActionListener(){
                public void actionPerformed(ActionEvent e) {
                    System.exit(0);
                }    
            });
            
        }
    
    
        public static void main(String[] args) {
                  new menu1();
        }
    
    }

    10.jar包双击执行

    利用java -d  编译生成mymenu包,再在当前目录建立一个文本文件,不妨为1.txt,则内容为:

    Main-Class: mymenu.menu1

    注意空格以及末尾的换行

    这是输入jar包生成指令:

    jar -cvfm my.jar 1.txt mymenu

    则会生成 my.jar

    WIN7系统下打开Jar文件时报错,提示"Could not find the main class" 的警告。

    通过修改注册表来解决该问题。

          步骤一:打开注册表,开始->运行(或者用快捷键WIN+R),输入regedit,确定;

          步骤二:找个Jar文件,选择打开方式,输入D:/Program Files/Java/jre/bin/javaw.exe,再选择打开就行了;

          步骤三:进入HKEY_CLASSES_ROOT/Applications/javaw.exe/shell/open/command,修改默认的键值为 "D:/Program Files/Java/jre/bin/javaw.exe" -jar "%1" 。

    Java环境安装在其他地方也类似,只要改一下文件地址就行了。

    eclipse导出jar包

    http://jingyan.baidu.com/article/5bbb5a1b280d0113eba179ce.html

    http://blog.csdn.net/memray/article/details/17969443

  • 相关阅读:
    xml=>数组
    php的session锁
    压缩服务器中的文件夹,并下载到电脑
    通过onkeydown事件来控制只允许数字
    简单算法
    memcahe安装
    HTML div css 强制 换行 不换行
    windows charles response 乱码解决办法
    根据字节流判断内容是否使用UTF-8编码
    nginx安装过程
  • 原文地址:https://www.cnblogs.com/liujunming/p/4541013.html
Copyright © 2011-2022 走看看