您的位置:首页 > 产品设计 > UI/UE

day22GUI编程。Frame。事件监听机制。自制记事本。打开保存功能。封装双击打开jar。

2013-05-23 11:24 337 查看
/*
GUI编程。Frame。事件监听机制。自制记事本。打开保存功能。封装双击打开jar。
*/

/*
GUI 图形化界面编程
java.Awt:需要调用本地系统方法实现功能,比较依赖系统。
重量级控件。

javax.Swing:在AWT的基础上,建立的一套图形界面系统。其中
提供了更多的组件。而且完全由java实现,移植性好,属轻量级组件。

////////////////////GUI继承关系图像




/*

特殊:
Container:作为一个组件,里面能够添加组件。add方法。
Frame:窗体。
布局管理器。
FlowLayout(流式布局管理器)
从左到右的顺序进行
Panal的默认布局。
BorderLayout(边界式布局)
东南西北中。
如果没有指定,就居中。再添加一个就覆盖。
Frame默认布局
GridLayout(网格布局管理器)
规则的矩阵,比如计算器数字按钮。
CardLayout(卡片布局管理器)
选项卡。比如:桌面上面->属性,上面的切换按钮
GridBagLayout(风格包布局管理器)
非规则的矩阵。比如:计算器数字上面按钮。不只占一格。
最叼的布局,坐标式布局,想放哪放哪。

复杂布局:
先整大的,再把小的布局添加到Panel面板中,设置面板的布局就好。
*/
Class AwtDemo
{
public static void main(String[] args)
{
Frame f = new Frame("my awt");
f.setSize(500,400);//设置大小。
f.setLocation(200,100);//设置一出来时窗口的位置。

Button b = new Botton("我是一个按钮");

f.setLayout(new FlowLayout());//设置布局
f.add(b);//把按钮添加到窗口里。

f.setVisible(true);//设为可见
System.out.println(".......");
}
}
import java.awt.*;
import java.awt.event.*;
/*
创建图形化界面:
1,创建frame窗体。
2,对窗体进行基本设置。
比如大小,位置,布局。
3,定义组件。
4,对组件通过窗体的add方法添加到窗体中。
5,让窗体显示。通过setVisible(ture)完成。

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

事件源:就是awt包或者swing包中的那些图形界面组件。
事件:每一个事件源都有自己特有的对应事件和共性事件。
监听器:将可以触发某一个事件的动作(不止一个动作)都封装到了监听器中。
以上三者在java中都已经定义好了,直接获取其对象来用就好了。

我们要做的事情是,就是对产生的动作进行处理。
*/
class  FrameDemo
{
public static void main(String[] args)
{
Frame f = new Frame("my awt");

f.setSize(500,400);

f.setLayout(new FlowLayout());
f.setLocation(200,100);

Button b = new Button("我是一个按钮");

f.add(b);
//f.addWindowListener(new MyWin());//第一种写法,添加窗体监听器
f.addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{
System.out.println("over");
System.exit(0);
}

public void windowActivated(WindowEvent e)
{
System.out.println("我活了。。。");
}

public void windowOpened(WindowEvent e)
{
System.out.println("我被打开了。。。");
}
});
f.setVisible(true);
}
}
/*
class MyWin extends WindowAdapter//对应第一种写法
{
public void windowClosing(WindowEvent e)//Event接收事件。
{
//System.out.println("window closing---"+e.toString());
System.exit(0);
}
}
//addWindowListener(接口),要监听的话,必须全部实现接口方法。
java给我们提供了一个方便的Adapter.每个Adapter都帮我们空实现了接口方法。
我们要用的话,只要自己实现我们要用的方法就可以了。
事件包是子包。import java.awt.event.*;
*/
import java.awt.*;
import java.awt.event.*;
//将事件和图形化界面分开。
class  FrameDemo1
{
//1定义该图形中所需的组件的引用。
private Frame f;
private Button but;

FrameDemo1()
{
init();//一初始化就具备了事件。
}
public void init()//组件的操作
{
f = new Frame("my frame");

//2对Frame进行基本设置。
f.setBounds(200,200,500,400);
f.setLayout(new FlowLayout());

but = new Button("my button");

f.add(but);//3组件添加到frame中

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

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

private void myEvent()//响应事件的操作。
{
f.addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{
System.exit(0);
}
});
/*
让按钮具备退出程序的功能

按钮是事件源。
那么选择哪个监听器呢?
通过关闭窗体示例了解到,想要知道哪个组件具备什么样的特有监听器,
需要查看该组件对象的功能。
通过查阅Button的描述,发现按钮支持一个特有监听addActionListener()
Adapter:方法超过三个,才去适配。不然没必要。
*/

but.addActionListener(new ActionListener()//只ActionListener只有一个方法,活动监听。
{
public void actionPerformed(ActionEvent e)//覆盖一个方法就可,没有适配Adapter
{
System.out.println("退出,按钮干的");
System.exit(0);
}
});
}
public static void main(String[] args)
{
new FrameDemo1();
}
}

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(200,200,500,400);
f.setLayout(new FlowLayout());

tf = new TextField(20);
f.add(tf);

but = new Button("my button");
f.add(but);//组件添加到frame中

