📄 makecontent.java~39~
字号:
package swingexample;
import java.io.*;
import java.util.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
//file filter
class dirFilter implements FilenameFilter {
String[] ext;
public dirFilter(String[] ext) {
this.ext = ext;
}
public boolean accept(File dir, String name){
if (ext == null) {
return true;
}
name = name.toLowerCase();
for (int i = 0; i < ext.length; i++){
if (name.endsWith(ext[i])) {
return true;
}
}
return false;
}
}
public class MakeContent extends JFrame{
public static void main(String[] args) {
System.setProperty("swing.plaf.metal.controlFont", "宋体");
new MakeContent();
}
public final boolean DEBUG = true;
private Container rootContainer = getContentPane();
private JRootPane rootPane = getRootPane();
private JPanel mainPanel = new JPanel();
private JPanel buttonPanel = new JPanel();
private JTextField ml = new JTextField();
private JButton btn_ml = new JButton("目录");
private JTextField out_name = new JTextField();
private JCheckBox new_win = new JCheckBox("新窗口", true);
private JTextField title = new JTextField();
private String[] lx = {"all", ".htm;.html", ".doc;.xls", ".c;.h;.java"};
private JComboBox leixing = new JComboBox(lx);
private JButton make = new JButton("生成");
private JButton exit = new JButton("退出");
File files; //目录对象
String[] str_filename; //文件名数组
BufferedWriter html_Out; //文件输出
StringBuffer str_href = new StringBuffer("<a href=\""); //一个字符串的缓冲
public MakeContent(){
setTitle("创建目录文件示例");
setDefaultCloseOperation(EXIT_ON_CLOSE);
initFrame();
pack();
int w = this.getWidth() + 130;
int h = this.getHeight();
Dimension ScreenSize = Toolkit.getDefaultToolkit().getScreenSize();
setBounds( (int) ( (ScreenSize.width - w) / 2),
(int) ( (ScreenSize.height - h) / 2), w, h);
setResizable(false);
setVisible(true);
}
private void initFrame(){
getMainPanel();
getButtonPanel();
rootPane.setDefaultButton(make);
rootContainer.add(mainPanel, BorderLayout.NORTH);
rootContainer.add(buttonPanel, BorderLayout.SOUTH);
}
private void getMainPanel(){
GridBagLayout gbl = new GridBagLayout();
GridBagConstraints gbc = new GridBagConstraints();
mainPanel.setLayout(gbl);
gbc.anchor = GridBagConstraints.WEST;
gbc.fill = GridBagConstraints.HORIZONTAL;
gbc.gridwidth = 1;
gbc.gridx = GridBagConstraints.RELATIVE;
gbc.gridy = 0;
mainPanel.add(new JLabel("源文件目录:", JLabel.RIGHT), gbc);
gbc.weightx = 1;
mainPanel.add(ml, gbc);
gbc.weightx = 0;
mainPanel.add(btn_ml, gbc);
gbc.gridy = 1;
mainPanel.add(new JLabel("目录文件名:", JLabel.RIGHT), gbc);
gbc.weightx = 1;
mainPanel.add(out_name, gbc);
gbc.weightx = 0;
mainPanel.add(new_win, gbc);
gbc.gridy = 2;
/* mainPanel.add(new JLabel("网页标题:", JLabel.RIGHT), gbc);
gbc.gridwidth = GridBagConstraints.REMAINDER;
mainPanel.add(title, gbc);
gbc.gridy = 3;
gbc.gridwidth = 1;
mainPanel.add(new JLabel("文件类型:", JLabel.RIGHT), gbc);
gbc.gridwidth = GridBagConstraints.REMAINDER;
mainPanel.add(leixing, gbc);*/
ml.setText(".");
out_name.setText("index.htm");
title.setText("目录文件生成示例");
leixing.setEditable(true);
leixing.setSelectedIndex(0);
ml.setToolTipText("填写源文件目录");
btn_ml.setToolTipText("选择源文件目录");
out_name.setToolTipText("在源文件目录的输出文件名");
new_win.setToolTipText("是否在新窗口中打开文件");
/* title.setToolTipText("输出目录文件的标题");
leixing.setToolTipText("输入多种类型文件之间请用分号分隔");*/
btn_ml.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
JFileChooser fc = new JFileChooser();
fc.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
String mm = ml.getText();
if (mm != null){
fc.setCurrentDirectory(new File(mm));
}
if (fc.showOpenDialog(null) == JFileChooser.APPROVE_OPTION){
File f = fc.getSelectedFile();
ml.setText(f.getPath());
}
}
});
}
void getButtonPanel(){
buttonPanel.setLayout(new FlowLayout(FlowLayout.CENTER));
buttonPanel.add(make);
buttonPanel.add(exit);
exit.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
System.exit(0);
}
});
make.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
makehtml();
}
});
}
boolean makehtml(){
String str_ml = ml.getText();
String str_out = out_name.getText();
String str_title = title.getText();
String[] exp = getLx();
boolean b = new_win.isSelected();
if ( (str_ml == null) || (str_out == null)) {
return false;
}
files = new File(str_ml);
str_filename = files.list(new dirFilter(exp));
str_out = str_ml + File.separator + str_out;
try {
html_Out = new BufferedWriter(new FileWriter(str_out));
StringBuffer ss = new StringBuffer(100);
ss.append("<html>\r\n<head>\r\n<title>");
if (str_title.equals("")) {
str_title = "目录下文件列表";
}
ss.append(str_title);
ss.append("</title>\r\n</head>\r\n<style type=text/css>\r\n");
ss.append("A.a1:link {text-decoration:none;color:#00007f;}\r\n");
ss.append("A.a1:visited {text-decoration:none;color:#00007f;}\r\n");
ss.append("A.a1:active {text-decoration:none;color:#ff0000;}\r\n");
ss.append("</style>\r\n<body bgcolor=#FFF3C9>\r\n");
ss.append("<p align=center><font color=#FF00FF size=7 face=楷体_GB2312>");
ss.append(str_title);
ss.append("</font>\r\n<p>");
html_Out.write(ss.toString());
for (int i = 0; i < str_filename.length; i++)
{
str_href.setLength(9);
str_href.append(str_filename[i]);
str_href.append("\" class=a1");
if (b) {
str_href.append(" target=_blank>");
}
else {
str_href.append(" >");
}
str_href.append(str_filename[i]);
str_href.append("</a><br>");
html_Out.write(str_href.toString());
html_Out.newLine();
}
html_Out.write("</body>");
html_Out.newLine();
html_Out.write("</html>");
html_Out.close();
}
catch (IOException ioe) {
ioe.printStackTrace();
try {
html_Out.close();
}
catch (IOException ioe1) {
return false;
}
return false;
}
return true;
}
String[] getLx()
{
String s = (String) leixing.getSelectedItem();
if ( (s == null) || (s.indexOf("全部文件") != -1)) {
return null;
}
ArrayList ss = new ArrayList(10);
int off1 = -1;
int off2 = 0;
while ( (off1 = s.indexOf(';', off2)) != -1)
{
ss.add(s.substring(off2, off1));
off2 = off1 + 1;
}
ss.add(s.substring(off2));
String[] exp = (String[]) ss.toArray(new String[0]);
return exp;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -