实现字符串大小写的转换 (Java经典编程案例)

创建窗体,实现将字符串中的字母全部转换为大写或者小写,代码如下:

import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JRadioButton;
import javax.swing.JTextField;
import javax.swing.UIManager;

public class Example extends JFrame {

    private JTextField inputTextField;
    private JTextField outputTextField;
    private final ButtonGroup buttonGroup = new ButtonGroup();

    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    Example frame = new Example();
                    frame.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

    //Create the frame.
    public Example() {
        setTitle("字符串大小写转换");
        setBounds(100, 100, 450, 250);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        getContentPane().setLayout(null);

        inputTextField = new JTextField();
        inputTextField.setBounds(23, 21, 383, 31);
        getContentPane().add(inputTextField);
        inputTextField.setColumns(10);

        JButton button = new JButton("\u8F6C\u6362");//转换
        button.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent arg0) {
                do_button_actionPerformed(arg0);
            }
        });
        button.setBounds(23, 77, 93, 23);
        getContentPane().add(button);

        JRadioButton radioButton = new JRadioButton("大写");
        radioButton.setActionCommand("大写");
        radioButton.setSelected(true);
        radioButton.setBounds(169, 77, 76, 23);
        buttonGroup.add(radioButton);
        getContentPane().add(radioButton);

        JRadioButton radioButton_1 = new JRadioButton("小写");
        radioButton_1.setBounds(280, 77, 121, 23);
        radioButton_1.setActionCommand("小写");
        buttonGroup.add(radioButton_1);
        getContentPane().add(radioButton_1);

        outputTextField = new JTextField();
        outputTextField.setEditable(false);
        outputTextField.setColumns(10);
        outputTextField.setBounds(23, 122, 383, 31);
        getContentPane().add(outputTextField);
        @SuppressWarnings("unused")
        String strBook = "MingRiBook".toLowerCase();
    }

    protected void do_button_actionPerformed(ActionEvent arg0) {
        // 获取大小写单选项的选择
        String command = buttonGroup.getSelection().getActionCommand();
        boolean upper = command.equals("大写");// 判断是否选择的大写单选项
        String text = inputTextField.getText();// 获取输入字符串
        if (upper) {// 大写转换
            outputTextField.setText(text.toUpperCase());
        } else {// 小写转换
            outputTextField.setText(text.toLowerCase());
        }
    }
}

执行结果如下图所示:
实现字符串大小写的转换 (Java经典编程案例)_第1张图片

你可能感兴趣的:(Java经典编程案例)