GUI:Graphical User Interface 图形用户接口
CLI:Command line User Interface 命令行用户接口
Awt和Swing
java.Awt:Abstract Window ToolKit 需要调用本地系统方法实现功能。属于重量级控件
javax.Swing:在awt基础上,建立的一套图形界面系统,提供了更多组件。不依赖本地系统,属于轻量级控件。
布局:容器中组件的排放方式。
FlowLayout:流式布局
BorderLayout:边界布局
GridLayout:网格布局
CardLayout:卡片布局
GridBagLayout:网格包布局
创建图形化界面:
1.创建frame窗体
2.对窗体进行基本设置
比如大小,位置,布局
3.定义组件
4.将组件通过窗体的add方法添加到窗体中。
5.让窗体显示,通过setVisible(true)
事件监听机制的特点:
1.事件源:就是awt包或者swing包中的那些图形界面事件
2.事件:每个事件源都有自己特有的对应事件和共性事件
3.监听器:将可以触发某一个事件的动作都已经封装到了监听器中
4.事件处理:
鼠标和键盘监听事件:
列出文件夹目录:
菜单项以及打开保存文件:
CLI:Command line User Interface 命令行用户接口
Awt和Swing
java.Awt:Abstract Window ToolKit 需要调用本地系统方法实现功能。属于重量级控件
javax.Swing:在awt基础上,建立的一套图形界面系统,提供了更多组件。不依赖本地系统,属于轻量级控件。
布局:容器中组件的排放方式。
FlowLayout:流式布局
BorderLayout:边界布局
GridLayout:网格布局
CardLayout:卡片布局
GridBagLayout:网格包布局
![](https://img-my.csdn.net/uploads/201303/30/1364576053_2112.jpg)
创建图形化界面:
1.创建frame窗体
2.对窗体进行基本设置
比如大小,位置,布局
3.定义组件
4.将组件通过窗体的add方法添加到窗体中。
5.让窗体显示,通过setVisible(true)
事件监听机制的特点:
1.事件源:就是awt包或者swing包中的那些图形界面事件
2.事件:每个事件源都有自己特有的对应事件和共性事件
3.监听器:将可以触发某一个事件的动作都已经封装到了监听器中
4.事件处理:
鼠标和键盘监听事件:
import java.awt.*;
import java.awt.event.*;
class FrameDemo
{
private Frame f;
private Button but;
private TextField tf;
FrameDemo()
{
init();
}
public void init()
{
f = new Frame("frame");
f.setSize(300,200);
f.setLocation(500,500);
f.setLayout(new FlowLayout());
but = new Button("button");
tf = new TextField(10);
f.add(but);
f.add(tf);
myEvent();
f.setVisible(true);
}
private void myEvent()
{
f.addWindowListener(new WindowAdapter(){//窗口关闭事件
public void windowClosing(WindowEvent e)
{
System.exit(0);
}
});
but.addActionListener(new ActionListener(){//活动窗口事件
public void actionPerformed(ActionEvent e)
{
//System.exit(0);
System.out.println("action ");
}
});
but.addMouseListener(new MouseAdapter(){//鼠标监听事件 双击事件
public void mouseClicked(MouseEvent e)
{
if(e.getClickCount()==2)
System.out.println("双击动作");
}
});
but.addKeyListener(new KeyAdapter(){//按下ctrl+enter键 退出程序
public void keyPressed(KeyEvent e)
{
if(e.isControlDown() && e.getKeyChar()==KeyEvent.VK_ENTER)
{
System.out.println(KeyEvent.getKeyText(e.getKeyCode())+"...."+e.getKeyCode());
System.exit(0);
}
}
});
tf.addKeyListener(new KeyAdapter(){//只允许部分字符输入事件
public void keyPressed(KeyEvent e)
{
if(!(e.getKeyCode()>=KeyEvent.VK_0 && e.getKeyCode()<=KeyEvent.VK_9))
{
System.out.println(e.getKeyChar()+"...是非法的");
e.consume();
}
}
});
}
public static void main(String[] args)
{
new FrameDemo();
}
}
列出文件夹目录:
import java.awt.*;
import java.awt.event.*;
import java.io.*;
class WindowDemo
{
private Frame f;
private Button but;
private TextField tf;
private TextArea ta;
private Dialog d;
private Label lab;
private Button okbut;
WindowDemo()
{
init();
}
public void init()
{
f = new Frame("window");
f.setBounds(500,200,600,500);
f.setLayout(new FlowLayout());
tf = new TextField(50);
ta = new TextArea(25,70);
but = new Button("转到");
f.add(tf);
f.add(but);
f.add(ta);
d = new Dialog(f,"提示",true);
d.setBounds(600,300,300,100);
d.setLayout(new FlowLayout());
lab = new Label();
okbut = new Button("确定");
d.add(lab);
d.add(okbut);
myEvent();
f.setVisible(true);
}
public void myEvent()
{
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);
}
});
/**/
d.addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e)
{
d.setVisible(false);
}
});
okbut.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e)
{
d.setVisible(false);
}
});
}
public void showDir()
{
ta.setText("");
String dirPath = tf.getText();
File dir = new File(dirPath);
if(dir.exists() && dir.isDirectory())
{
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 WindowDemo();
}
}
菜单项以及打开保存文件:
import java.awt.*;
import java.awt.event.*;
import java.io.*;
class MenuBarDemo
{
private Frame f;
private MenuBar mb;
private Menu fileM,createM;
private MenuItem openMi,closeMi,saveMi,fileMi;
private TextArea ta;
private FileDialog openDialog,saveDialog;
private File file;
MenuBarDemo()
{
init();
}
public void init()
{
f = new Frame("window");
f.setBounds(500,150,700,700);
f.setLayout(new BorderLayout());
ta = new TextArea();
mb = new MenuBar();
fileM = new Menu("文件");
createM = new Menu("新建");
openMi = new MenuItem("打开");
closeMi = new MenuItem("退出");
saveMi = new MenuItem("保存");
fileMi = new MenuItem("文本文档");
mb.add(fileM);
fileM.add(createM);
fileM.add(openMi);
fileM.add(saveMi);
fileM.add(closeMi);
createM.add(fileMi);
f.setMenuBar(mb);
f.add(ta);
openDialog = new FileDialog(f,"打开",FileDialog.LOAD);
saveDialog = new FileDialog(f,"保存",FileDialog.SAVE);
myEvent();
f.setVisible(true);
}
public void myEvent()
{
f.addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e)
{
System.exit(0);
}
});
closeMi.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e)
{
System.exit(0);
}
});
openMi.addActionListener(new ActionListener(){//打开文件,并把文件内容输入到文本框
public void actionPerformed(ActionEvent e)
{
openDialog.setVisible(true);
String dirPath = openDialog.getDirectory();
String fileName = openDialog.getFile();
if(dirPath==null || fileName==null)//如果不作判断,选择文件取消后会发生异常
return;
ta.setText("");
file = new File(dirPath,fileName);
BufferedReader bufr = null;
try
{
bufr =
new BufferedReader(new FileReader(file));
String line = null;
while((line=bufr.readLine())!=null)
{
ta.append(line+"\r\n");
}
}
catch (IOException ex)
{
throw new RuntimeException("文件打开失败");
}
finally
{
try
{
if(bufr!=null)
bufr.close();
}
catch (IOException ioe)
{
throw new RuntimeException("资源关闭失败");
}
}
}
});
saveMi.addActionListener(new ActionListener(){//保存文件,需要判断是否要弹出对话框
public void actionPerformed(ActionEvent e)
{
if(file==null)//判断文件是否存在
{
saveDialog.setVisible(true);
String dirPath = saveDialog.getDirectory();
String fileName = saveDialog.getFile();
if(dirPath==null || fileName==null)
return;
file = new File(dirPath,fileName);
}
BufferedWriter bufw = null;
try
{
bufw = new BufferedWriter(new FileWriter(file));
String text = ta.getText();
bufw.write(text);
}
catch (IOException ex)
{
throw new RuntimeException("保存文件失败");
}
finally
{
try
{
if(bufw!=null)
bufw.close();
}
catch (IOException ioe)
{
throw new RuntimeException("关闭资源失败");
}
}
}
});
}
public static void main(String[] args)
{
new MenuBarDemo();
}
}