//改变窗体左上角的java图标 Toolkit toolkit=frame.getToolkit(); Image topicon=toolkit.getImage("C:\\Users\\superzhao\\Pictures\\qq.jpg"); frame.setIconImage(topicon); north = new Panel(); p1 = new Panel(); p2 = new Panel(); p3 = new Panel(); p4 = new Panel(); p5 = new Panel(); p6 = new Panel(); p7 = new Panel(); lb_1 = new Label("帐号:"); lb_2 = new Label("注册帐号"); lb_3 = new Label("密码:"); lb_4 = new Label("找回密码"); lb_5 = new Label("记住密码"); lb_6 = new Label("自动登录");
File file = new File("C:\\Users\\superzhao\\Pictures\\~O_3BTGFON`}LI1VV{3ZDMG.jpg"); BufferedImage image = ImageIO.read(file); lb_7 = new JLabel(new ImageIcon(image));
tf_1 = new JTextField(12); tf_2 = new JPasswordField(12);
//改变窗体左上角的java图标 Toolkit toolkit=frame.getToolkit(); Image topicon=toolkit.getImage("C:\\Users\\superzhao\\Pictures\\qq.jpg"); frame.setIconImage(topicon); north = new Panel(); p1 = new Panel(); p2 = new Panel(); p3 = new Panel(); p4 = new Panel(); p5 = new Panel(); p6 = new Panel(); p7 = new Panel(); lb_1 = new Label("帐号:"); lb_2 = new Label("注册帐号"); lb_3 = new Label("密码:"); lb_4 = new Label("找回密码"); lb_5 = new Label("记住密码"); lb_6 = new Label("自动登录");
File file = new File("C:\\Users\\superzhao\\Pictures\\~O_3BTGFON`}LI1VV{3ZDMG.jpg"); BufferedImage image = ImageIO.read(file); lb_7 = new JLabel(new ImageIcon(image));
tf_1 = new JTextField(12); tf_2 = new JPasswordField(12);
TextField tf = new TextField();.
.
.if(tf.getText() != null && "".equals(tf.getText().trim())) {}
你这样子写
TextField tf = new TextField(); tf.getText().length()==0;或是 "".equals(tf.getText());
TextField tf = new TextField();
tf.getText()==null;//试试行不,字符串为空时没有字符串对象,怎么能调用length()方法
* This method initializes jTextField3
*
* @return javax.swing.JTextField
*/
private JTextField getJTextField3() {
if (jTextField3 == null) {
jTextField3 = new JTextField();
}
return jTextField3;
}你应该先有这么一段把,没有完整代码,别人也没法帮你
import javax.imageio.ImageIO;
import java.io.*;
import java.awt.image.*;
import java.awt.event.*;
import javax.swing.*;
/**
*
* @author superzhao
*/public class QQEnter extends JFrame {
//定义对象
private JFrame frame;
private Panel north;
private Panel p1,p2,p3,p4,p5,p6,p7;
private Label lb_1, lb_2,lb_3,lb_4,lb_5,lb_6;
private JLabel lb_7;
private JTextField tf_1;
private JPasswordField tf_2;
private Button bt_1,bt_2;
private Checkbox ck_1,ck_2;//定义构造函数,用于初始化对象
public QQEnter()throws IOException
{
frame = new JFrame("QQ2010");
//改变窗体左上角的java图标
Toolkit toolkit=frame.getToolkit();
Image topicon=toolkit.getImage("C:\\Users\\superzhao\\Pictures\\qq.jpg");
frame.setIconImage(topicon); north = new Panel();
p1 = new Panel();
p2 = new Panel();
p3 = new Panel();
p4 = new Panel();
p5 = new Panel();
p6 = new Panel();
p7 = new Panel(); lb_1 = new Label("帐号:");
lb_2 = new Label("注册帐号");
lb_3 = new Label("密码:");
lb_4 = new Label("找回密码");
lb_5 = new Label("记住密码");
lb_6 = new Label("自动登录");
File file = new File("C:\\Users\\superzhao\\Pictures\\~O_3BTGFON`}LI1VV{3ZDMG.jpg");
BufferedImage image = ImageIO.read(file);
lb_7 = new JLabel(new ImageIcon(image));
tf_1 = new JTextField(12);
tf_2 = new JPasswordField(12);
ck_1 = new Checkbox();
ck_2 = new Checkbox();
bt_1 = new Button("设置");
bt_2 = new Button("登录");
}/*public void actionPerformed(ActionEvent e)
{
System.exit(1);
}
*/
public void FormFrame()
{
north.setSize(336,61);
north.add(lb_7);
frame.add(north,BorderLayout.NORTH);
p1.setLayout(new FlowLayout());
p1.add( lb_1);
p1.add( tf_1);
p1.add( lb_2);
p2.setLayout(new FlowLayout());
p2.add( lb_3);
tf_2.setEchoChar('*');
p2.add( tf_2);
p2.add( lb_4);
p3.setLayout(new FlowLayout());
p3.add( ck_1);
p3.add( lb_5);
p3.add( ck_2);
p3.add( lb_6); p6.setLayout(new GridLayout(1,2));
p4.setLayout(new FlowLayout(FlowLayout.LEFT));
p4.add(bt_1);
p5.setLayout(new FlowLayout(FlowLayout.RIGHT));
bt_2.addActionListener(new QQEnterEvent());
/* bt_2.addActionListener(
new ActionListener() {
public void actionPerformed(ActionEvent event) {
if(tf_1.getText().equals(" ")|| tf_2.getPassword().equals(" ")) {
JOptionPane.showMessageDialog(QQEnter.this,"请您输入帐号或密码",
"Error", JOptionPane.ERROR_MESSAGE);
return;
}
else
JOptionPane.showMessageDialog(QQEnter.this,"您已成功登录");
}
} );
*/
p5.add(bt_2);
p6.add(p4);
p6.add(p5); p7.setLayout(new GridLayout(4,1));
p7.add(p1);
p7.add(p2);
p7.add(p3);
p7.add(p6); frame.add(p7,BorderLayout.CENTER);
frame. addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
frame.setSize(345,251);
frame.setResizable(false);
frame.setVisible(true);
}//使用内部内可以直接访问对象的私有属性
private class QQEnterEvent implements ActionListener {
/*
QQEnter qq =null;
public QQEnterEvent(QQEnter qq) {
this.qq = qq;
}*/
public void actionPerformed(ActionEvent e) {
if((tf_1.getText() != null && "".equals(tf_1.getText().trim())) || (tf_2.getText() != null
&& "".equals(tf_2.getText().trim() )) ){
JOptionPane.showMessageDialog(QQEnter.this,"请您输入帐号或密码",
"Error", JOptionPane.ERROR_MESSAGE);
return;
}
else
JOptionPane.showMessageDialog(QQEnter.this,"您已成功登录");
}
}
public static void main(String[]args)throws IOException
{
QQEnter q = new QQEnter();
q.FormFrame();
}
}
import java.awt.*;
import javax.imageio.ImageIO;
import java.io.*;
import java.awt.image.*;
import java.awt.event.*;
import javax.swing.*;
/**
*
* @author superzhao
*/public class QQEnter extends JFrame {
//定义对象
private JFrame frame;
private Panel north;
private Panel p1,p2,p3,p4,p5,p6,p7;
private Label lb_1, lb_2,lb_3,lb_4,lb_5,lb_6;
private JLabel lb_7;
private JTextField tf_1;
private JPasswordField tf_2;
private Button bt_1,bt_2;
private Checkbox ck_1,ck_2;//定义构造函数,用于初始化对象
public QQEnter()throws IOException
{
frame = new JFrame("QQ2010");
//改变窗体左上角的java图标
Toolkit toolkit=frame.getToolkit();
Image topicon=toolkit.getImage("C:\\Users\\superzhao\\Pictures\\qq.jpg");
frame.setIconImage(topicon); north = new Panel();
p1 = new Panel();
p2 = new Panel();
p3 = new Panel();
p4 = new Panel();
p5 = new Panel();
p6 = new Panel();
p7 = new Panel(); lb_1 = new Label("帐号:");
lb_2 = new Label("注册帐号");
lb_3 = new Label("密码:");
lb_4 = new Label("找回密码");
lb_5 = new Label("记住密码");
lb_6 = new Label("自动登录");
File file = new File("C:\\Users\\superzhao\\Pictures\\~O_3BTGFON`}LI1VV{3ZDMG.jpg");
BufferedImage image = ImageIO.read(file);
lb_7 = new JLabel(new ImageIcon(image));
tf_1 = new JTextField(12);
tf_2 = new JPasswordField(12);
ck_1 = new Checkbox();
ck_2 = new Checkbox();
bt_1 = new Button("设置");
bt_2 = new Button("登录");
}public void FormFrame()
{
north.setSize(336,61);
north.add(lb_7);
frame.add(north,BorderLayout.NORTH);
p1.setLayout(new FlowLayout());
p1.add( lb_1);
p1.add( tf_1);
p1.add( lb_2);
p2.setLayout(new FlowLayout());
p2.add( lb_3);
tf_2.setEchoChar('*');
p2.add( tf_2);
p2.add( lb_4);
p3.setLayout(new FlowLayout());
p3.add( ck_1);
p3.add( lb_5);
p3.add( ck_2);
p3.add( lb_6); p6.setLayout(new GridLayout(1,2));
p4.setLayout(new FlowLayout(FlowLayout.LEFT));
p4.add(bt_1);
p5.setLayout(new FlowLayout(FlowLayout.RIGHT));
bt_2.addActionListener(new QQEnterEvent());
p5.add(bt_2);
p6.add(p4);
p6.add(p5); p7.setLayout(new GridLayout(4,1));
p7.add(p1);
p7.add(p2);
p7.add(p3);
p7.add(p6); frame.add(p7,BorderLayout.CENTER);
frame. addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
frame.setSize(345,251);
frame.setResizable(false);
frame.setVisible(true);
}//使用内部内可以直接访问对象的私有属性
private class QQEnterEvent implements ActionListener {
public void actionPerformed(ActionEvent e) {
if((tf_1.getText() != null && "".equals(tf_1.getText().trim())) || (tf_2.getText() != null
&& "".equals(tf_2.getText().trim() )) ){
JOptionPane.showMessageDialog(QQEnter.this,"请您输入帐号或密码",
"Error", JOptionPane.ERROR_MESSAGE);
return;
} else
JOptionPane.showMessageDialog(QQEnter.this,"您已成功登录");
}
}
public static void main(String[]args)throws IOException
{
QQEnter q = new QQEnter();
q.FormFrame();
}
}
红色的是图片文件,可以不要的。重点是蓝色的,应该是问题所在。
或者用try catch
测试的时候 try catch 多多用用!
if(tf.getText()!=null&&!"".equals(tf.getText())){
}
private JTextField getJTextField3() {
if (jTextField3!=null&&"".equals(jTextField3.getTest())) {
jTextField3 = new JTextField();
}
return jTextField3;
}
tf_2 = new JPasswordField("",12); 再判断 if("" != tf_1.getText())就行了!如果你只想new一个tf_1,硬是不要赋初始值的话,那就当我没说!
}if(tf.getText()==null|| ts.getText().equals("") ){}
说类似这两种方法正解的,请你们亲自去调试之后再做决定。你们这样说可能只要打几行字或者COPY一下,但对于不理解的人来说就是思想的误导!