Java学习笔记(9输入输出+10图形用户界面)

Java学习笔记 CH9输入输出
1.输入:数据以程序为目的地
  输出:数据以程序为发源地
  
2.所属包:java.io.*


3.
InputStream:
int read(char[]cbuf/char []cbuf,int off,int len)           //读入流中
skip(long n)                                                 //跳过若干字节
available()                                                  //返回流中可用字节
mark(int readAHeadlimit)                                     //在流中标记一个位置
reset()                                                      //返回到标记的位置
markSupported()                                              //返回boolean型,描述流是否支持标记或者复位
close()                                                      //关闭流



OutputStream:
write(char []cbuf/char []cbuf,int off,int len/int c/String str)//写入文件
flush()                                                        //强制将被缓冲内容写到输出
close()                                                        //关闭流

4.例程:文件输出
import java.io.*;
public class Copy{
private void copy(InputStream in,OutputStream out)throws IOException{
byte[]buf = new byte[4096];
int len = in.read(buf);
while(len!=-1){
out.write(buf,0,len);
len = in.read(buf);
}
}
public void cat(String fsrc,String fdest){
try{
InputStream in = new FileInputStream(fsrc);
OutputStream out = new FileOutputStream(fdest,true);//true 表示追加;flase 表示从头开始写
copy(in,out);
out.close();
in.close();
}
catch(IOException e){System.err.println(e)}
}
}




5.内存输出
import java.io.*;
public class ByteArrayOutputStreamtest{
public static void mian(String []args){
try{
ByteArrayOutputStream baos = new ByteArrayOutputStream();
PrintStream ps = new PrintStream(baos);
for(int i = 0;i<1000;i++){
ps.println(i+"ABCD");
}
long start = System.currentTimeMillis();
FileOutputStream fos = new FileOutputStream("ByteArrayOutputStreamTest");
baos.writeTo(fos);
fos.close();
long stop = Sytem.currentTimeMillis();
System.out.println("time used: "+(stop-start));
}catch(FileNotFoundException fnfe){
System.err.println(fnfe.getMessage());
}catch(IOException ioe){
System.err.println(ioe.getMessage());
}
}
}




6.管道:一个线程输入是另一个线程输出,省却中间环节
class PipeExample{
public static void main(String[]args)throws IOException{
byte dataA = 123,dataB = 321;
PipedInputStream pis = new PipedInputStream();
PipedOutputStream pos = new PipedOutputStream(pis);//pos.connect(pis);//pis.connect(pos);
try{
pos.write(dataA);
pos.write(dataB);
System.out.println((byte)pis.read());//123
System.out.println((byte)pis.read());//65,因为321-256(byte周期)=65
}finally{
pis.close();
pos.close();
}
}
}




Java学习笔记 CH10图形界面
1.常见图形界面元素
基本组件:文本框、按钮、单选/多选按钮
容器:窗体,面板,框架


2.
AWT:java.awt.*;
基本组件:Label,Button,List,TextField
容器:Frame,Panel,Container


Swing:javax.swing.*;
基本组件:JLabel,JButton,JList,JTextField
容器:JFrame,JPanel


不能在容器中混用Swing和AWT组件


3.
JLabel              不可编辑文本行
JTextFiled          可编辑文本行
JButton             按钮        
JCheckBox           复选框
JRadioButton        单选框
JComboBox           下拉框
JPanel              面板
JFrame              框架


4.
import java.awt.*;
import javax.swing.*;


public class GUITest extends JFrame{
private JLabel label;
private JButton button;
private JCheckBox checkbox;
private JRadioButton rbutton;
private JTextFiled  textfiled;
private JComboBox cbox;
private JList list;

public GUITest(){
super("GUI test");
Container container = getConentPane();
container.setLayout = new FlowLayout();

label = new JLbel("Im JLabel");
button = new JButton("Im JButton");
rbutton = new JRadioButton("...");
textfield = new JTextField(10);
textfiled.seText("Im JTextFiled");
checkbox = new JCheckBox("Im JCheckBox");
cbox = new JComboBox();
cbox.add("JComboBox item1");
cbox.add("JComboBox item2");
list = new list();
String[] data = {"JList data1","JList data2","JList data3"};
list.setListData(data);
container.add(label);
container.add(list);
container.add(button);
container.add(checkbox);
container.add(rbutton);
container.add(textfield);
container.add(cbox);
setsize(300,200);
setVisiable(true);
}
public static void main(String [] args){
GUITest tst = new GUITest();
tst.setDefaultCloseOperation(JFrame_EXIT_ON_CLOSE);
}
}


5.事件处理:
事件源对象(按钮,文本)-》
事件对象(系统产生)-》
事件监听对象(处理程序)


程序员将时间监听对象注册到时间源对象,编写时间处理代码


6.
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class EventTest extends JFrame{
private JButton button;
public EventTest(){
super("Event Test");
Container container = getContentPanel();
button = new JButton("click me");
EventHandler handler = new EventHandler();
container.add(button);
setsize(200,100);
setVisible(true);
button.addActionListener(handler);
}

public class EventHandler implements ActionLister{
public void actionPerformed(ActionEvent event){
((JButton)event.getSource()).setText("Im Clicked");
} 
}

public static void main(String []args){
EventTest App = new EventTest();
App.setDefaultCloseOperation(JFrame_EXIT_ON_CLOSE);
} 
}



7. 适配器
addMouseListener(new MouseAdapter(){
mouseClicked(MouseEvent event){
//...
}
});




8.布局管理器
setLayout(new FlowLayout());
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值