myEvent();
f.setVisible(true);//显示窗体。
}
private void myEvent()
{
f.addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{
System.exit(0);
}
});

//给tf文本.添加一个键盘监听
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();//非法的就不让进去文本框。
}
/*//isControlDown():是否被按下。。。。
//组合键。
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());
}
});
but.addMouseListener(new MouseAdapter()
{
private int count = 1;
private int kcount = 1;
public void mouseEntered(MouseEvent e)
{
System.out.println("鼠标进入组件"+count++);
}
public void mouseClicked(MouseEvent e)
{
if (e.getClickCount() == 2)//双击事件。
{
System.out.println("双击动作"+kcount++);
}
}
});
//活动监听和鼠标监听,二个都点击,谁先执行。鼠标监听先执行。但是没有鼠标也能按Enter。
//所以我们尽量加ActionLister

but.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
System.out.println("按钮干的");
}
});
}
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(200,150,500,400);
f.setLayout(new FlowLayout());

tf = new TextField(50);
but = new Button("转到");
ta = new TextArea(25,45);

d = new Dialog(f,"提示信息-self",true);//模式为true,不点击就操作不了其他的。
d.setBounds(400,200,240,150);
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);
}

private void myEvent()
{
d.addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{
d.setVisible(false);//点击X对话框关闭
}
});

okbut.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
d.setVisible(false);//点击确定对话框关闭
}
});

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

f.addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{
System.exit(0);
}
});
}
private void showDir()
{
String dirPath = tf.getText();
//ta.setText(text);
//System.out.println(text);

File dir =  new File(dirPath);
if (dir.exists() && dir.isDirectory())
{
ta.setText("");
String[] names = dir.list();
for (String name:names )
{
ta.append(name+"\r\n");
}
}
else
{
//Dialog:对话框。也是一个窗体。里面有错误信息(Label)和按钮(Button)
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.*;
//制作记事本一样的菜单,子菜单,退出。
//明白一点,Menu,可以添加Menu,还可以添加MenuItem.
//Menu添加到Menu时,作为子菜单,里面又可以添加。
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(200,150,500,600);
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()
{
f.addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{
System.exit(0);
}
});

closeItem.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
System.exit(0);
}
});
}
public static void main(String[] args)
{
new MyMenuDemo();
}
}

/*
完善记事本的保存打开功能
*/
import java.awt.*;
import java.awt.event.*;
import java.io.*;
public class MyMenuTestDemo {
public static void main(String[] args) {
new MenuDemo();
}
}
class MenuDemo
{
private Frame f;
private MenuBar bar;
private Menu me;
private MenuItem load,save,over;
private TextArea ta;
private FileDialog fd;
private FileDialog savefd;
private File file;
MenuDemo()
{
init();
}
public void init()
{
f = new Frame("frame");
bar = new MenuBar();
me = new Menu("文件");
load = new MenuItem("打开");
save = new MenuItem("保存");
over = new MenuItem("退出");
ta = new TextArea();
fd = new FileDialog(f);
savefd = new FileDialog(f,"save",FileDialog.SAVE);;

f.setBounds(200, 300, 600, 600);
//f.setLayout(new FlowLayout());

bar.add(me);
me.add(load);
me.add(save);
me.add(over);

f.setMenuBar(bar);
f.add(ta);

myEvent();
f.setVisible(true);
}
public void myEvent()
{
save.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent arg0) {
String text = ta.getText();
if(file==null){//如果没有文件名,才弹出。
savefd.setVisible(true);
String path = savefd.getDirectory();
String name = savefd.getFile();
file = new File(path,name);
if(path == null||name==null)//按取消时别发生异常
return;
}

BufferedWriter bufw = null;
try {
bufw = new BufferedWriter(new FileWriter(file));
bufw.write(text);
bufw.close();
} catch (IOException e) {
throw new RuntimeException("保存失败");
}
}
});
load.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent arg0) {
fd.setVisible(true);
String path = fd.getDirectory();
String name = fd.getFile();
if(path == null||name==null)
return;
file = new File(path,name);
ta.setText("");
BufferedReader bufr = null;
try {
bufr = new BufferedReader(new FileReader(file));
String line = null;
while((line=bufr.readLine())!=null)
{
ta.append(line+"\r\n");
}
bufr.close();
} catch (IOException e) {
throw new RuntimeException("打开失败");
}
}
});
over.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent arg0) {
// TODO Auto-generated method stub
System.exit(0);
}
});
f.addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e)
{
System.exit(0);
}
});
}
}

/*
把以上记事本程序封装成双击可打开的jar包。
1步:javac -d c:\myclass MenuTest.java  -->把java编译到包c:\myclass中,这个包要首先自己创建好。
2.	第3步之前要先创建一个1.txt文件,内容如下。这个文件放到class文件夹下。

3:jar -cvfm  myjar.jar			1.txt   mymenu
封装成什么名字		需要封装的文件,文件夹
注意是cvfm,不是cvf,过程中记得目录的进入。

配置清单的信息:
1.txt
Main-Class: mymenu.day22.MenuTest

注意 Class: 后面要有空格,记得写上包名。day22.

文件内容最后要有回车,文件名(1.txt)可以随便起。
*/
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