黑马程序员_java基础11-GUI

来源:互联网 发布:ubuntu ntfs支持 编辑:程序博客网 时间:2024/06/12 19:26
------- android培训、java培训、期待与您交流! ---------

day22**GUI*************************************************************
GUI==Graphical User Interface图形用户接口
CLI==Command line User Interface命令行用户接口
java为GUI提供的对象都在java.Awt和javax.Swing中!
java.Awt:Abstract Window ToolKit(抽象窗口工具包),
需要调用本地系统的方法实现功能,属重量级控件。(和系统的依赖性较强)
javax.Swing:在AWT的基础上,建立的一套图形化系统,其中提供了更多的组件,
而且完全由Java实现,增强了移植性,属轻量级控件。(和系统的依赖性不强)
去eclipse网下载swt包!
体系如下!
@@Component组件


1 Button按钮:
2 Label标签:封装文字
3 Checkbox复选框:
4 TextComponent:文本组件
TextArea  多行文本区域
TextField 单行文本


5特殊 Container容器(特殊:里面能够添加组件)
Window窗口
Frame框架
Dialog对话框
FileDialog文件对话框
Panel面板
@@布局
容器中的组件的排放方式,就是布局。
常见的布局管理器:
FlowLayout 流式布局管理器
从左到右的顺序排列
Panel面板 默认的布局
BorderLayout 边界布局管理器
东南西北中(没有具体制定方向的话,默认居中,添加新的就覆盖)
Frame框架 默认的布局
GridLayout 网格布局管理器
规则的矩阵
CardLayout 卡片布局管理器
选项卡
GridBagLayout 网格包布局管理器
非规则的矩阵
@@Frame
图形化界面由另外线程控制。
 void setVisible(boolean b) 
          根据参数 b 的值显示或隐藏此组件。 




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


1 Frame f = new Frame("我的窗体");
2 f.setSize(500,500);//横坐标,纵坐标,组件坐标!
f.setLocation(700,200);//距左边界距离,距上边界距离,组件大小!
//f.setBounds(300,100,600,500);//组件的坐标和大小。
f.setLayout(new FlowLayout());
3 Button b = new Button("按钮");
...


4 f.add(b);
5 f.setVisible(true);


java.awt.event.*;
@@事件监听机制的特点:
1,事件源
2,事件
3,监听器
4,事件处理。


事件源:就是awt包或者swing包中的那些图形化界面组件。
事件:每一个事件源都有自己特有的对应事件和共性事件。
监听器:将可以触发某一个事件的动作(不止一个)都已经封装到了监听器中。


以上三者,在java中已经定义好了,直接获取来用就好。
我们要做的是对产生的动作进行处理


@@Frame f = new Frame("我的窗体");
f.addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent e)//关窗体时调用!
{
System.out.println("我关!");
System.exit(0);
}
public void windowActivated(WindowEvent e)//窗体前置时调用!
{
System.out.println("窗体前置!");
}
public void windowOpened(WindowEvent e)//窗体初次被打开时调用!
{
System.out.println("我被打开了,哈哈哈哈");
}
});
@@Button b = new Button("按钮");
//让按钮具备退出程序的功能。
/*
按钮就是事件源。
那么选择哪个监听器呢?
通过关闭窗体实例了解到,想要知道哪个组件具备什么样的特有监听器。
需要查看该组件对象的功能。
通过查阅button的描述,发现按钮支持一个特有监听addActionListener
*/
but.addActionListener(new ActionListener()//3个不需适配器中的一个!
{
public void actionPerformed(ActionEvent e)
{
System.out.println("退出,按钮干的!");
System.exit(0);
}
});
@@MouseAndKeyEvent 共性事件 在java.awt.event.*;中
MouseEvent


 int getClickCount() 
          返回与此事件关联的鼠标单击次数。 


