java開發實戰1200(I)--------081去掉字串中的所有空格
package DeleteBlank;
import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
public class DeleteBlank extends JFrame {
private JLabel label;
private JTextField input_text;
private JTextField output_text;
private JButton action_button;
private JPanel north,center;
public DeleteBlank(){
north=new JPanel();
label=new JLabel("輸入字串:");
input_text=new JTextField("",20);
north.add(label);
north.add(input_text);
add(north,BorderLayout.NORTH);
action_button=new JButton("去掉空格");
output_text=new JTextField("",20);
JPanel panel=new JPanel();
panel.add(action_button);
panel.add(output_text);
center=new JPanel();
center.add(panel);
add(center);
action_button.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
String text=input_text.getText();
StringBuilder strBuilder=new StringBuilder();
for(int i=0;i<text.length();i++){
char charAt=text.charAt(i);
if(charAt==' '){
continue;
}
strBuilder.append(charAt);
}
output_text.setText(strBuilder.toString());
}
});
pack();
}
}