日期:2014-05-20 浏览次数:20967 次
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
import javax.swing.filechooser.FileFilter;
import javax.swing.text.DefaultStyledDocument;
import javax.swing.text.Document;
import javax.swing.text.StyledDocument;
import java.io.*;
import java.util.HashMap;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class TestFileDialog {
public static void main(String[] args) {
new FileDialog();
}
}
class FileDialog {
private File currentFile =null;
public static JTextPane display;
private JFrame frame=new JFrame("File");
private JButton openButton,deleteButton,redoButton,saveButton;
private Container container=frame.getContentPane();
private JPanel panel;
private boolean change;
private JScrollPane scroll;
private JMenuBar menuBar;
private JMenu fileMenu=new JMenu("文件");
private JLabel statusBar = new JLabel();
private JToolBar toolBar = new JToolBar();
private JMenuItem fileMenuItem,openMenuItem,exitMenuItem;
private StyledDocument styledDoc=new DefaultStyledDocument();
private Document doc;
private static Map<String,ImageIcon> iconsMap = new HashMap<String,ImageIcon>();
private static ImageIcon[] icons = null;
static {
icons =new ImageIcon[]{
new ImageIcon(TestFileDialog.class.getClassLoader().getResource("images/save.gif")),
new ImageIcon(TestFileDialog.class.getClassLoader().getResource("images/open.gif")),
new ImageIcon(TestFileDialog.class.getClassLoader().getResource("images/title.gif"))
};
iconsMap.put("open", icons[1]);
iconsMap.put("save",icons[0]);
iconsMap.put("title",icons[2]);
}
public FileDialog() {
/*
try{
UIManager.setLookAndFeel("com.sun.java.swing.plaf.motif.MotifLookAndFeel");
}catch(Exception e){
e.printStackTrace();
}
*/
frame.setSize(800,600);
frame.setLocation(400,100);
frame.setIconImage(Toolkit.getDefaultToolkit().getImage(TestFileDialog.class.getClassLoader().getResource("images/title.gif")));
openButton =new JButton("打开",icons[1]);
//redoButton =new JButton("RedoButton");
saveButton =new JButton("保存",icons[0]);
menuBar =new JMenuBar();
menuBar.add(fileMenu);
fileMenuItem = new JMenuItem("保存");
openMenuItem = new JMenuItem("打开");
exitMenuItem = new JMenuItem("退出");
fileMenu.add(openMenuItem);
fileMenu.add(fileMenuItem);
fileMenu.addSeparator();
fileMenu.add(exitMenuItem);
display= new JTextPane(styledDoc);
styledDoc.addDocumentListener(new documentLis());//
doc=display.getDocument();
display.setFont(new Font("宋体", Font.PLAIN, 20));
container.setLayout(new BorderLayout());
container.add(display,"Center");
toolBar.add(saveButton);
toolBar.addSeparator();
container.add(statusBar,"South");
toolBar.add(openButton);
container.add(toolBar,"North");
scroll = new JScrollPane(display);
container.add(scroll);
frame.setDefaultCloseOperation(WindowConstants.DO_NOTHING_ON_CLOSE);
frame.addWindowListener(new WindowAdapter(){
public void windowClosing (WindowEvent e) {
System.exit(0);
}
});
frame.setJMenuBar(menuBar);
frame.setVisible(true);
//事件管理驱动
ActionListener file = new fileAction();
//添加事件
exitMenuItem.addActionListener(file);
openMenuItem.addActionListener(file);
saveButton.addActionListener(file);
fileMenuItem.addActionListener(file);
openButton.addActionListener(file);
}