最简单的 BorderLayout布局例子:

最简单的 BorderLayout布局例子:

import javax.swing.JFrame;

import javax.swing.JButton;
import java.awt.Container;
import java.awt.BorderLayout;

public class TopLevJF{
public static void main(String[] args){
JFrame jf = new JFrame();
Container containtPane = jf.getContentPane();
containtPane.add(new JButton("North"),BorderLayout.NORTH);
containtPane.add(new JButton("Sourth"),BorderLayout.SOUTH);
containtPane.add(new JButton("CENTER"),BorderLayout.CENTER);
containtPane.add(new JButton("East"),BorderLayout.EAST);
containtPane.add(new JButton("North"),BorderLayout.WEST);
jf.setSize(300,200);
jf.setVisible(true);
jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}

最简单的从键盘读入并输出的例子;
 import java.io.InputStream;
import java.io.IOException;

public class STream{
public static void main(String[] args){
byte[] b = new byte[10]; 
try{
System.in.read(b);
}
catch(IOException e){
e.printStackTrace();
}
for(int i= 0;i
System.out.println(b[i]);
}
}
最简单的从硬盘读取文件并输出的例子:
 import java.io.IOException;
import java.io.FileInputStream;
import java.io.FileNotFoundException;

public class ReadFile{
public static void main(String[] args){
try{
FileInputStream fi = new FileInputStream("D:\\work\\java_workspace\\haha.txt");
int i;

//写代码一定要讲究灵活性:

//while(i=fi.read() != valited){
//System.out.println(b[i++]);
/*while((i=fi.read()).toString().equals(valited.toString())){
System.out.println(i);*/
try{
i=fi.read();
while(i != -1){
System.out.print((char)i);
i = fi.read();
}
}catch(IOException e){
e.printStackTrace();
}

}catch(FileNotFoundException e){
e.printStackTrace();
}
}
}
最简单的硬盘文件拷贝:
 import java.io.IOException;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;

public class ReadFile{
public static void main(String[] args){
try{
FileInputStream fi = new FileInputStream("C:\\work\\java_workspace\\haha.txt");
FileOutputStream fos = new FileOutputStream("C:\\work\\java_workspace\\haha_copy.txt");
int i;
//while(i=fi.read() != valited){
//System.out.println(b[i++]);
/*while((i=fi.read()).toString().equals(valited.toString())){
System.out.println(i);*/
try{
i=fi.read();
while(i != -1){
System.out.print((char)i);
fos.write((char)i);
i = fi.read();
}
}catch(IOException e){
e.printStackTrace();
}

}catch(FileNotFoundException e){
e.printStackTrace();
}
}
}

你可能感兴趣的:(java)