效果图:

详细代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
package 
com.lixiyu;
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 
StringConvert 
extends 
JFrame {
    
private 
static 
final 
long 
serialVersionUID = 4556387791998133270L;
    
private 
JTextField inputTextField;
    
private 
JTextField outputTextField;
    
private 
final 
ButtonGroup buttonGroup=
new 
ButtonGroup();
       
    
public 
static 
void 
main(String[] args){
        
try
{
        
UIManager.setLookAndFeel(
"com.sun.java.swing.plaf.nimbus.NimbusLookAndFeel"
);
    
}
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(
"\u5B57\u7B26\u4E32\u5927\u5C0F\u5199\u8F6C\u6362"
);
    
setBounds(
100
,
100
,
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
);
       
    
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());
}
}
}

本文转自lixiyu 51CTO博客,原文链接:http://blog.51cto.com/lixiyu/1305498,如需转载请自行联系原作者