- 论坛徽章:
- 0
|
//import java.awt.BorderLayout;
import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.Image;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.io.RandomAccessFile;
import java.net.URL;
import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextArea;
import javax.swing.JScrollPane;
import javax.swing.filechooser.FileFilter;
/*
* Created on 2005-4-6
*
* To change the template for this generated file go to
* Window&references&Java&Code Generation&Code and Comments
*/
/**
* @author Administrator
*
* To change the template for this generated type comment go to
* Window&references&Java&Code Generation&Code and Comments
*/
public class Texttest
{
public static void main(String[] args)
{
TextFrame frame = new TextFrame();
//frame.pack();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.show();
}
}
class TextFrame extends JFrame
{
public TextFrame()
{
setTitle("TextTest"
setSize(400, 300);
JPanel panel = new JPanel();
text = new JTextArea(30,56);
text.setLineWrap(true);
JScrollPane scrollPane = new JScrollPane(text);
Container content = getContentPane();
content.add(panel,BorderLayout.NORTH);
content.add(scrollPane,BorderLayout.CENTER);
initMenuBar();
initToolBar();
Image image = getFrameIconifiedImage("server.gif"
if (image != null)
setIconImage(image); // doesn't change dialog icon
pack();
}
public void setText(String file)
{
String line = null;
String textcontent = null;
try
{
BufferedReader br = new BufferedReader(new FileReader(file));
while ((line = br.readLine()) != null)
{
text.append(line + "\n"
}
br.close();
}
catch (Exception e)
{
e.printStackTrace();
}
}
class SysAction implements java.awt.event.ActionListener
{
/* (non-Javadoc)
* @see java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent)
*/
public void actionPerformed(ActionEvent e)
{
// TODO Auto-generated method stub
Object obj = e.getSource();
if ( openbtn == obj )
{
openFile();
}
else if ( clearbtn == obj )
{
closeFile();
}
else if ( saveasbtn == obj )
{
saveFile();
}
}
}
class MenuAction implements ActionListener
{
/* (non-Javadoc)
* @see java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent)
*/
public void actionPerformed(ActionEvent e)
{
// TODO Auto-generated method stub
Object obj = e.getSource();
if ( obj == openmenu )
{
openFile();
}
else if ( obj == clearmenu )
{
closeFile();
}
else if ( obj == saveasmenu )
{
saveFile();
}
}
}
public void openFile()
{
//int ret = chooser.showOpenDialog(TextFrame.this);
//System.out.println(ret);
chooser.setCurrentDirectory(new File(System.getProperty("user.dir" + File.separator + "cfgsavset");
//ExtensionFileFilter filter = new ExtensionFileFilter(".txt","opentextfile"
//chooser.addChoosableFileFilter(filter);
int ret = chooser.showOpenDialog(TextFrame.this);
if (ret != JFileChooser.APPROVE_OPTION)
{
// cancel.
System.out.println("Failed!"
}
String dir = chooser.getCurrentDirectory().getPath();
System.out.println(dir);
String fileName = chooser.getSelectedFile().getName();
System.out.println(fileName);
System.out.println(dir + fileName);
String finalfile = dir + "\\" + fileName;
System.out.println(finalfile);
setText(finalfile);
/*
int i = fileName.lastIndexOf('.');
if ( i == -1 ) {
// append .cfg
fileName += ".txt"; //$NON-NLS-1$
} else if ((i+1) == fileName.length() ) {
// append cfg (dot is already there)
fileName += ".txt"; //$NON-NLS-1$
}
*/
}
public void closeFile()
{
text.setText(""
}
public void saveFile()
{
text.getText();
//String dir = System.getProperty("user.dir"
int ret = chooser.showSaveDialog(TextFrame.this);
if (ret != JFileChooser.APPROVE_OPTION)
{
// cancel.
System.out.println("Failed!"
}
String dir = chooser.getCurrentDirectory().getPath();
String fileName = chooser.getSelectedFile().getName();
String outfile = dir + "\\" + fileName;
System.out.println(outfile);
try
{
RandomAccessFile of = new RandomAccessFile(outfile, "rw");
of.writeBytes(text.getText());
of.close();
}
catch(IOException e)
{
e.printStackTrace();
}
}
private void initMenuBar()
{
MenuAction amenuaction = new MenuAction();
filemenu.setText("File");
filemenu.setMnemonic('F');
mainmenubar.add(filemenu);
openmenu.setText("Open");
openmenu.setMnemonic('O');
openmenu.addActionListener(amenuaction);
filemenu.add(openmenu);
saveasmenu.setText("Save As");
saveasmenu.setMnemonic('S');
saveasmenu.addActionListener(amenuaction);
filemenu.add(saveasmenu);
clearmenu.setText("Clear");
clearmenu.setMnemonic('C');
clearmenu.addActionListener(amenuaction);
filemenu.add(clearmenu);
exitmenu.setText("Exit");
exitmenu.setMnemonic('E');
exitmenu.addActionListener(amenuaction);
filemenu.add(exitmenu);
setJMenuBar(mainmenubar);
}
private void initToolBar()
{
toolbar.setFloatable(false);
getContentPane().add(BorderLayout.NORTH, toolbar);
SysAction aSysAction = new SysAction();
openbtn.setToolTipText("Open");
openbtn.addActionListener(aSysAction);
openbtn.setIcon(new javax.swing.ImageIcon(getURLImagePath("print.gif")));
toolbar.add(openbtn);
clearbtn.setToolTipText("Clear");
clearbtn.addActionListener(aSysAction);
toolbar.add(clearbtn);
saveasbtn.setToolTipText("Save As");
saveasbtn.addActionListener(aSysAction);
toolbar.add(saveasbtn);
}
/* if you need, you can use ToolKit to load this images. */
public static Image getFrameIconifiedImage(String filename)
{
Toolkit toolKit = Toolkit.getDefaultToolkit();
return toolKit.getImage(getURLImagePath(filename));
}
private static Class muteClass = null;
static
{
try
{
muteClass = Class.forName("Texttest");//(new rsimage()).getClass(); //$NON-NLS-1$
}
catch (ClassNotFoundException e)
{
muteClass = null;
}
}
public static URL getURLImagePath(String filename)
{
URL Location = muteClass.getResource("/images/" + "/" + filename);//("images/" + filename); //$NON-NLS-1$
if (Location == null)
{
String err = "Can not found file: " + filename; //$NON-NLS-1$
//new rsexception(err);
System.out.println(err);
}
return(Location);
}
javax.swing.JMenuBar mainmenubar = new javax.swing.JMenuBar();
javax.swing.JMenu filemenu = new javax.swing.JMenu();
javax.swing.JMenuItem openmenu = new javax.swing.JMenuItem();
javax.swing.JMenuItem clearmenu = new javax.swing.JMenuItem();
javax.swing.JMenuItem saveasmenu = new javax.swing.JMenuItem();
javax.swing.JMenuItem exitmenu = new javax.swing.JMenuItem();
javax.swing.JToolBar toolbar = new javax.swing.JToolBar();
javax.swing.JToolBar.Separator separator = new javax.swing.JToolBar.Separator();
private JButton openbtn = new JButton();
private JButton clearbtn = new JButton();
private JButton saveasbtn = new JButton();
private JFileChooser chooser = new JFileChooser();
private JTextArea text;
}
class ExtensionFileFilter extends FileFilter
{
String Extension;
String Description;
/* the construct accept some ext such cfg, txt*/
public ExtensionFileFilter(String Ext, String Descript)
{
Extension = Ext;
Description = Descript;
System.out.println(Extension + " " + Description);
}
public boolean accept(File f)
{
boolean acceptit = f.isDirectory();
if (!acceptit) {
// is file
String suffix = getSuffix(f);
if (suffix != null) {
acceptit = suffix.equalsIgnoreCase(Extension);
}
}
return(acceptit);
}
public String getDescription()
{
return Description;
}
private String getSuffix(File f)
{
String s = f.getPath();
String suffix = null;
int i = s.lastIndexOf('.');
if (i >; 0 && i < s.length() - 1) {
suffix = s.substring(i + 1);
}
return suffix;
}
} |
|