单机工具--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();
}
}
很有意思的东东
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();
}
}
很有意思的东东
相关文章推荐
- [leetcode] 206. Reverse Linked List
- Windows平台下SVN安装配置及使用
- 谈谈 Swift 中的 map 和 flatMap
- Windows平台下SVN安装配置及使用
- 产品打包工具的制作,ant,编译源码,打jar包,打tag,打war包,备份release版本等
- 数组、指针
- 简单动态规划总结
- 【学习笔记】hibernate相关(1)Criteria接口
- Windows平台下SVN安装配置及使用
- C++类的6个默认构造函数
- Android动画 - Vector Animation
- C#用链式方法表达循环嵌套
- 实现网站的高并发访问
- 百度音乐,购物车的全选功能
- PyQt之按钮传递鼠标按下事件点击失效
- C++之模板(template)
- hibernate中设置字段不持久化 注解
- gimp 文字渐变效果
- HTML5 LocalStorage 本地存储
- Eclipse中用User Library管理jar包