KeyEvent e


 char getKeyChar() 
          返回与此事件中的键关联的字符。 
 int getKeyCode() 
          返回与此事件中的键关联的整数 keyCode。
 static String getKeyText(int keyCode) 
          返回描述 keyCode 的 String,如 "HOME"、"F1" 或 "A"。 


 e.consume();//取消最近的一次键盘输入!


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(e.getKeyText(code)+"非法");
e.consume();//取消非法字符的输入!
}
}
});*/
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();
}
});
but2.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
d.setVisible(false);
}
});
d.addWindowListener(new WindowAdapter(){
public void windowClosing(Window e)
{
d.setVisible(false);
}
});
but.addMouseListener(new MouseAdapter()//鼠标监听
{
public void mouseClicked(MouseEvent e)//比活动监听先执行
{
showDir();
}

});
}
private void showDir()
{
String dirPath = tf.getText();
File dir = new File(dirPath);

if(dir.exists()&&dir.isDirectory())
{
ta.setText("");
String[] strs = dir.list();
for(String name : strs)
{
ta.append(name+"\r\n");
}
}
else
{
String info = "你输入的地址有误,请重新输入!";
lab.setText(info);
d.setVisible(true);
}
}
@@菜单  菜单栏添加菜单,菜单添加菜单项(菜单)。(菜单是菜单项的子类)
MenuBar 菜单栏
 Menu add(Menu m) 
          将指定的菜单添加到菜单栏。


 getMenu(int i) 
          获取指定的菜单。
 void setHelpMenu(Menu m) 
          将指定的菜单设置为此菜单栏的帮助菜单。 
 Menu getHelpMenu() 
          获取该菜单栏上的帮助菜单。
 void remove(int index)  
          从此菜单栏移除指定索引处的菜单。   


MenuItem菜单项(最终可执行的项目,点击后可执行,没有右箭头)
 void addActionListener(ActionListener l) 
          添加指定的动作侦听器,以从此菜单项接收动作事件。 
 
 void setEnabled(boolean b) 
          设置是否可以选择此菜单项。 
 void setLabel(String label) 
          将此菜单项的标签设置为指定标签。
 String getLabel() 
          获取此菜单项的标签。


Menu 菜单(是菜单项的子类) (不可执行的项目,有右箭头,点击后出现下一级菜单项或菜单!)
MenuItem add(MenuItem mi) 
将指定的菜单项添加到此菜单。 
void add(String label) 
将带有指定标签的项添加到此菜单。


MenuBar 类封装绑定到框架的菜单栏的平台概念。
为了将该菜单栏与 Frame 对象关联,可以调用该框架的 setMenuBar 方法。


 void setMenuBar(MenuBar mb) 
          将此窗体的菜单栏设置为指定的菜单栏。 
例如 
private MenuBar mb;//菜单栏
private Menu m_file,m_new;//二个菜单
private MenuItem mi_open,mi_save,mi_exit;//三个菜单项


m_new = new Menu("新建");
mi_open = new MenuItem("打开");
mi_save = new MenuItem("保存");
mi_exit = new MenuItem("退出");


m_file = new Menu("文件");
m_file.add(m_new);
m_file.add(mi_open);
m_file.add(mi_save);
m_file.add(mi_exit);


mb = new MenuBar();
f.setMenuBar(mb);


@@FileDialog文件对话框
FileDialog(Frame parent, String title, int mode) 
          创建一个具有指定标题的文件对话框窗口,用于加载或保存文件。
mode
--FileDialog.LOAD 打开(默认)
--FileDialog.SAVE 保存
 String getDirectory() 
          获取此文件对话框的目录。 
 String getFile() 
          获取此文件对话框的选定文件。 
 void setDirectory(String dir) 
          将此文件对话框窗口的目录设置为指定目录。 
 void setFile(String file) 
          将此文件对话框窗口的选定文件设置为指定文件。 
 void setFilenameFilter(FilenameFilter filter) 
          将此文件对话框窗口的文件名过滤器设置为指定的过滤器。 
 void setMode(int mode) 
          设置文件对话框的模式。 
@@简易记事本
package myNotepad;


