您的位置:首页 > 职场人生

黑马程序员——Java基础-图形界面

2015-07-26 14:29 471 查看
------- android培训java培训、期待与您交流!
----------

图形化界面由各种组件构成,Component为组件的抽象超类,其下有Container(容器)、Button(按钮)、Label(标签)、Checkbox(选项框)、List(选项表)、Textcomponent(文本控件)等常用子类。这些子类中只有Container为容器组件,其他为控件。容器中能添加容器及控件,控件中不能添加容器或者控件。

容器添加容器或者控件时需要定义其布局方式,Frame默认的是BorderLayout布局管理器,Panel默认的是FlowLayout布局管理器,也可以通过setLayout()方法来定义布局。

流式布局:FlowLayout

    从左到右的顺序排列

    Panel默认的布局管理器

边界式布局:BorderLayout

    东、南、西、北、中

    Frame默认的布局管理器

网格布局:GridLayout

    规则的矩阵

卡片布局:CardLayout

    选项卡

网格包布局:GridBagLayout

    非规则的矩阵

除了界面之外,实现交互还需要事件监听器,能够监听用户的操作并做出反应。常用的监听器有ActionListener、WindowListener,这些监听器都是添加在组件上,用户操作组件就能促发相应事件。
一下为通过java编写的类似记事本的小程序
/*
带包编译
javac -d f:\ Mymenu.java
jar -cvfm my.jar 1.txt mymenu
//1.txt为配置信息Main-Class:,双击时告诉jar包中找哪个类执行
*/
package mymenu;
import java.awt.*;
import java.awt.event.*;
import java.io.*;
public class Mymenu
{
private Frame f;
private TextArea ta;
private MenuBar bar;
private Menu fileMenu;
private MenuItem openItem,saveItem,closeItem;
private FileDialog openDia,saveDia;
private File file;
Mymenu()
{
init();
}
public void init()
{
f=new Frame("my window");//新建框架容器
f.setBounds(300,200,650,600);//设置位置和大小
//Frame默认为BorderLayout可以通过设置改变布局
//f.setLayout(new FlowLayout());
ta=new TextArea();//新建文本区控件
f.add(ta);//将文本区控件添加到框架中
bar=new MenuBar();//新建菜单条
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);//将菜单条设置到框架中
//新建打开文件对话框,并调用Windows底层打开文件对话框
openDia=new FileDialog(f,"我要打开",FileDialog.LOAD);
//新建保存文件对话框,并调用Windows底层保存文件对话框
saveDia=new FileDialog(f,"我要保存",FileDialog.SAVE);
myEvent();//调用监听事件方法
f.setVisible(true);//将框架设置为可见
}
private void myEvent()
{
//在保存菜单子目录中添加监听器
saveItem.addActionListener(new ActionListener()
{
//复写监听器中的actionPerformed方法
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.close();
}
catch(IOException ee)
{
throw new RuntimeException("保存失败");
}

}
});
//在打开菜单子目录中添加监听器
openItem.addActionListener(new ActionListener()
{
//复写监听器中的actionPerformed方法
public void actionPerformed(ActionEvent e)
{
openDia.setVisible(true);//将打开文件对话框设置为可见
String dirPath=openDia.getDirectory();
String fileName=openDia.getFile();
if(dirPath==null||fileName==null)
return;
file=new File(dirPath,fileName);
ta.setText("");//将文本区控件初始化为空
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 ee)
{
throw new RuntimeException("读取失败");
}
}
});
//往框架中添加监听器
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 Mymenu();
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  java 图形界面