您的位置:首页 > 其它

单机工具--jframe应用

2016-06-01 11:35 309 查看
最近看同事写的一个工具很有意思,分享下

public class EncodingPanel extends JPanel {
private static final long serialVersionUID = 1L;

public EncodingPanel() {
super(new GridBagLayout());
initComponent();
}
public static void main(String[] args) {
new EncodingPanel().initComponent();
}

/**
* 初始化控件
*/
private void initComponent() {
GridBagConstraints constraints = new GridBagConstraints();
constraints.fill = GridBagConstraints.BOTH;
constraints.ipady = 8;
constraints.insets = new Insets(8, 0, 0, 0);
// 输入:
JLabel srcFileLabel = new JLabel("输入:");
constraints.gridwidth = 1;
constraints.weightx = 0;
this.add(srcFileLabel, constraints);
final JComboBox<String> srcEncodeType = new JComboBox<String>(
new String[] { "GBK", "GB2312", "UTF-8", "ISO8859-1" });
constraints.gridwidth = 2;
constraints.weightx = 0;
this.add(srcEncodeType, constraints);
final JButton srcButton = new JButton("请选择输入文件路径");
constraints.gridwidth = 3;
constraints.weightx = 1;
this.add(srcButton, constraints);
srcButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
String file = DocParseFrame.chooseFile(JFileChooser.FILES_AND_DIRECTORIES);
if (!file.equals("")) {
srcButton.setText(file);
}
}
});
// 输出:
JLabel targetFileLabel = new JLabel("输出:");
constraints.gridwidth = 1;
constraints.gridy = 1;
constraints.weightx = 0;
this.add(targetFileLabel, constraints);
final JComboBox<String> targetEncodeType = new JComboBox<String>(
new String[] { "GBK", "GB2312", "UTF-8", "ISO8859-1" });
constraints.gridwidth = 2;
constraints.weightx = 0;
constraints.gridy = 1;
this.add(targetEncodeType, constraints);
final JButton targetButton = new JButton("请选择输出文件路径");
constraints.gridwidth = 3;
constraints.gridy = 1;
constraints.weightx = 0;
this.add(targetButton, constraints);
targetButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
String file = DocParseFrame.chooseFile(JFileChooser.FILES_AND_DIRECTORIES);
if (!file.equals("")) {
targetButton.setText(file);
}
}
});
// 编码选择

// 转换按钮
final JButton changeButton = new JButton("转换");
constraints.gridwidth = 2;
constraints.gridy = 2;
constraints.weightx = 0;
constraints.insets = new Insets(24, 10, 0, 0);
this.add(changeButton, constraints);
final JLabel progresslabel = new JLabel("");
constraints.gridwidth = 2;
constraints.gridy = 2;
constraints.weightx = 0;
this.add(progresslabel, constraints);

// 错误面板
final JTextArea errTextArea = new JTextArea();
constraints.gridwidth = 6;
constraints.gridy = 3;
constraints.weighty = 20;

constraints.insets = new Insets(10, 0, 10, 0);
this.add(errTextArea, constraints);

changeButton.addActionListener(new ActionListener() {

@Override
public void actionPerformed(ActionEvent e) {
final String inFilePath = srcButton.getText();
final String inCharsetName = srcEncodeType.getSelectedItem().toString();
final String outFilePath = targetButton.getText();
final String outCharsetName = targetEncodeType.getSelectedItem().toString();
if (inFilePath.equals("请选择输入文件路径") || outFilePath.equals("请选择输出文件路径")) {
JOptionPane.showMessageDialog(EncodingPanel.this, "输入参数不正确");
return;
}
changeButton.setEnabled(false);
new Thread() {
public void run() {
try {
new EncodingUtil().recursionFileEncodingTrans(inFilePath, inCharsetName, outFilePath,
outCharsetName, new EncodingUtil.ProgressInterface() {
@Override
public void progress(int index) {
progresslabel.setText("正在处理:第" + index + "个");
}
});
progresslabel.setText("转换完毕");
} catch (Exception e) {
e.printStackTrace();
errTextArea.setText(e.toString());
}finally{
changeButton.setEnabled(true);
}
};
}.start();
}
});
}

}

package com.dinfo.oec.doc.ui;

import java.io.File;

import javax.swing.JFileChooser;

import javax.swing.JFrame;

import javax.swing.JLabel;

import javax.swing.JTabbedPane;

public class DocParseFrame extends JFrame {
private static final long serialVersionUID = 1L;

public DocParseFrame() {
createJTabbedPane();
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setBounds(100, 100, 400, 400);
this.setVisible(true);
}

public void createJTabbedPane() {
JTabbedPane tabbedPane = new JTabbedPane();
// 1。编码转换
tabbedPane.addTab("编码转换", new EncodingPanel());
// 2.文档解析
tabbedPane.addTab("文档解析", new DocParsePanel());
getContentPane().add(tabbedPane);
}

// 选择文件
public static String chooseFile(int chooseType) {
JFileChooser jfc = new JFileChooser();
jfc.setFileSelectionMode(chooseType);
jfc.showDialog(new JLabel(), "选择");
File file = jfc.getSelectedFile();
if (file != null) {
return file.getAbsolutePath();
} else {
return "";
}
}

public static void main(String[] args) {
new DocParseFrame();
}

}

很有意思的东东
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: