java常用类和方法 下载本文

内容发布更新时间 : 2024/5/24 14:02:23星期一 下面是文章的全部内容请认真阅读。

实用

MenuItem(String s) 构造标题是s的菜单项 setEnabled(boolean b) 设置是否可以被选择 getLabel() 得到菜单选项名 addActionListener() 添加监视器 5、有关菜单的技巧

addSeparator() 增加菜单分割线 CheckboxMenuItem() 复选框菜单项

setShortcut(MenuShortcut k) 设置快捷键(k取值KeyEvent.VK_A----KeyEvent.VK_Z)

建立对话框 1、Dialog类

Dialog(Frame f,String s) 构造对话框,初始不可见,s是标题,f是对话框所依赖的窗口 Dialog(Frame f,String s,boolean b) b设置初始是否可见 getTitle() 获取对话框标题

setTitle(String s) 设置对话框标题 setModal(boolean b) 设置对话框模式 setSize(int w,int h) 设置对话框大小 setVisible(boolean b) 显示或隐藏对话框 2、FileDialog类

Filedialog(Frame f,String s,int mode) mode的值是fileDialog.LOAD或者fileDialog.SAVE public String getDirectory() 获取当前文件对话框中显示的文件所属目录

public String getFile() 获取当前文件对话框中文件的字符串表示,不存在返回null

Java中的鼠标和键盘事件

1、使用MouseListener借口处理鼠标事件

鼠标事件有5种:按下鼠标键,释放鼠标键,点击鼠标键,鼠标进入和鼠标退出 鼠标事件类型是MouseEvent,主要方法有: getX(),getY() 获取鼠标位置

getModifiers() 获取鼠标左键或者右键 getClickCount() 获取鼠标被点击的次数 getSource() 获取鼠标发生的事件源

事件源获得监视器的方法是addMouseListener(),移去监视器的方法是removeMouseListener()

处理事件源发生的时间的事件的接口是MouseListener 接口中有如下的方法 mousePressed(MouseEvent) 负责处理鼠标按下事件 mouseReleased(MouseEvent) 负责处理鼠标释放事件 mouseEntered(MouseEvent) 负责处理鼠标进入容器事件 mouseExited(MouseEvent) 负责处理鼠标离开事件 mouseClicked(MouseEvent) 负责处理点击事件 2、使用MouseMotionListener接口处理鼠标事件 事件源发生的鼠标事件有2种:拖动鼠标和鼠标移动 鼠标事件的类型是MouseEvent

事件源获得监视器的方法是addMouseMotionListener()

文档

实用

处理事件源发生的事件的接口是MouseMotionListener 接口中有如下的方法 mouseDragged() 负责处理鼠标拖动事件 mouseMoved() 负责处理鼠标移动事件 3、控制鼠标的指针形状

setCursor(Cursor.getPreddfinedCursor(Cursor.鼠标形状定义)) 鼠标形状定义见(书 P 210) 4、键盘事件

键盘事件源使用addKeyListener 方法获得监视器 键盘事件的接口是KeyListener 接口中有3个方法 public void keyPressed(KeyEvent e) 按下键盘按键 public void keyReleased(KeyEvent e) 释放键盘按键 public void keyTypde(KeyEvent e) 按下又释放键盘按键

Java多线程机制

1、Java的线程类与Runnable接口 Thread类

public Thread() 创建线程对象

public Thread(Runnable target) target 称为被创建线程的目标对象,负责实现Runnable接口 线程优先级

Thread类有三个有关线程优先级的静态常量:MIN_PRIORITY,MAX_PRIORITY,NORM_PRIORITY 新建线程将继承创建它的副相承的优先级,用户可以调用Thread类的setPriority(int a)来修改 a的取值:

Thread.MIN_PRIORITY,Thread.MAX_PRIORITY,Thread.NORM_PRIORITY 主要方法 启动线程 start() 定义线程操作 run() 使线程休眠 sleep()

sleep(int millsecond) 以毫秒为单位的休眠时间

