当前位置: 技术问答>java相关
关于密码验证输入框的问题!
来源: 互联网 发布时间:2015-10-13
本文导语: 我做了个密码验证的小程序,在用户名输入框输入用户名后,还有好多空格,我必须手动删掉才行,有没有办法处理这个问题,该怎么处理,谢谢! 附代码: package passwd; import java.awt.*; import java.awt.event.*; import ja...
我做了个密码验证的小程序,在用户名输入框输入用户名后,还有好多空格,我必须手动删掉才行,有没有办法处理这个问题,该怎么处理,谢谢!
附代码:
package passwd;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import com.borland.jbcl.layout.*;
public class Frame1 extends JFrame {
private JPanel contentPane;
private XYLayout xYLayout1 = new XYLayout();
private Panel panel1 = new Panel();
private Panel panel2 = new Panel();
private Panel panel3 = new Panel();
private Panel panel4 = new Panel();
private CheckboxGroup checkboxGroup1 = new CheckboxGroup();
private Checkbox checkbox1 = new Checkbox();
private Checkbox checkbox2 = new Checkbox();
private Label label1 = new Label();
private TextField textField1 = new TextField();
private Label label2 = new Label();
private TextField textField2 = new TextField();
private Button button1 = new Button();
private TextField textField3 = new TextField();
//Construct the frame
public Frame1() {
enableEvents(AWTEvent.WINDOW_EVENT_MASK);
try {
jbInit();
}
catch(Exception e) {
e.printStackTrace();
}
}
//Component initialization
private void jbInit() throws Exception {
//setIconImage(Toolkit.getDefaultToolkit().createImage(Frame1.class.getResource("[Your Icon]")));
contentPane = (JPanel) this.getContentPane();
contentPane.setLayout(xYLayout1);
this.setSize(new Dimension(400, 300));
this.setTitle("passwd");
checkbox1.setCheckboxGroup(checkboxGroup1);
checkbox1.setLabel("high");
checkbox1.setState(true);
checkbox2.setCheckboxGroup(checkboxGroup1);
checkbox2.setLabel("low");
label1.setText("use");
label2.setText("passwd");
textField2.setEchoChar('*');
textField2.setText(" ");
textField1.setText(" ");
button1.setLabel("check");
button1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(ActionEvent e) {
button1_actionPerformed(e);
}
});
contentPane.add(panel1, new XYConstraints(5, 22, 74, 101));
panel1.add(checkbox1, null);
panel1.add(checkbox2, null);
contentPane.add(panel2, new XYConstraints(155, 23, 212, 40));
panel2.add(label1, null);
panel2.add(textField1, null);
contentPane.add(panel3, new XYConstraints(153, 78, 218, 50));
panel3.add(label2, null);
panel3.add(textField2, null);
contentPane.add(panel4, new XYConstraints(228, 209, 149, 40));
panel4.add(button1, null);
contentPane.add(textField3, new XYConstraints(2, 192, 182, 35));
}
//Overridden so we can exit when window is closed
protected void processWindowEvent(WindowEvent e) {
super.processWindowEvent(e);
if (e.getID() == WindowEvent.WINDOW_CLOSING) {
System.exit(0);
}
}
void button1_actionPerformed(ActionEvent e) {
//add code to drive event
String s1,s2;
s1=textField1.getText();
s2=textField2.getText();
if(checkbox1.getState()){
if((s1.equals("cjy"))&&(s2.equals("22")))
textField3.setText("you input correctly");
else
textField3.setText("");
textField3.setText("you are not correctly");
}
else
{
if((s1.equals("tom"))&&(s2.equals("222222")))
textField3.setText("you are correctly");
else
textField3.setText("you are not correctly");
}
}
}
附代码:
package passwd;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import com.borland.jbcl.layout.*;
public class Frame1 extends JFrame {
private JPanel contentPane;
private XYLayout xYLayout1 = new XYLayout();
private Panel panel1 = new Panel();
private Panel panel2 = new Panel();
private Panel panel3 = new Panel();
private Panel panel4 = new Panel();
private CheckboxGroup checkboxGroup1 = new CheckboxGroup();
private Checkbox checkbox1 = new Checkbox();
private Checkbox checkbox2 = new Checkbox();
private Label label1 = new Label();
private TextField textField1 = new TextField();
private Label label2 = new Label();
private TextField textField2 = new TextField();
private Button button1 = new Button();
private TextField textField3 = new TextField();
//Construct the frame
public Frame1() {
enableEvents(AWTEvent.WINDOW_EVENT_MASK);
try {
jbInit();
}
catch(Exception e) {
e.printStackTrace();
}
}
//Component initialization
private void jbInit() throws Exception {
//setIconImage(Toolkit.getDefaultToolkit().createImage(Frame1.class.getResource("[Your Icon]")));
contentPane = (JPanel) this.getContentPane();
contentPane.setLayout(xYLayout1);
this.setSize(new Dimension(400, 300));
this.setTitle("passwd");
checkbox1.setCheckboxGroup(checkboxGroup1);
checkbox1.setLabel("high");
checkbox1.setState(true);
checkbox2.setCheckboxGroup(checkboxGroup1);
checkbox2.setLabel("low");
label1.setText("use");
label2.setText("passwd");
textField2.setEchoChar('*');
textField2.setText(" ");
textField1.setText(" ");
button1.setLabel("check");
button1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(ActionEvent e) {
button1_actionPerformed(e);
}
});
contentPane.add(panel1, new XYConstraints(5, 22, 74, 101));
panel1.add(checkbox1, null);
panel1.add(checkbox2, null);
contentPane.add(panel2, new XYConstraints(155, 23, 212, 40));
panel2.add(label1, null);
panel2.add(textField1, null);
contentPane.add(panel3, new XYConstraints(153, 78, 218, 50));
panel3.add(label2, null);
panel3.add(textField2, null);
contentPane.add(panel4, new XYConstraints(228, 209, 149, 40));
panel4.add(button1, null);
contentPane.add(textField3, new XYConstraints(2, 192, 182, 35));
}
//Overridden so we can exit when window is closed
protected void processWindowEvent(WindowEvent e) {
super.processWindowEvent(e);
if (e.getID() == WindowEvent.WINDOW_CLOSING) {
System.exit(0);
}
}
void button1_actionPerformed(ActionEvent e) {
//add code to drive event
String s1,s2;
s1=textField1.getText();
s2=textField2.getText();
if(checkbox1.getState()){
if((s1.equals("cjy"))&&(s2.equals("22")))
textField3.setText("you input correctly");
else
textField3.setText("");
textField3.setText("you are not correctly");
}
else
{
if((s1.equals("tom"))&&(s2.equals("222222")))
textField3.setText("you are correctly");
else
textField3.setText("you are not correctly");
}
}
}
|
s1=s1.trim();
s2=s2.trim();
就可以去掉首位的空格
s2=s2.trim();
就可以去掉首位的空格
|
是去掉前后空格!
|
查找帮助吧/兄弟
|
trim
public String trim()
Removes white space from both ends of this string.
If this String object represents an empty character sequence, or the first and last characters of character sequence represented by this String object both have codes greater than 'u0020' (the space character), then a reference to this String object is returned.
Otherwise, if there is no character with a code greater than 'u0020' in the string, then a new String object representing an empty string is created and returned.
Otherwise, let k be the index of the first character in the string whose code is greater than 'u0020', and let m be the index of the last character in the string whose code is greater than 'u0020'. A new String object is created, representing the substring of this string that begins with the character at index k and ends with the character at index m-that is, the result of this.substring(k, m+1).
This method may be used to trim whitespace from the beginning and end of a string; in fact, it trims all ASCII control characters as well.
Returns:
this string, with white space removed from the front and end.
--------------------------------------------------------------------------------
public String trim()
Removes white space from both ends of this string.
If this String object represents an empty character sequence, or the first and last characters of character sequence represented by this String object both have codes greater than 'u0020' (the space character), then a reference to this String object is returned.
Otherwise, if there is no character with a code greater than 'u0020' in the string, then a new String object representing an empty string is created and returned.
Otherwise, let k be the index of the first character in the string whose code is greater than 'u0020', and let m be the index of the last character in the string whose code is greater than 'u0020'. A new String object is created, representing the substring of this string that begins with the character at index k and ends with the character at index m-that is, the result of this.substring(k, m+1).
This method may be used to trim whitespace from the beginning and end of a string; in fact, it trims all ASCII control characters as well.
Returns:
this string, with white space removed from the front and end.
--------------------------------------------------------------------------------