import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
import java.io.*;
import java.nio.file.Files;
public class text{
public static void main(String args[ ]){
new FirstWindow("文本查看器");
}
}
class FirstWindow extends JFrame implements ActionListener{
/**
*
*/
private static final long serialVersionUID = 1L;
JMenuBar menubar;
JMenu menu1,menu2,menu3,menu4,menu5;
JMenuItem item1,item2,item3,item4,item5,item6,item7,item8,item9,item10,item11;
JTextArea text=new JTextArea(); //文本框
long x=0,y[]=new long[100];
static int fontsize=15;
static int i=0;
String path;
FirstWindow(String s){
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel jp=new JPanel();//面板
this.setResizable(false);
JScrollPane jsp=new JScrollPane(text);//滚动条
GridLayout grid=new GridLayout(1,1);//面板布局
setTitle(s); //窗口标题
setSize(1000,615); //窗口大小
setLocation(200,80); //窗口位置
y[0]=0;
text.setLineWrap(true); //自动换行
jp.setBounds(0,0,980,550); //面板大小
jp.setLayout(grid); //面板获得布局
add(jp); //添加面板
jp.add(jsp); //面板添加滚动条
text.setEditable(true); //可编辑
setVisible(true); //窗口可视
menubar=new JMenuBar(); //菜单栏
setJMenuBar(menubar); //添加菜单栏
menu1=new JMenu("文件");
menu2=new JMenu("选项");
menu3=new JMenu("字体");
menu4=new JMenu("书签");
menu5=new JMenu("关于");
item1=new JMenuItem("打开");
item2=new JMenuItem("关闭");
item3=new JMenuItem("下一页");
item4=new JMenuItem("上一页");
item5=new JMenuItem("字体颜色");
item6=new JMenuItem("背景颜色");
item7=new JMenuItem("字体变大");
item8=new JMenuItem("字体变小");
item9=new JMenuItem("添加书签");
item10=new JMenuItem("读取书签");
item11=new JMenuItem("关于软件");
menu1.add(item1);
menu1.add(item2);
menu2.add(item3);
menu2.add(item4);
menu2.add(item5);
menu2.add(item6);
menu3.add(item7);
menu3.add(item8);
menu4.add(item9);
menu4.add(item10);
menu5.add(item11);
menubar.add(menu1);
menubar.add(menu2);
menubar.add(menu3);
menubar.add(menu4);
menubar.add(menu5);
item1.addActionListener(this);
item2.addActionListener(this);
item3.addActionListener(this);
item4.addActionListener(this);
item5.addActionListener(this);
item6.addActionListener(this);
item7.addActionListener(this);
item8.addActionListener(this);
item9.addActionListener(this);
item10.addActionListener(this);
item10.addActionListener(this);
item11.addActionListener(this);
}
public void actionPerformed(ActionEvent e){
if(e.getSource()==item1){
JFileChooser file = new JFileChooser ("*.txt");
int result=file.showOpenDialog(null);
if(result == JFileChooser.APPROVE_OPTION)
{
path= file.getSelectedFile().getAbsolutePath();
this.setTitle(path);
text.setText(" ");
new opens(path);
text.setEditable(false);
}
}
else if(e.getSource()==item3){
text.setText(" ");
new next(path);
}
else if(e.getSource()==item2){
System.exit(0);
}
else if(e.getSource()==item4){
text.setText(" ");
new up(path);
}
else if(e.getSource()==item5){
new color();
}
else if(e.getSource()==item6){
new colorback();
}
else if(e.getSource()==item7){
fontsize=fontsize+3;
new font(fontsize);
}
else if(e.getSource()==item8){
fontsize=fontsize-3;
new font(fontsize);
}
else if(e.getSource()==item9){
new addplace();
}
else if(e.getSource()==item10){
text.setText(" ");
new readplace();
}
else if(e.getSource()==item11){
new about();
}
}
class opens{
int n=0;
opens(String path){
try {
RandomAccessFile input=new RandomAccessFile(path,"r");
while(n<1000){
String str=input.readLine();
byte b[]=str.getBytes("iso-8859-1");
String content=new String(b);
text.append(content);
text.append("\n");
n=n+1;
item4.setEnabled(false);
x=input.getFilePointer();
}
i=1;
y[i]=x;
text.setCaretPosition(0);
} catch (FileNotFoundException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
}
class next{
next(String path){
try {int n=0;
RandomAccessFile input=new RandomAccessFile(path,"r");
input.seek(x);
while(n<1000){
String str=input.readLine();
byte b[]=str.getBytes("iso-8859-1");
String content=new String(b);
text.append(content);
text.append("\n");
n=n+1;
item4.setEnabled(true);
x=input.getFilePointer();
}
i+=1;
y[i]=x;
text.setCaretPosition(0);
} catch (FileNotFoundException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
}
class up{
up(String path){
try {int n=0;
RandomAccessFile input=new RandomAccessFile(path,"r");
input.seek(y[i-2]);
while(n<1000){
String str=input.readLine();
byte b[]=str.getBytes("iso-8859-1");
String content=new String(b);
text.append(content);
text.append("\n");
n=n+1;
x=input.getFilePointer();
}
i-=1;
text.setCaretPosition(0);
} catch (FileNotFoundException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
}
class turnto{
turnto(String path,long point){
try {int n=0;
RandomAccessFile input=new RandomAccessFile(path,"r");
input.seek(point);
while(n<1000){
String str=input.readLine();
byte b[]=str.getBytes("iso-8859-1");
String content=new String(b);
text.append(content);
text.append("\n");
n=n+1;
item4.setEnabled(false);
x=input.getFilePointer();
}
text.setCaretPosition(0);
} catch (FileNotFoundException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
}
class color extends JFrame implements ChangeListener{
JColorChooser tcc = new JColorChooser();
color(){
this.setBounds(200,200,500,400);
setVisible(true);
setTitle("选择颜色");
add(tcc, BorderLayout.PAGE_END);
tcc.getSelectionModel().addChangeListener(this);
}
@Override
public void stateChanged(ChangeEvent e) {
// TODO Auto-generated method stub
Color newForegroundColor = tcc.getColor();
text.setForeground(newForegroundColor);
}
}
class colorback extends JFrame implements ChangeListener{
JColorChooser tcc = new JColorChooser();
colorback(){
this.setBounds(200,200,500,400);
setVisible(true);
setTitle("选择颜色");
add(tcc, BorderLayout.PAGE_END);
tcc.getSelectionModel().addChangeListener(this);
}
@Override
public void stateChanged(ChangeEvent e) {
// TODO Auto-generated method stub
text.setForeground(Color.RED);
Color newForegroundColor = tcc.getColor();
text.setBackground(newForegroundColor);
}
}
class font {
font(int fontsize){
Font font = new Font("微软雅黑",0,fontsize);
text.setFont(font);
}
}
class setfont extends JFrame{
setfont(){
JFrame jf =new JFrame();
JPanel jp=new JPanel();
}
}
class addplace{
addplace(){
File f = new File("D:\\place.txt");
if (!f.exists())
{
try {
f.createNewFile();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
try {
FileOutputStream output=new FileOutputStream(f);
String outstring=String.valueOf(y[i-1]);
byte b[]=outstring.getBytes();
output.write(b);
} catch (FileNotFoundException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
class readplace{
long point;
readplace(){
try {
RandomAccessFile inputfile=new RandomAccessFile("D:\\place.txt","r");
String input=inputfile.readLine();
point=Long.parseLong(input);
new turnto(path,point);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
class about extends JFrame implements ActionListener{
about(){
this.setUndecorated(true);
GridLayout lay=new GridLayout(3,1);
this.setBounds(400, 100,200,200);
this.setVisible(true);
JPanel p=new JPanel();
p.setLayout(lay);
JLabel l1=new JLabel(" version:1.0");
JLabel l2=new JLabel(" by:poklau");
JButton button=new JButton("确定");
p.add(l1);
p.add(l2);
p.add(button);
add(p);
button.addActionListener(this);
}
public void actionPerformed(ActionEvent e){
this.dispose();
}
}
}
第一个java实用的应用程序,完成与2013年寒假。