package student2;
import javax.swing.*;
import java.awt.Container;
import java.awt.event.*;public class Student2 extends JFrame implements ActionListener
{
JLabel JL1_name,JL2_password,JL3_tishi;
JButton JB1_enter,JB2_exit;
JTextField JF1;
JPasswordField JP1;
public Student2()
{
super("密码验证");
JL1_name=new JLabel("用户名称");
JL1_name.setBounds(15,5,60,20);

JL2_password=new JLabel("登录密码");
JL2_password.setBounds(15,25,60,20);
//JL3_tishi=new JLabel()
JF1=new JTextField("",20);
JF1.setBounds(110,5,100,20);

JP1=new JPasswordField("",20);
JP1.setBounds(110,25,100,20);

JB1_enter=new JButton("确定");
JB1_enter.setBounds(35,50,60,30);
JB1_enter.addActionListener(this)
;
JB2_exit=new JButton("退出");
JB2_exit.setBounds(130,50,60,30);
JB2_exit.addActionListener(this);

JL3_tishi=new JLabel("");
JL3_tishi.setBounds(60,90,200,30);


Container w1Container=this.getContentPane();
w1Container.add(JL1_name);
w1Container.add(JL2_password);
//w1Container.add(JL3_tishi);
//w1Container.add(JF1);
w1Container.add(JP1);
w1Container.add(JB1_enter);
w1Container.add(JB2_exit);
this.setSize(250,150);
this.setVisible(true);
}

public void actionPerformed(ActionEvent e)
{
if(e.getSource()==JB1_enter)
{
if(JF1.getText().equals("456")&&(JP1.getText().equals("123")))
JL3_tishi.setText("输入正确,登录成功");
else
JL3_tishi.setText("输入错误,登录失败");
}
else if(e.getSource()==JB2_exit)
System.exit(0);
}

public static void main(String[] args)
{
Student2 w1=new Student2();
}
}

解决方案 »

  1.   

    代码可以run出界面,但是JP1.getText()不用了,换个其他的。JF1看不到
    package com.csdn.javase;import javax.swing.*; 
    import java.awt.Container; 
    import java.awt.event.*; //@SuppressWarnings("serial")
    public class student2 extends JFrame implements ActionListener 

    JLabel JL1_name,JL2_password,JL3_tishi; 
    JButton JB1_enter,JB2_exit; 
    JTextField JF1; 
    JPasswordField JP1; 
    public student2() 

    super("Password"); 
    JL1_name=new JLabel("Name"); 
    JL1_name.setBounds(15,5,60,20); JL2_password=new JLabel("Password"); 
    JL2_password.setBounds(15,25,60,20); 
    //JL3_tishi=new JLabel() 
    JF1=new JTextField("",20); 
    JF1.setBounds(110,5,100,20); JP1=new JPasswordField("",20); 
    JP1.setBounds(110,25,100,20); JB1_enter=new JButton("Sure"); 
    JB1_enter.setBounds(35,50,60,30); 
    JB1_enter.addActionListener(this) 

    JB2_exit=new JButton("Log off"); 
    JB2_exit.setBounds(130,50,60,30); 
    JB2_exit.addActionListener(this); JL3_tishi=new JLabel(""); 
    JL3_tishi.setBounds(60,90,200,30); 
    Container w1Container=this.getContentPane(); 
    w1Container.add(JL1_name); 
    w1Container.add(JL2_password); 
    //w1Container.add(JL3_tishi); 
    //w1Container.add(JF1); 
    w1Container.add(JP1); 
    w1Container.add(JB1_enter); 
    w1Container.add(JB2_exit); 
    this.setSize(250,150); 
    this.setVisible(true); 
    } //@SuppressWarnings("deprecation")
    public void actionPerformed(ActionEvent e) 

    if(e.getSource()==JB1_enter) 

    if(JF1.getText().equals("456")&&(JP1.getText().equals("123"))) 
    JL3_tishi.setText("Correct, success"); 
    else 
    JL3_tishi.setText("Error, fail"); 
    }
    else if(e.getSource()==JB2_exit) 
    System.exit(0); 
    } public static void main(String[] args) 

    student2 w1=new student2(); 

      

  2.   


    package com.xxm.csdn;import javax.swing.*;
    import java.awt.Container;
    import java.awt.FlowLayout;
    import java.awt.event.*;public class Student2 extends JFrame implements ActionListener {
    JLabel JL1_name, JL2_password, JL3_tishi;
    JButton JB1_enter, JB2_exit;
    JTextField JF1;
    JPasswordField JP1; public Student2() {
    super("密码验证");
    JL1_name = new JLabel("用户名称");
    JL1_name.setBounds(15, 5, 60, 20); JL2_password = new JLabel("登录密码");
    JL2_password.setBounds(15, 25, 60, 20);
    // JL3_tishi=new JLabel()
    JF1 = new JTextField("", 20);
    JF1.setBounds(110, 5, 100, 20); JP1 = new JPasswordField("", 20);
    JP1.setBounds(110, 25, 100, 20); JB1_enter = new JButton("确定");
    JB1_enter.setBounds(35, 50, 60, 30);
    JB1_enter.addActionListener(this);
    JB2_exit = new JButton("退出");
    JB2_exit.setBounds(130, 50, 60, 30);
    JB2_exit.addActionListener(this); JL3_tishi = new JLabel("");
    JL3_tishi.setBounds(60, 90, 200, 30); Container w1Container = this.getContentPane();
    w1Container.setLayout(new FlowLayout());
    w1Container.add(JL1_name);//这里帮你加了下 摆放模式 你的放位完全不对 这代码几乎就是乱乱的   开刚开始要一步一步地写 写一点运行一点  这样去积累 不要去抄敲完整个代码再运行 这样很乱 就会导致成你现在这样 完全是个乱代码   
    w1Container.add(JF1);
    w1Container.add(JL2_password);
    w1Container.add(JP1);
     w1Container.add(JL3_tishi);
     w1Container.add(JF1);

    w1Container.add(JB1_enter);
    w1Container.add(JB2_exit);
    this.setSize(250, 150);
    this.setVisible(true);
    } public void actionPerformed(ActionEvent e) {
    if (e.getSource() == JB1_enter) {
    if (JF1.getText().equals("456") && (JP1.getText().equals("123")))
    JL3_tishi.setText("输入正确,登录成功");
    else
    JL3_tishi.setText("输入错误,登录失败");
    } else if (e.getSource() == JB2_exit)
    System.exit(0);
    } public static void main(String[] args) {
    Student2 w1 = new Student2();
    }
    }
      

  3.   

    代码问题如下1 没有设置布局模式   默认布局   你的按钮就充满整个窗体了
    2 文本框和一个JLabel没有添加到容器中
    3 没有设置关闭窗体后释放资源
    4 获取密码框的内容时没有把字符数组转换成String2楼的第四个问题依旧存在