sleep(int millsecond,int nanosecond) 以纳秒为单位的休眠时间 currentThread() 判断谁在占用CPU的线程 第二十章 输入输出流 1、FileInputStream类

FileInputStream(String name) 使用给定的文件名name创建一个FileInputStream对象 FileInputStream(File file) 使用File对象创建FileInpuStream对象 File类有两个常用方法: File(String s) s确定文件名字

File(String directory,String s) directory是文件目录

例如:

File f=new File(\

FileInputStream istream=new FileInputStream(f); 处理I/O异常

当出现I/O错误的时候,Java生成一个IOException(I/O异常)对象来表示这个错误的信号。 程序必须使用一个catch检测这个异常 例如:

文档

实用 try{

FileInputStream ins= new FileInputStream(\}

catch(IOException e){

System.out.println(\}

从输入流中读取字节

int read() 返回0~255之间一个整数,如果到输入流末尾,则返回-1 int read(byte b[]) 读取字节数组

int read(byte b[],int off,int len) off指定把数据存放在b中什么地方,len指定读取的最大字节数 关闭流 close()

2、FileOutputStream类

FileOutputStream(String name) 使用指定的文件名name创建FileOutputStream对象 FileOutputStream(File file) 使用file对象创建FileOutputStream对象

FileOutputStream(FileDescriptor fdobj) 使用FileDescriptor对象创建FileOutputStream对象 3、FileReader类和FileWriter类 FileReader(String filename)

FileWriter(String filename)

处理时需要FileNotFoundException异常 4、RandomAccessFile类

RandomAccessFile不同于FileInputStream和FileOutputStream,不是他们的子类

当我们想对一个文件进行读写操作的时候,创建一个指向该文件的RandomAccessFile流就可以了 RandomAccessFile类有两个构造方法:

RandomAccessFile(String name, String mode) name是文件名,mode取r(只读)或rw(读写) RandomAccessFile(File file,String mode) file给出创建流的源

seek(long a) 移动RandomAccessFile流指向文件的指针,a确定指针距文件开头的位置 getFilePointer() 获取当前文件的指针位置 close() 关闭文件

getFD() 获取文件的FileDescriptor length() 获取文件长度 read() 读取一个字节数据 readBoolean() 读取一个布尔值 readByte() 读取一个字节 readChar() readFloat() readFully(byte b[]) readInt() readLine() readLong()

readUnsignedShort()

readUTF() 读取一个UTF字符串

setLength(long newLength) 设置文件长度

文档

实用

skipByte(int n) 在文件中跳过给定数量的字节 write(byte b[]) 写b.length个字节到文件 writeBoolean(bolean b) writeByte(int v) writeChar(char c) writeChars(String s) writeDouble(double d) writeFloat(float v) writeInt(int i) writeLong(long l) writeShort(int i) writeUTF(String s) 5、管道流

PipedInputStream类

PipedInputStream() 创建一个管道输入流

PipedInputStream(PipedOutputStream a) 连接到输出流a的输入流 read() 从输入流中读取一个字节

read(byte b[],int off,int len) off是在b中的开始位置,len是字节长度 PipedOutputStream类

PipedOutputStream() 创建一个输出流

PipedOutputStream(PipedInputStream a) 连接到输入流a的输出流 write(int b)

write(byte b[],int off,int len) counnect() 连接输入输出流 close() 关闭流

在使用的时候要捕获IOException异常。 6、数据流

DataInputStream类(数据输入流)

DataInputStream(InputStream in) 将数据输入流指向一个由in指定的输入流 DataOutputStream类(数据输出流)

DataOutputStream(OutputStream out) 将数据输出流指向一个由out指定的输出流 主要方法: close()

read() 读取一个字节数据 readBoolean() 读取一个布尔值 readByte() 读取一个字节 readChar() readFloat() readFully(byte b[]) readInt() readLine() readLong()

readUnsignedShort()

readUTF() 读取一个UTF字符串

文档