JavaGUI技术总结。
一.GUI(图形化用户界面)
1.用来将文件或文件夹封装为对象。
2.方便对文件与文件夹进行属性的操作,从而弥补了流的不足(留不可以操作文件夹、文件的
GUIGraphicalUser Interface(图形用户接口)。用图形的方式,来显示计算机操作的界面,这样更方便更直观。
CLICommand line User Interface (命令行用户接口)就是常见的Dos命令行操作。需要记忆一些常用的命令,操作不直观。
举例:比如:创建文件夹,或者删除文件夹等。
Md file.rd file;
Java为GUI提供的对象都存在java.Awt和javax.Swing两个包中。
二.布局管理器。
布局管理器:
容器中的组件的排放方式,就是布.
常见的布局管理器:FlowLayout(流式布局管理器)从左到右的顺序排列。
Panel
默认的布局管理器。
BorderLayout(边界布局管理器)东,南,西,北,中
Frame
默认的布局管理器。
GridLayout(网格布局管理器)
规则的矩阵
CardLayout(卡片布局管理器)
选项卡
GridBagLayout(网格包布局非规则的矩.
三.建立一个简单的窗体。
事件监听。
1.事件原
2.事件
3.监听器
4.事件处理(窗口事件监听(关闭、最大化))
事件原:就是awt包或者swing包中的那些图像界面组件
事件:每一个事件原都有自己特有的对象事件和共性事件。
监听器:将可以触发某一个事件的动作(不止一个动作)都已将封装到了监听器中。
以上三者:在java中都已经定义好了。直接获取对象来用就可以了(封装了所有的事件,监听对比,反馈处理)。
4.窗体事件:
import java.awt.*;
import java.awt.event.*;
class GUIDemo
{
publicstatic void main(String[] args)
{
Framef =new Frame("my awt");//创建窗体对象
f.setSize(500,400);//设置窗体的属性。
f.setLocation(300,200);//设置窗口坐标系
f.setLayout(newFlowLayout());//设置布局
f.setTitle("helloworld");
Buttonb = new Button("new button");//创建按钮对象
f.add(b);//添加按钮对象。
f.addWindowListener(newMyWin());
f.setVisible(true);//设置窗体的属性为可见
}
}
class MyWin implements WindowListener
{
//覆盖7个方法。可以我只用到了关闭的动作。
//其他动作都没有用到,可是却必须复写。
}
因为WindowListener的子类WindowAdapter已经实现了WindowListener接口。
并覆盖了其中的所有方法。那么我只要继承自Windowadapter覆盖我需要的方法即可。
class MyWin extends WindowAdapter
{
publicvoid windowClosing(WindowEvent e)
{
System.out.println("windowclosing---"+e.toString());
System.exit(0);
}
}
练习,定义一个窗体,在窗体中添加一个按钮具备关闭该窗体的功能。
创建界面的步骤1.创建窗体对象。
2.设置对象(窗体)。
3.设置布局(setLayout(newFlowLat))
4.添加安组件
5.添加事件监听器(addWindowListener(new继承自WindowAdapter的事件类对象(七种事件(开,关))))
6.设置可见。
import java.awt.*;
import java.awt.event.*;
class GUIDemoTest
{ publicstatic void main (String[] args)
{
Framef = new Frame("newFrame");
f.setSize(300,300);
f.setLocation(500,500);
f.setLayout(newFlowLayout());
Buttonb =new Button("ok");
f.add(b);
f.addWindowListener(newMyWin());
f.setVisible(true);
}
}
class MyWin extends WindowAdapter
{
publicvoid windowClosing(WindowEvent e)
{
System.out.println("windowclosing---"+e.toString());
System.exit(0);
}
publicvoid windowActivated(WindowEvent e)
{
System.out.println("windowclosing---"+e.toString());
}
publicvoid windowOpened(WindowEvent e)
{
System.out.println("windowclosing---"+e.toString());
}
}*/
/*
优化写法:(*)课后练习
让按钮具备退出程序的功能
选择监听器:
通过关闭窗体实例了解到:
想要知道那个组件具备什么样的特有监听器。
需要查看该组件对象的功能。监听器封装于button中(addActionListener())查API。
button.addActionListener(new ActionLister()
{
publicvoid actionPerformed(ActionEvent e)
{
System.exit(0);
}
});
import java.awt.*;
import java.awt.event.*;
class GUIDemo2
{publicstatic void main(String[] args)
{
Framef = new Frame("hello");
f.setLayout(newFlowLayout());
f.setSize(300,300);
Buttonb = new Button();
TextFieldtf = new TextField(20);
f.add(tf);
f.add(b);
b.addActionListener(newActionListener()
{
publicvoid actionPerformed(ActionEvent e)
{
//System.exit(0);
}
});
b.addMouseListener(newMouseAdapter()
{
publicvoid mouseClicked(MouseEvent e)
{ if(e.getClickCount() == 2)
System.out.println("click");
}
});
b.addKeyListener(newKeyAdapter()
{
publicvoid keyPressed(KeyEvent e)//查键盘事件对象
{
System.out.println(e.getKeyChar()+""+e.getKeyCode());
if(e.isControlDown()&&e.getKeyCode()== KeyEvent.VK_ENTER)
System.exit(0);
}
});
tf.addKeyListener(newKeyAdapter()
{
publicvoid keyPressed(KeyEvent e)
{
intcode = e.getKeyCode();
if(!(code>=KeyEvent.VK_0&&code<=KeyEvent.VK_9))
{
System.out.println("");
e.consume();
}
}
});
f.setVisible(true);
}
}
四.创建对话框。
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.util.*;
class GUIDemo3
{
publicstatic void main(String[] args)
{
DrawGUIgui = new DrawGUI();
gui.drawFrame();
}
}
class DrawGUI
{
privateFrame f;
privateButton b;
privateTextField tf;
privateTextArea tx;
privateStringBuffer[] str;
privateDialog d;
privateLabel lab;
privateButton okButton;
privateString str1;
publicvoid drawFrame()
{
f= new Frame("窗体");
f.setSize(300,500);
f.setBounds(300,100,600,500);
f.setLayout(newFlowLayout());
f.setLocation(500,500);
tf= new TextField(20);
b= new Button("获取");
tx= new TextArea(25,70);
d= new Dialog(f,"self",true);//对话框窗体,事件触发后才出现。
lab= new Label("fault");
okButton= new Button("确定");
d.add(lab);
d.add(okButton);
d.setBounds(400,200,240,150);
d.setLayout(newFlowLayout());
f.add(b);
f.add(tf);
f.add(tx);
events();
f.setVisible(true);
}
publicvoid events()
{
f.addWindowListener(newWindowAdapter()
{
publicvoid windowClosing(WindowEvent e)
{
System.exit(0);
}
});
b.addMouseListener(newMouseAdapter()//在创建按钮后使用否则出现异常
{
publicvoid mouseClicked(MouseEvent a)
{
str1= tf.getText();
System.out.println(str1);
//str= str1;
//tx.setRows(1000);
tx.setText(str1);
readFile(str1);
tf.setText("");
}
});
tf.addKeyListener(newKeyAdapter()
{
publicvoid keyPressed(KeyEvent k)
{ str1 = tf.getText();
if(k.getKeyCode()== KeyEvent.VK_ENTER)
readFile(str1);
}
});
okButton.addMouseListener(newMouseAdapter()//在创建按钮后使用否则出现异常
{
publicvoid mouseClicked(MouseEvent a)
{
d.setVisible(false);
}
});
}
publicvoid readFile(String str2)
{
StringBuildersb = new StringBuilder();
tx.setText("");
getFileName(newFile(str2));
}
publicvoid getFileName(File fe)
{ if(!fe.exists())
{
lab.setText("信息错误");
d.setVisible(true);
}
File[]names = fe.listFiles();
for(File name : names)
{
if(name.isFile())
tx.append(name.getAbsolutePath()+""+name.getName()+"\r\n");
else
getFileName(name);
}
}
}
五.编写一个Notepad。
1.具有打开文件功能。2.具有存储文本文件的功能。3.实现菜单栏的效果。
import java.awt.*;
import java.awt.event.*;
import java.io.*;
class TextDemo
{
publicstatic void main(String[] args)
{
DrawTextgui = new DrawText();
gui.drawFrame();
}
}
class DrawText
{
privateFrame f;
privateMenuBar mb;
privateMenu m;
privateMenuItem open,save,exit;
privateFileDialog openDial,saveDial;
privateTextArea text;
privateFile saveFile;
//privateMenuItem mi,subItem;
publicvoid drawFrame()
{
f= new Frame("窗体");
f.setSize(300,500);
f.setBounds(300,100,600,500);
//f.setLayout(newFlowLayout());
f.setLocation(500,500);
//mb= new MenuBar();
//mf= new Menu("文件");
//mz= new Menu("子菜单");
//mi= new MenuItem("退出");
//subItem= new MenuItem("子条目");
//mz.add(subItem);
//mf.add(mz);
//mf.add(mi);
//mb.add(mf);
text= new TextArea();
openDial= new FileDialog(f,"open",FileDialog.LOAD);
saveDial= new FileDialog(f,"save",FileDialog.SAVE);
m= new Menu("文件");
open= new MenuItem("打开文件");
save= new MenuItem("保存文件");
exit= new MenuItem("退出");
m.add(open);
m.add(save);
m.add(exit);
mb= new MenuBar();
mb.add(m);
f.setMenuBar(mb);//设置菜单栏到窗体.
f.add(text);
events();
f.setVisible(true);
}
/*publicvoid readFile (File get)
{
try
{
BufferedReaderbfr = new BufferedReader(new FileReader(get));
Stringline = null;
while((line= bfr.readLine())!=null)
{ System.out.println(line);
text.append(line+"\r\n");
}
bfr.close();
}
catch(IOException e)
{
thrownew RuntimeException("读取失败");
}
}*/
publicvoid events()
{
f.addWindowListener(newWindowAdapter()
{
publicvoid windowClosing(WindowEvent e)
{
System.exit(0);
}
});
exit.addActionListener(newActionListener()
{
publicvoid actionPerformed(ActionEvent e)
{
System.exit(0);
}
});
open.addActionListener(newActionListener()
{
publicvoid actionPerformed(ActionEvent e)
{
openDial.setVisible(true);//写在获取路径的前边只有打开了选择框才能获取路径
Stringdir = openDial.getDirectory();//openDial是对话框对象,获取选取的文件的路径。
Stringname = openDial.getFile();
if(dir== null&&name == null)
return;
FilefileName = new File(dir,name);
//readFile(fileName);
try
{
BufferedReaderbfr = new BufferedReader(new FileReader(fileName));
Stringline = null;
while((line= bfr.readLine())!=null)
{ System.out.println(line);
text.append(line+"\r\n");
}
bfr.close();
}
catch(IOException g)
{
throw new RuntimeException("读取失败");
}
}
});
save.addActionListener(newActionListener()
{
publicvoid actionPerformed(ActionEvent e)
{
//if(saveFile== null)
//{
saveDial.setVisible(true);
Stringdir = saveDial.getDirectory();//openDial是对话框对象,获取选取的文件的路径。
Stringname = saveDial.getFile();
if(dir== null&&name == null)//假如没选择某文件
return;//返回
FilesaveFile = new File(dir,name);
//}//readFile(fileName);
try
{
BufferedWriterbfw = new BufferedWriter(new FileWriter(saveFile));
Stringline = null;
Stringtxt = text.getText();
bfw.write(txt);
bfw.close();
}
catch(IOException k)
{
thrownew RuntimeException("读取失败");
}
}