接受用户输入的一个字符串和一个字符,把字符串中所有指定的字符删除后输出。
题目:
编写一个图形界面程序,接受用户输入的一个字符串和一个字符,把字符串中所有指定的字符删除后输出。(尽可能多的方法) 考核:1、学会使用Java API 文档 2、区分String、StringBuffer、StringBuilder类 递交资料:1、运行结果截图,能体现出用了几种方法 2、每种方法的代码截图 3、Java源文件
直接代码:
package demo1; import java.awt.BorderLayout; import java.awt.EventQueue; import java.awt.Font; import javax.swing.JFrame; import javax.swing.JPanel; import javax.swing.border.EmptyBorder; import javax.swing.JTextField; import javax.swing.JLabel; import javax.swing.JButton; import java.awt.event.ActionListener; import java.awt.event.ActionEvent; import javax.swing.JTextArea; import javax.swing.JComboBox; public class frame03 extends JFrame { private JPanel contentPane; private JTextField textField; private JTextField textField_1; private JTextArea textArea; private JComboBox comboBox; /** * Launch the application. */ public static void main(String[] args) { EventQueue.invokeLater(new Runnable() { public void run() { try { frame03 frame = new frame03(); frame.setVisible(true); } catch (Exception e) { e.printStackTrace(); } } }); } /** * Create the frame. */ public frame03() { 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); textField = new JTextField(); textField.setBounds(137, 38, 152, 21); contentPane.add(textField); textField.setColumns(10); textField_1 = new JTextField(); textField_1.setBounds(137, 71, 152, 21); contentPane.add(textField_1); textField_1.setColumns(10); textArea = new JTextArea(); textArea.setBounds(48, 102, 378, 118); contentPane.add(textArea); textArea.setColumns(6); Font font=new Font("黑体",Font.PLAIN,10); textArea.setFont(font); JLabel lblNewLabel = new JLabel("\u8F93\u5165\u5B57\u7B26\u4E32\uFF1A"); lblNewLabel.setBounds(60, 41, 81, 15); contentPane.add(lblNewLabel); JLabel lblNewLabel_1 = new JLabel("\u8F93\u51FA\uFF1A"); lblNewLabel_1.setBounds(10, 122, 58, 15); contentPane.add(lblNewLabel_1); JLabel lblNewLabel_1_1 = new JLabel("\u8F93\u5165\u5B57\u7B26\uFF1A"); lblNewLabel_1_1.setBounds(60, 74, 67, 15); contentPane.add(lblNewLabel_1_1); JButton btnNewButton = new JButton("\u786E\u5B9A"); btnNewButton.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { String string1=textField.getText(); StringBuffer string2= new StringBuffer(textField.getText()); StringBuilder string3=new StringBuilder(textField.getText()); String sal0=textField_1.getText(); char[] sal1=textField_1.getText().toCharArray(); //第一种:使用String将符合的字符找到并进行替换为""。 String resultString1=string1.replace(sal0, ""); textArea.setText("第一种:使用String将符合的字符找到并进行替换为:\n"+resultString1+'\n'); //第二种string将字符串转换为字符数组,匹配删去 int len=string1.length(); String resultString2=""; char[] textChar=string1.toCharArray(); for(int i=0;i<len;i++) { if(textChar[i]==sal1[0]) continue; else { resultString2+=textChar[i]; } } textArea.append("第二种string将字符串转换为字符数组,匹配删去:\n"+resultString2+'\n'); //第三种:使用StringBuffer: StringBuffer StringBuffer = new StringBuffer(""); for(int i=0;i<string2.length();i++) { if(sal1[0]==string2.charAt(i)) continue; else StringBuffer.append(string2.charAt(i)); } String resultString3=StringBuffer.toString(); textArea.append("第三种:使用StringBuffer解决线程问题,并用charAt()方法寻找对应字符:\n"+resultString3+'\n'); //第四种:使用StringBuilder:循环删除字符,因为删除一个字符就会移位,所以要重新计算位置,也是最简单的方法: while(string3.indexOf(sal0)!=-1) { string3=string3.deleteCharAt(string3.indexOf(sal0)); } String resultString4=string3.toString(); textArea.append(resultString4+'\n'); } }); btnNewButton.setBounds(177, 230, 67, 23); contentPane.add(btnNewButton); comboBox = new JComboBox(); comboBox.setBounds(392, 171, 34, 23); contentPane.add(comboBox); } }
运行截图: