import java.awt.event.*;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import javax.swing.*;
import javax.swing.filechooser.FileFilter;
import javax.swing.filechooser.FileNameExtensionFilter;
import java.awt.*;
public class StringConvert extends JFrame {
/**
*
*/
private static final long serialVersionUID = 1L;
private JTextField inputTextField;
private JTextArea outputTextField;
private BufferedReader in;
private File imgFile = null;// 声明所选择文件
private final ButtonGroup buttonGroup = new ButtonGroup();
//一组按钮意味着“开启”其中一个按钮时,将关闭组中的其他所有按钮。
/**
* Launch the application.
*/
public static void main(String[] args) throws ClassNotFoundException, InstantiationException, IllegalAccessException, UnsupportedLookAndFeelException {
try {
UIManager
.setLookAndFeel("com.sun.java.swing.plaf.nimbus.NimbusLookAndFeel");
} catch (Throwable e) {
e.printStackTrace();
}
//获得系统的界面属性
/*try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (Throwable e) {
e.printStackTrace();
}*/
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
StringConvert frame = new StringConvert();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the frame.
*/
public StringConvert() {//构造函数
setTitle("字符串大小写转换");
setBounds(500,200, 450, 214);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// getContentPane().setLayout(null);
/*
inputTextField = new JTextField();
inputTextField.setBounds(23, 21, 383, 31);
getContentPane().add(inputTextField);
inputTextField.setColumns(10);
*/
final JPanel panel = new JPanel();
getContentPane().add(panel, BorderLayout.NORTH);
final JLabel label = new JLabel();
label.setText("路径:");
panel.add(label);
inputTextField = new JTextField();
inputTextField.setPreferredSize(new Dimension(270, 25));
panel.add(inputTextField);
final JButton button1 = new JButton();
button1.addActionListener(new ActionListener() {
public void actionPerformed(final ActionEvent e) {
JFileChooser fileChooser = new JFileChooser();// 创建文件选择器
FileFilter filter = new FileNameExtensionFilter(
"文件(txt/docx/doc/java/cpp/asm)", "txt","docx","doc","java","cpp","asm");// 创建过滤器
fileChooser.setFileFilter(filter);// 设置过滤器
int flag = fileChooser.showOpenDialog(null);// 显示打开对话框
if (flag == JFileChooser.APPROVE_OPTION) {
imgFile = fileChooser.getSelectedFile(); // 获取选中图片的File对象
}
if (imgFile != null) {
inputTextField.setText(imgFile.getAbsolutePath());// 图片完整路径
try {
in=new BufferedReader(new FileReader
(imgFile.getAbsoluteFile()));//获得绝对路径
} catch (IOException ex) {
ex.printStackTrace();
}
}
}
});
button1.setText("选择文件");
panel.add(button1);
final JPanel panel1 = new JPanel();
getContentPane().add(panel1, BorderLayout.CENTER);
JButton button = new JButton("转换");
button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
try {
do_button_actionPerformed(arg0);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
});
button.setBounds(23, 77, 93, 23);
panel1.add(button);
JRadioButton radioButton = new JRadioButton("大写");
radioButton.setActionCommand("大写");
radioButton.setSelected(true);
radioButton.setBounds(169, 77, 76, 23);
buttonGroup.add(radioButton);
panel1.add(radioButton);
JRadioButton radioButton_1 = new JRadioButton("小写");
radioButton_1.setBounds(280, 77, 121, 23);
radioButton_1.setActionCommand("小写");
buttonGroup.add(radioButton_1);
panel1.add(radioButton_1);
final JPanel panel2 = new JPanel();
getContentPane().add(panel2, BorderLayout.SOUTH);
outputTextField = new JTextArea(4,37);
JScrollPane js = new JScrollPane(outputTextField);
panel2.add(js);
}
protected void do_button_actionPerformed(ActionEvent arg0) throws IOException {
// 获取大小写单选项的选择
String command = buttonGroup.getSelection().getActionCommand();
boolean upper = command.equals("大写");// 判断是否选择的大写单选项
if (upper) {// 大写转换
try{
String s;
while((s=in.readLine())!=null){
outputTextField.append(s.toUpperCase());
outputTextField.append("\n");
}
} finally {
in.close();
}
} else {// 小写转换
try {
String s;
while((s=in.readLine())!=null){
outputTextField.append(s.toLowerCase());
outputTextField.append("\n");
}
} finally {
in.close();
}
}
}
}
Java窗口化改变文件中的大小写
最新推荐文章于 2024-02-14 13:43:58 发布