/* (程序头部注释开始)
* 程序的版权和版本声明部分
* Copyright (c) 2011, 烟台大学计算机学院学生
* All rights reserved.
* 文件名称:
* 作 者: 臧鹏
* 完成日期: 2013 年 8月 16日
* 版 本 号: 001
* 对任务及求解方法的描述部分
* 输入描述:
* 问题描述:利用eclipse的windowBuilder的编写的窗口程序.数组反转算法
* 程序输出:
* 程序头部的注释结束
*/
package 第三章数组;
import java.awt.BorderLayout;
public class 用数组翻转字符串 extends JFrame {
private JPanel contentPane;
private JTextField textField;
private JTextField textField_1;
private JTextArea textArea ;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
用数组翻转字符串 frame = new 用数组翻转字符串();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the frame.
*/
public 用数组翻转字符串() {
setTitle("\u7528\u6570\u7EC4\u53CD\u8F6C\u5B57\u7B26\u4E32");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 450, 300);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(null);
JLabel lblNewLabel = new JLabel("\u8F93\u5165\u4E00\u4E2A\u5B57\u7B26\u4E32");
lblNewLabel.setBounds(10, 33, 95, 15);
contentPane.add(lblNewLabel);
textField = new JTextField();
textField.setBounds(139, 30, 285, 21);
contentPane.add(textField);
textField.setColumns(10);
textField_1 = new JTextField();
textField_1.setColumns(10);
textField_1.setBounds(139, 64, 285, 21);
contentPane.add(textField_1);
JButton button = new JButton("\u53CD\u8F6C");
button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String inputstr = textField.getText(); //获取用户输入的字符串
char[] str = inputstr.toCharArray(); //转化为字符数组
textArea.setText(""); //文本域清空
for(int i = 0;i<str.length/2;i++){ //字符交换算法
char temp = str[i];
str[i] = str[str.length-i-1];
str[str.length-i-1] = temp;
textArea.append("第"+(i+1)+"次循环:\t"); //显示循环反转过程
for(char c : str){ //显示每次反转的结果
textArea.append(c+"");
}
textArea.append("\n"); //文本域换行
}
String outputstr = new String(str); //把字符数组转化为字符串
textField_1.setText(outputstr); //显示反转后的字符串
}
});
button.setBounds(10, 63, 93, 23);
contentPane.add(button);
textArea = new JTextArea();
textArea.setBounds(10, 96, 414, 156);
contentPane.add(textArea);
}
}
数组反转算法
最新推荐文章于 2024-07-31 17:33:24 发布