- import java.awt.*;
- import java.awt.event.ActionEvent;
- import java.awt.event.ActionListener;
- import java.awt.event.WindowAdapter;
- import java.awt.event.WindowEvent;
- import javax.swing.*;
- import java.util.Scanner;
- public class Convert {
- private JFrame f=null;
- private JTextField t1=null;
- private JTextField t2=null;
- private JButton b=null;
- private JRadioButton r1=null;
- private JRadioButton r2=null;
- private ButtonGroup bg=null;
- public Convert(){
- f=new JFrame("字符大小写转换");
- Container c=f.getContentPane();
- // c.setLayout(new GridLayout(1,3));
-
- JPanel p=new JPanel();
- // p.setLayout(new GridLayout(1,3));
- p.setBorder(BorderFactory.createTitledBorder("输入字符串再按转换按钮"));
-
- t1=new JTextField(20);
- t2=new JTextField(20);
- b=new JButton("转换");
- bg=new ButtonGroup();
- r1=new JRadioButton("大写");
- r2=new JRadioButton("小写");
- bg.add(r1);
- bg.add(r2);
- b.addActionListener(new ActionListener(){
- public void actionPerformed(ActionEvent arg0) {
- // TODO 自动生成的方法存根
- String c=bg.getSelection().getActionCommand();
- boolean up=c.equals("大写");
- String t=t1.getText();
- if(up){
- t2.setText(t.toUpperCase());
- }else{
- t2.setText(t.toLowerCase());
- }
- }
-
- });
- p.add(t1);
- p.add(b);
- p.add(r1);
- p.add(r2);
- p.add(t2);
-
- c.add(p);
- f.setSize(300, 150);
- f.setVisible(true);
- f.addWindowListener(new WindowAdapter(){
- public void windowClosing(WindowEvent e){
- System.exit(0);
- }
- });
-
- }
- public static void main(String[] args) {
- // TODO 自动生成的方法存根
- new Convert();
- }
- }
复制代码 |
|