该组件比较特殊,可将其他组件添加到容器的列表中;
add(Component comp);将指定组件追加到容器尾部;
add(Component comp,int index);将指定组件添加到容器的给定位置上;
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进行基本设置。
f.setBounds(300,100,600,500);
f.setLayout(new FlowLayout());
but = new Button("my button");
//将组件添加到frame中
f.add(but);
//加载一下窗体上事件。
myEvent();
//显示窗体;
f.setVisible(true);
}
private void myEvent(){
f.addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e){
System.exit(0);
}
});
//让按钮具备退出程序的功能
/*
按钮就是事件源。
那么选择哪个监听器呢?
通过关闭窗体示例了解到,想要知道哪个组件具备什么样的特有监听器。
需要查看该组件对象的功能。
通过查阅button的描述。发现按钮支持一个特有监听addActionListener。
*/
but.addActionListener(new ActionListener(){
private int count = 1;
public void actionPerformed(ActionEvent e){
//System.out.println("退出,按钮干的");
//System.exit(0);
//f.add(new Button("Button-"+(count++)));
//f.setVisible(true);
//f.validate();
//System.out.println(e.getSource());
Button b = (Button)e.getSource();
Frame f1 = (Frame)b.getParent();
f1.add(new Button("button-"+count++));
f1.validate();
}
});
}
public static void main(String[] args){
new FrameDemo();
}
}
事件监听机制组成
事件监听机制的特点:
添加事件监听器:
窗体事件:
WindowAdepter以实现该接口,但方法为空,此类存在的目的就是方便于创建侦听器对象,(如果要实现WindowListener接口,则需要定义接口内的所有方法,比较麻烦,故:此类的出现,只需将需要的方法复写即可);
例 :定义一个类继承WindowAdpter并复写关闭方法,则可实现窗口关闭的需要:
class MyWin extends WindowAdapter
{
public void WindowClosing(WindowEvent e)
{
System.exit(0);
}
}
该方法关联到监听器并添加到事件源,点击关闭按钮,则会退出程序方法中的参数:WindowEvent e为事件信息;
注:添加事件监听器是,需导入Java.awt.even包;
事件监听机制:
mport 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 window");
f.setBounds(300,100,600,500);
f.setLayout(new FlowLayout());
tf = new TextField(60);
but = new Button("转到");
ta = new TextArea(25,70);
d = new Dialog(f,"提示信息-self",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(){
okBut.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
d.setVisible(false);
}
});
d.addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent 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();
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{
String info = "您输入的信息:"+dirPath+"是错误的。请重输";
lab.setText(info);
d.setVisible(true);
}
}
public static void main(String[] args) {
new MyWindowDemo();
}
}
附:Component中的方法:
- addKeyListener;添加键盘事件;
- addMouseListener;添加鼠标事件;
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");
f.setBounds(300,100,600,500);
f.setLayout(new FlowLayout());
tf = new TextField(20);
but = new Button("my button");
f.add(tf);
f.add(but);
myEvent();
f.setVisible(true);
}
private void myEvent(){
f.addWindowListener(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添加一个键盘监听。
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());
}
});
/*
but.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
System.out.println("action ok");
}
});
*/
/*
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++);
}
});
*/
}
public static void main(String[] args) {
new MouseAndKeyEvent();
}
}
常见构造函数:
Label(String text)/(String text,int alignment);text-标签显示的字符串,alignment-对齐方式值;
常用方法:
该类封装绑定到框架的菜单栏的平台概念,可以使Fram的setMenuBar方法将菜单栏与Fram对象相关联,例:
MenuBar mb=new MenuBar();
Fram f=new Frame();
f.setMenuBar(mb);
常用方法:
add(Menu m);将指定的菜单添加到菜单栏;
……
该类的对象是从菜单栏部署的下拉是菜单组件,菜单可以是任意分离式菜单,菜单中的每一项都必须属于MenuItem类,它可以是MenuItem的一个实例,子菜单或复选框,Menu为MenuItem的子类;
简单说:窗体(Frame)中可以添加菜单栏(MenuBar);
菜单栏中可以添加菜单Menu或MenuItem子菜单;
MenuItem下不能添加东西,只能被添加;
构造方法;
常用方法:
import java.awt.*;
import java.awt.event.*;
import java.io.*;
public class MyMenuTest{
private Frame f;
private MenuBar bar;
private TextArea ta;
private Menu fileMenu;
private MenuItem openItem,saveItem,closeItem;
private FileDialog openDia,saveDia;
private File file;
MyMenuTest(){
init();
}
public void init(){
f = new Frame("my window");
f.setBounds(300,100,650,600);
bar = new MenuBar();
ta = new TextArea();
fileMenu = new Menu("文件");
openItem = new MenuItem("打开");
saveItem = new MenuItem("保存");
closeItem = new MenuItem("退出");
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);
}
private void myEvent(){
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.flush();
bufw.close();
}catch (IOException ex){
throw new RuntimeException();
}
}
});
openItem.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
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 = 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("读取失败");
}
}
});
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();
}
}
将一个java程序做成一个可双击执行的程序,jar包;
制作步骤:
编译:将文件包编译到指定目录下
javac -d c:\……(目录路径) MyPackage.java(源文件名);
配置信息设置:添加主类名称;
指定文件配置信息,将刚才设置爱好的配置信息编译进jar包,命令(先进入到包存储目录下):
jar - cvfm my.jar(jar包名) 1.txt(配置文件) mymenu(包名);
将jar文件关联到jre\bin下的javaw.exe文件;
注:XP系统可通过配置系统文件win7系统下需要通过修改注册表才能实现
HKEY_CLASSES_ROOT\Application\javaw.ext\shell\open\command,修改数据值为:[“D:\Program\Files\Java\jdk1.6…\jre\bin\javaw.exe”-jar”%1”](在元数据的”%1”前面加上“-jar”即可,如上所示;)