当前位置: 代码迷 >> Java相关 >> [原创]写了个大小写转换工具
  详细解决方案

[原创]写了个大小写转换工具

热度:190   发布时间:2007-09-02 11:25:17.0
以下是引用野蛮女人在2007-9-1 10:08:52的发言:

感谢冰封的支持

哈哈。。引用a276202460的发言,感谢的是冰封。。
----------------解决方案--------------------------------------------------------
哦 也感谢大家
----------------解决方案--------------------------------------------------------
呵呵,没代码呀?
把你的转换核心代码贴上来我们大家批判一下~哈哈哈哈

----------------解决方案--------------------------------------------------------
支持
----------------解决方案--------------------------------------------------------
好的啊 没问题 大家一定要指点哦 不要光“顶”
----------------解决方案--------------------------------------------------------

程序代码:

package swing;

import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.SystemColor;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;

import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JTextField;
import javax.swing.UIManager;

public class Temp extends JFrame {

private ButtonGroup buttonGroup = new ButtonGroup();
private static Temp frame;

public static void main(String args[]) {
frame = new Temp();
frame.setTitle(\"大小写转换工具\");
frame.setVisible(true);
}

public Temp() {
getContentPane().setLayout(null);
setBounds(100, 100, 500, 375);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

final JPanel panel = new JPanel();
panel.setBackground(SystemColor.activeCaptionBorder);
panel.setLayout(null);
panel.setBounds(64, 59, 371, 272);
getContentPane().add(panel);

final JLabel label = new JLabel();
label.setBounds(22, 48, 71, 15);
label.setText(\"来源路径:\");
panel.add(label);

final JLabel label_1 = new JLabel();
label_1.setText(\"目标路径:\");
label_1.setBounds(22, 98, 71, 15);
panel.add(label_1);

final JTextField textField = new JTextField();
textField.setBounds(99, 45, 161, 21);
panel.add(textField);

final JTextField textField_1 = new JTextField();
textField_1.setBounds(99, 95, 161, 21);
panel.add(textField_1);

final JButton button = new JButton();
button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
JFileChooser chooser = new JFileChooser();
int returnVal = chooser.showOpenDialog(button);
if(returnVal == chooser.APPROVE_OPTION){
File file = chooser.getSelectedFile();
textField.setText(file.getAbsolutePath());
}

}
});
button.setText(\"选择\");
button.setBounds(266, 44, 99, 23);
panel.add(button);

final JButton button_1 = new JButton();
button_1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
JFileChooser chooser = new JFileChooser();
int returnVal = chooser.showSaveDialog(button);
if(returnVal == chooser.APPROVE_OPTION){
File file = chooser.getSelectedFile();
textField_1.setText(file.getAbsolutePath());
}

}
});
button_1.setText(\"选择\");
button_1.setBounds(266, 94, 99, 23);
panel.add(button_1);

final JRadioButton radioButton = new JRadioButton();
radioButton.setSelected(true);
radioButton.setBackground(SystemColor.activeCaptionBorder);
buttonGroup.add(radioButton);
radioButton.setText(\"小写转大写\");
radioButton.setBounds(48, 143, 121, 23);
panel.add(radioButton);

final JRadioButton radioButton_1 = new JRadioButton();
radioButton_1.setBackground(SystemColor.activeCaptionBorder);
buttonGroup.add(radioButton_1);
radioButton_1.setText(\"大写转小写\");
radioButton_1.setBounds(225, 143, 121, 23);
panel.add(radioButton_1);

final JButton button_2 = new JButton();
button_2.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String path1 = textField.getText();
String path2 = textField_1.getText();
try {
if(radioButton.isSelected()){
TransitionToUpper(path1, path2);
ShowDialog(\"转换成功!\",\"成功\");
}else if(radioButton_1.isSelected()){
TransitionToLower(path1, path2);
ShowDialog(\"转换成功!\",\"成功\");
}
} catch (IOException e1) {
ShowDialog(\"转换失败!\",\"失败\");
}
}

private void ShowDialog(String str1,String str2) {

final JDialog dialog = new JDialog(frame, str2);
dialog.setSize(180, 80);
JLabel label = new JLabel(str1);
dialog.setLayout(new FlowLayout());
dialog.add(label);
dialog.setLocation(420, 250);
dialog.setVisible(true);

}

});

button_2.setText(\"转换开始\");
button_2.setBounds(132, 211, 99, 23);
panel.add(button_2);

final JLabel label_2 = new JLabel();
label_2.setFont(new Font(\"\", Font.BOLD, 26));
label_2.setText(\"大小写转换\");
label_2.setBounds(174, 23, 144, 30);
getContentPane().add(label_2);
getContentPane().setBackground(SystemColor.activeCaptionBorder);

}

public void TransitionToUpper(String path1, String path2) throws IOException {
String tmp = null;
BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(path1)));
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(path2)));
while ((tmp = br.readLine()) != null) {
String tmp1 = tmp.toUpperCase();
bw.write(tmp1 + \"\n\");
bw.flush();
}
br.close();
bw.close();
}

public void TransitionToLower(String path1, String path2) throws IOException {
String tmp = null;
BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(path1)));
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(path2)));
while ((tmp = br.readLine()) != null) {
String tmp1 = tmp.toLowerCase();
bw.write(tmp1 + \"\n\");
bw.flush();
}
br.close();
bw.close();
}
}


----------------解决方案--------------------------------------------------------
我顶
----------------解决方案--------------------------------------------------------

楼上的注意了!


----------------解决方案--------------------------------------------------------
需要更正的地方

一、动态变量调用静态方法/变量产生歧义
if (returnVal == chooser.APPROVE_OPTION) {
File file = chooser.getSelectedFile();
textField.setText(file.getAbsolutePath());
}

因为 chooser.APPROVE_OPTION;是一个静态的变量,所以最好不要用动态的变量去获取它,以避免歧义,让别人阅读代码时误认为是普通的方法/变量。

更改为:

if (returnVal == JFileChooser.APPROVE_OPTION) {
File file = chooser.getSelectedFile();
textField.setText(file.getAbsolutePath());
}

以下其他地方同理...


二、变量命名混乱
label_1、button_1、button_2大量无意义的变量名充斥其中,根本看不明白那个是那个

三、布局方式混乱
radioButton_1.setBounds(225, 143, 121, 23);
既然Java有布局管理,就不应该要你来操这个心。否则你会丧失JAVA的SWING编程最大的优势:控件随着窗体的改变而改变。不相信你拉拉你的窗体看看就知道了。

还有其他的,你先能改正这些再说吧


----------------解决方案--------------------------------------------------------
还有就是GUI中的MVC设计模式
具体可以看看我的这个帖子
http://bbs.bc-cn.net/viewthread.php?tid=164297&extra=&page=10#291555

----------------解决方案--------------------------------------------------------
  相关解决方案