java awt jar包_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(newWindowAdapter()

{public voidwindowClosing(WindowEvent e)

{

System.exit(0);

}

});

5.Action事件

closeItem.addActionListener(newActionListener()

{public voidactionPerformed(ActionEvent e)

{

System.exit(0);

}

});

6.鼠标事件

but.addMouseListener(newMouseAdapter()

{private int count = 1;private int clickCount = 1;public voidmouseEntered(MouseEvent e)

{

System.out.println("鼠标进入到该组件"+count++);

}public voidmouseClicked(MouseEvent e)

{if(e.getClickCount()==2)

System.out.println("双击动作"+clickCount++);

}

});

7.键盘事件

but.addKeyListener(newKeyAdapter()

{public voidkeyPressed(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 classmenu1 {

Frame f;

MenuBar bar;

Menu m1;

TextArea ta=newTextArea();

MenuItem openItem,saveItem,exitItem;

FileDialog openDialog,saveDialog;privateFile fl;publicmenu1(){

inite();

}private voidinite(){

f=new Frame("FileList");

f.setSize(300, 400);

f.setLocation(100, 200);

bar=newMenuBar();

m1=new Menu("file");

openItem=new MenuItem("open");

saveItem=new MenuItem("save");

exitItem=new MenuItem("exit");

bar.add(m1);

ta=newTextArea();

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 voidgetEvent() {

saveItem.addActionListener(newActionListener(){public voidactionPerformed(ActionEvent e) {if(fl==null){

saveDialog.setVisible(true);

String dirPath=saveDialog.getDirectory();

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

fl=newFile(dirPath,fileName);

}try{

BufferedWriter bfw=new BufferedWriter(newFileWriter(fl));

bfw.write(ta.getText());

bfw.flush();

}catch(IOException e1) {

e1.printStackTrace();

}

}

});

openItem.addActionListener(newActionListener(){public voidactionPerformed(ActionEvent e) {

openDialog.setVisible(true);

String dirPath=openDialog.getDirectory();

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

ta.setText("");

fl=newFile(dirPath,fileName);try{

BufferedReader bfr=new BufferedReader(newFileReader(fl));

String str=null;while((str=bfr.readLine())!=null){

ta.append(str+"\r\n");

}

}catch(FileNotFoundException e1) {

e1.printStackTrace();

}catch(IOException e1) {

e1.printStackTrace();

}

}

});

f.addWindowListener(newWindowAdapter(){public voidwindowClosing(WindowEvent e){

System.exit(0);

}

});

exitItem.addActionListener(newActionListener(){public voidactionPerformed(ActionEvent e) {

System.exit(0);

}

});

}public static voidmain(String[] args) {newmenu1();

}

}

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

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值