------- android培训、java培训、期待与您交流!
-----------------------------------------------------创建图形化界面--------------------------------------------
图形化界面里有两个包awt和swing。
创建图形化界面步骤:
2,对窗体进行基本设置。
比如大小,位置,布局。
3,定义组件。
4,将组件通过窗体的add方法添加到窗体中。
5,让窗体显示,通过setVisible(true)
常用图形布局:
FlowLayout流式布局
BorderLayout边界布局
GridLayout网格布局
CardLayout卡片布局
GridBagLayout网络包布局
事件监听机制的特点:
1,事件源。
2,事件。
3,监听器。
4,事件处理。
事件源:就是awt包或者swing包中的那些图形界面组件。
事件:每一个事件源都有自己特有的对应事件和共性事件。
监听器:将可以触发某一个事件的动作(不只一个动作)都已经封装到了监听器中。
以上三者,在java中都已经定义好了。
直接获取其对象来用就可以了。
我们要做的事情是,就是对产生的动作进行处理。
//代码演示
import java.awt.*;
import java.awt.event.*;
class AwtDemo
{
public static void main(String[] args)
{
Frame f = new Frame("my awt");
f.setSize(500,400);
f.setLocation(300,200);
f.setLayout(new FlowLayout());
Button b = new Button("我是一个按钮");
f.add(b);
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("我被打开了,hahahhahah");
}
});
f.setVisible(true);
//System.out.println("Hello World!");
}
}
/*
class MyWin implements WindowListener
{
//覆盖7个方法。可以我只用到了关闭的动作。
//其他动作都没有用到,可是却必须复写。
}
//因为WindowListener的子类WindowAdapter已经实现了WindowListener接口。
//并覆盖了其中的所有方法。那么我只要继承自Windowadapter覆盖我需要的方法即可。
class MyWin extends WindowAdapter
{
public void windowClosing(WindowEvent e)
{
//System.out.println("window closing---"+e.toString());
System.exit(0);
}
}
*/
</pre><pre code_snippet_id="362845" snippet_file_name="blog_20140525_3_8040723" name="code" class="java">简单模拟文本阅读器小程序
</pre><pre code_snippet_id="362845" snippet_file_name="blog_20140525_5_3438134" name="code" class="java"><pre name="code" class="java">import java.awt.*;
import java.awt.event.*;
import java.io.*;
public class MyText {
private Frame f;
private TextArea ta;
private Menu m,newFile;
private MenuBar mb;
private MenuItem closeItem,newText,newClass,open,save;
private File file;
public MyText(){
init();
}
public void init(){
f=new Frame("My Text");
f.setBounds(400,100,650,600);
ta=new TextArea();
m=new Menu("菜单");
newFile=new Menu("新建");
save=new MenuItem("保存");
open=new MenuItem("打开");
mb=new MenuBar();
closeItem=new MenuItem("退出");
newText=new MenuItem("新建文本文件");
newClass=new MenuItem("新建class文件");
m.add(newFile);
m.add(save);
m.add(open);
m.add(closeItem);
newFile.add(newText);
newFile.add(newClass);
mb.add(m);
f.setMenuBar(mb);
f.add(ta);
MyEvent();
f.setVisible(true);
}
public void MyEvent()
{
save.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
FileDialog fd=new FileDialog(f,"保存",FileDialog.SAVE);
if(file==null){
fd.setVisible(true);
String dir=fd.getDirectory();
String name=fd.getFile();
if(dir==null||name==null){
return;
}
file=new File(dir,name);
}
BufferedWriter bfw=null;
try{
bfw=new BufferedWriter(new FileWriter(file));
String text=ta.getText();
bfw.write(text);
}
catch(IOException ex){
throw new RuntimeException("写入失败");
}
finally{
try{
if(bfw!=null){
bfw.close();
}
}
catch(IOException ex){
throw new RuntimeException("写入失败");
}
}
}
});
open.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e){
FileDialog fd=new FileDialog(f,"打开",FileDialog.LOAD);
fd.setVisible(true);
String dir=fd.getDirectory();
String name=fd.getFile();
if(dir==null||name==null){
return;
}
ta.setText(null);
file=new File(dir,name);
BufferedReader bfr=null;
try{
bfr=new BufferedReader(new FileReader(file));
String line=null;
while((line=bfr.readLine())!=null){
ta.append(line+"\r\n");
}
}
catch(IOException i){
throw new RuntimeException("读取失败");
}
finally{
try{
if(bfr!=null)
bfr.close();
}
catch(IOException i){
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 MyText();
}
}
注意:要将程序打成可以双击执行的jar包,需要添加配置信息。
格式:Main-Class: myText.MyText+回车 (存入一个文本,名字后缀随便起)
然后在命令行打jar包时 jar -cvfm xxx.jar xx.txt 包名
xx.txt为配置信息文本