GUI
- GUI
- Graphical User Interface(图形用户接口).
- 用图形的方式,来显示计算机操作的界面,这样更方便更只管
- CLI
- Command line User Interface (命令行用户接口)
- 就是常见的Dos命令行操作
- 需要记忆一些常用的命令,操作不只管
- 举例:
- 比如:创建文件夹,或者删除文件夹等
- Java为GUI提供的对象都存在java.Awt 和 javax.Swing两个包中
Awt 与 Swing
java.Awt:Abstract Window ToolKit(抽象窗口工具包),需要调用本地系统方法实现功能。属重量级空间。
javax.Swing:在AWT的基础上,建立的一套图形界面系统,其中提供了更多的组建,而且完全由Java实现。增强了移植性,属轻量级控件。
布局管理器
- 容器中的组建的排放方式,就是布局
- 常见的布局管理器
- FlowLayout(流式布局管理器)
- 从左到右的顺序排列
- Panel默认的布局管理器
- BorderLayout(边界布局管理器)
- 东,南,西,北,中
- Frame默认的布局管理器。
- GridLayout(网格布局管理器)
- 规则的矩阵
- CardLayout(卡片布局管理器)
- 选项卡
- GridBagLayout(网格包布局管理器)
- 非规则的矩阵
Frame
import java.awt.*;
class GuiDemo
{
public static void main(String[] args)
{
Frame fm=new Frame("Title");
fm.setSize(400,300);
fm.setLocation(200,200);
Button b1=new Button("登录");
Button b2=new Button("注册");
fm.setLayout(new FlowLayout());
fm.add(b1);
fm.add(b2);
fm.setVisible(true);
}
}
事件监听机制
- 事件源(组件)
- 事件(Event)
- 监听器(Listener)
- 事件处理(引发事件后处理方式)
窗体事件
fm.addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{
System.exit(0);
}
public void windowActivated(WindowEvent e)
{
System.out.println("windowActivated");
}
public void windowOpened(WindowEvent e)
{
System.out.println("windowOpened");
}
});
Action事件
import java.awt.*;
import java.awt.event.*;
class GuiDemo2
{
private Frame f;
private Button but;
GuiDemo2()
{
init();
}
public void init()
{
f=new Frame("Frame");
f.setBounds(300,100,600,500);
f.setLayout(new FlowLayout());
but=new Button("button");
f.add(but);
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.out.println(e.toString());
}
});
}
public static void main(String[] args)
{
new GuiDemo2();
}
}
鼠标事件
import java.awt.*;
import java.awt.event.*;
class GuiDemo3
{
public static void main(String[] args)
{
Frame fm=new Frame("Frame");
fm.setBounds(300,300,400,300);
fm.addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e)
{
System.exit(0);
}
});
Button bt1=new Button("hello");
bt1.addMouseMotionListener(new MouseMotionAdapter()
{
public void mouseMoved(MouseEvent e)
{
Point pt1=e.getPoint();
Point pt2=e.getLocationOnScreen();
System.out.println("控件内部坐标X:"+pt1.x+",Y:"+pt1.y);
System.out.println("屏幕坐标 X:"+pt2.x+",Y:"+pt2.y);
//System.out.println(e.toString());
}
public void mouseDragged(MouseEvent e)
{
System.out.println("MouseDragged");
}
});
bt1.addMouseListener(new MouseAdapter()
{
public void mouseClicked(MouseEvent e)
{
bt1.setLabel("mouseClicked");
System.out.println("mouseClicked");
}
public void mouseEntered(MouseEvent e)
{
System.out.println("mouseEntered");
}
public void mouseExited(MouseEvent e)
{
System.out.println("mouseExited");
}
public void mousePressed(MouseEvent e)
{
System.out.println("mousePressed");
}
public void mouseReleased(MouseEvent e)
{
System.out.println("mouseReleased");
}
});
fm.setLayout(new FlowLayout());
fm.add(bt1);
fm.setVisible(true);
}
}
键盘事件
import java.awt.*;
import java.awt.event.*;
class GuiDemo4
{
public static void main(String[] args)
{
Frame fm=new Frame("Frame");
fm.setBounds(300,300,400,300);
fm.addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e)
{
System.exit(0);
}
});
fm.setLayout(new FlowLayout());
TextField tf1=new TextField(20);
tf1.addKeyListener(new KeyAdapter()
{
public void keyPressed(KeyEvent e)//屏蔽按键,只允许使用数字和退格键
{
int code=e.getKeyCode();
if(!((code>=KeyEvent.VK_0 && code<=KeyEvent.VK_9)||code==KeyEvent.VK_BACK_SPACE))
{
//System.out.println(code);
e.consume();
//System.out.println("非法的");
}
}
});
Button bt1=new Button("登录");
bt1.addKeyListener(new KeyAdapter()
{
public void keyPressed(KeyEvent e)
{
//System.out.println(e.getKeyText(e.getKeyCode()));
//System.out.println(e.getKeyChar()+" "+e.getKeyCode());
}
});
fm.add(tf1);
fm.add(bt1);
fm.setVisible(true);
}
}
简易文件查看器1.0
import java.awt.*;
import java.awt.event.*;
import java.io.File;
//
class FileMan
{
private Frame fm;
private TextField tf;
private Button btn;
private TextArea ta;
private List fileList;
private Choice rootCh;
private File root;
private Dialog dlg;
FileMan()
{
init();
}
public void init()
{
fm=new Frame("Frame");
fileList=new List();
rootCh=new Choice();
tf=new TextField();
dlg=new Dialog(fm,"Error");
fm.setBounds(300,100,400,600);
dlg.setBounds(350,350,200,100);
fm.setLayout(new BorderLayout());
//fm.setLayout(null);
//rootCh.setPreferredSize(new Dimension(100,40));
//fileList.setPreferredSize(new Dimension(300,300));
fm.add(rootCh,BorderLayout.NORTH);
fm.add(fileList,BorderLayout.CENTER);
fm.add(tf,BorderLayout.SOUTH);
dlg.add(new Label("盘符打开失败!"));
for(File f:File.listRoots())
{
rootCh.add(f.getPath());
}
tf.setText(rootCh.getSelectedItem());
showList(new File(tf.getText()).listFiles());
myEvent();
fm.setVisible(true);
}
public void showList(File[] flist)
{
if(flist==null)
dlg.setVisible(true);
for(int i=0;(flist!=null)&&(i<flist.length);i++)
{
fileList.add(flist[i].getName());
}
}
public void myEvent()
{
fm.addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e)
{
System.exit(0);
}
});
dlg.addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e)
{
dlg.setVisible(false);
}
});
rootCh.addItemListener(new ItemListener()
{
public void itemStateChanged(ItemEvent e)
{
String rootName=rootCh.getSelectedItem();
tf.setText(rootName);
fileList.removeAll();
File[] flist=new File(rootName).listFiles();
showList(flist);
}
});
fileList.addMouseListener(new MouseAdapter()
{
public void mouseClicked(MouseEvent e)
{
String fileName=fileList.getSelectedItem();
File dFile=new File(tf.getText()+File.separator+fileName);
if(dFile.isDirectory())
{
tf.setText(dFile.getPath());
fileList.removeAll();
showList(dFile.listFiles());
}
}
});
}
public static void main(String[] args)
{
new FileMan();
//gd.init();
}
}
菜单
MenuBar 菜单栏
Menu 菜单
MenuItem 菜单项
菜单栏 包含 菜单
菜单 包含 菜单 或 菜单项
import java.awt.*;
import java.awt.event.*;
class MenuDemo
{
private Frame f;
private MenuBar mb;
private Menu m,subMenu;
private MenuItem closeItem,subItem;
MenuDemo()
{
init();
}
public void init()
{
f=new Frame("Menu");
f.setBounds(300,100,500,600);
f.setLayout(new FlowLayout());
mb=new MenuBar();
m=new Menu("文件");
subMenu=new Menu("子菜单");
subItem=new MenuItem("子条目");
closeItem=new MenuItem("退出");
subMenu.add(subItem);
m.add(subMenu);
m.add(closeItem);
//mb.add(subMenu);
mb.add(m);
f.setMenuBar(mb);
myEvent();
f.setVisible(true);
}
public void myEvent()
{
closeItem.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e)
{
//System.out.println(e.toString());
System.exit(0);
}
});
f.addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e)
{
System.exit(0);
}
});
}
public static void main(String[] args)
{
new MenuDemo();
}
}
简易记事本
import java.awt.*;
import java.awt.event.*;
import java.io.*;
class MenuDemo
{
private Frame fm;
private MenuBar bar;
private Menu fileMenu;
private MenuItem openItem,closeItem,saveItem;
private FileDialog openDialog,saveDialog;
private TextArea ta;
//打开的文件
private File file;
MenuDemo()
{
init();
}
//初始化
public void init()
{
fm=new Frame("简易文本编辑器");
fm.setBounds(300,100,650,600);
//创建文本编辑框
ta=new TextArea();
//创建菜单栏
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);
//框架设置菜单栏
fm.setMenuBar(bar);
//框架添加文本框
fm.add(ta);
//创建文件对话框
openDialog=new FileDialog(fm,"打开",FileDialog.LOAD);
saveDialog=new FileDialog(fm,"保存",FileDialog.SAVE);
//初始化事件监视器
myEvent();
//显示框架
fm.setVisible(true);
}
//打开文件显示在文本框内
public void openFile()
{
//System.out.println(path);
String line=null;
BufferedReader bufr=null;
ta.setText("");
try
{
bufr=new BufferedReader(new FileReader(file));
line=bufr.readLine();
while(line!=null)
{
ta.append(line+System.getProperty("line.separator"));
line=bufr.readLine();
}
}
catch(IOException e)
{
throw new RuntimeException("文件读取失败");
}
finally
{
if(bufr!=null)
{
try{
bufr.close();
}
catch(IOException e)
{
throw new RuntimeException("文件关闭失败");
}
}
}
}
//保存文件到file对象
public void saveFile()
{
String text=null;
BufferedWriter bufw=null;
try{
bufw=new BufferedWriter(new FileWriter(file));
text=ta.getText();
bufw.write(text);
}
catch(IOException e)
{
throw new RuntimeException("写入文件失败");
}
finally
{
if(bufw!=null)
{
try{
bufw.close();
}
catch(IOException e)
{
throw new RuntimeException("关闭文件失败");
}
}
}
}
//事件监听器
public void myEvent()
{
//打开菜单事件
openItem.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e)
{
//System.out.println(e.toString());
openDialog.setVisible(true);
String dirPath=openDialog.getDirectory();
String fileName=openDialog.getFile();
if(dirPath!=null&&fileName!=null)
{
file=new File(dirPath,fileName);
openFile();
}
}
});
//保存菜单事件
saveItem.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e)
{
//System.out.println(e.toString());
if(file==null)
{
saveDialog.setVisible(true);
String dirPath=saveDialog.getDirectory();
String fileName=saveDialog.getFile();
if(dirPath!=null&&fileName!=null)
file=new File(dirPath,fileName);
else
return;
}
saveFile();
}
});
//关闭菜单事件
closeItem.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e)
{
//System.out.println(e.toString());
System.exit(0);
}
});
//窗口关闭事件
fm.addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e)
{
System.exit(0);
}
});
}
public static void main(String[] args)
{
new MenuDemo();
}
}
双击执行jar
1.java文件使用package指定包
2.主类使用 public 声明
3.将java文件编译
javac SimpleNotepad.java -d ./
4.编写配置文件config.txt 内容为
Main-Class: SimpleNotepad
注意,冒号’:’后必须加空格,行尾必须加回车!
5.使用jar -cvfm SimpleNotepad.jar config.txt ./包名 进行打包
6.双击执行jar包
jar格式在windows 7系统下执行配置方法
打开“控制面板\所有控制面板项\默认程序\设置关联”
找到 .jar格式 点击更改程序
选择JDK中的 javaw.exe