Java窗口化改变文件中的大小写

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();
		}
        }
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值