import java.io.*;
import java.util.*;
import java.awt.*;
import java.awt.event.*;
class myNotepad
{
private Frame f ;
private Button but;
private TextField tf;
private TextArea ta;


private MenuBar mb;
private Menu m_file,m_new,m_edit,m_help;
private MenuItem mi_open,mi_save,mi_exit;


private FileDialog openDia,saveDia;
private File file;


private Dialog d;
private Label lab;
private Button but2;
private Scrollbar sbar;
myNotepad()
{
init();
}
public void init()
{
f = new Frame("我的窗体");


but = new Button("转到");
tf = new TextField(70);//列数
ta = new TextArea(25,70);
f.setBounds(700,400,600,600);
f.setLayout(new FlowLayout());

mi_open = new MenuItem("打开");
mi_save = new MenuItem("保存");
mi_exit = new MenuItem("退出");


m_file = new Menu("文件");
m_new = new Menu("新建");
m_file.add(m_new);
m_file.add(mi_open);
m_file.add(mi_save);
m_file.add(mi_exit);
m_edit = new Menu("编辑");
m_help = new Menu("帮助");
mb = new MenuBar();
mb.add(m_file);
mb.add(m_edit);
mb.add(m_help);


f.setMenuBar(mb);


openDia = new FileDialog(f,"打开",FileDialog.LOAD);
saveDia = new FileDialog(f,"另存为",FileDialog.SAVE);


d = new Dialog(f,"错误",false);
d.setBounds(800,500,240,150);
d.setLayout(new FlowLayout());
lab = new Label();
but2 = new Button("确定");

d.add(lab);
d.add(but2);


f.add(tf);
f.add(but);
f.add(ta);


myEvent();

f.setVisible(true);
}
private void save()
{
if(file==null)//如果已打开一个文件,则不用new新的文件
{
saveDia.setVisible(true);
String dirPath = saveDia.getDirectory();
String fileName = saveDia.getFile();
if(dirPath==null || fileName==null)
return ;
file = new File(dirPath,fileName);
tf.setText(dirPath+fileName);
}
try
{
BufferedWriter bufw = 
new BufferedWriter(new FileWriter(file));
String text = ta.getText();
bufw.write(text);
bufw.close();
}
catch (IOException ioe)
{
throw new RuntimeException("保存失败!");
}
}
private void open()
{
openDia.setVisible(true);
String dirPath = openDia.getDirectory();
String fileName = openDia.getFile();
if(dirPath==null || fileName==null)
return ;
ta.setText("");
file = new File(dirPath,fileName);
tf.setText(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 ioe)
{
throw new RuntimeException("文件打开失败!");
}
}
private void myEvent()
{
/**
实现打开菜单项的功能
*/
mi_open.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e)
{
open();
}
});
/**
实现保存菜单项的功能
*/
mi_save.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e)
{
save();
}
});
ta.addKeyListener(new KeyAdapter(){
public void keyPressed(KeyEvent e)
{
int code = e.getKeyCode();
if(e.isControlDown() && code==KeyEvent.VK_S)
save();
}
});
/**
实现退出菜单项的功能
*/
mi_exit.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e)
{
System.exit(0);
}
});
/**
实现窗体的退出功能
*/
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(e.getKeyText(code)+"非法");
e.consume();//取消非法字符的输入!
}
}
});*/
/**
在文本区域键入ENTER后,显示指定的目录列表
*/
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();
}
});
but2.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
d.setVisible(false);
}
});
d.addWindowListener(new WindowAdapter(){
public void windowClosing(Window e)
{
d.setVisible(false);
}
});
but.addMouseListener(new MouseAdapter()//鼠标监听
{
public void mouseClicked(MouseEvent e)//比活动监听先执行
{
showDir();
}

});
}
private void showDir()
{
String dirPath = tf.getText();
File dir = new File(dirPath);

if(dir.exists()&&dir.isDirectory())
{
ta.setText("");
String[] strs = dir.list();
for(String name : strs)
{
ta.append(name+"\r\n");
}
}
else
{
String info = "你输入的地址有误,请重新输入!";
lab.setText(info);
d.setVisible(true);
}
}
}
public class test 
{
public static void main(String[] args) throws Exception 
{
new myNotepad();
}
public static void sop(Object o)
{
System.out.println(o);
}
}

-------- android培训、java培训、期待与您交流! ----------

原创粉丝点击