编程小问题求助(自学难..)
import javax.swing.*;
import java .awt.*;
import java.awt.event.*;
public class A extends JFrame implements ActionListener {
static A w1;
JLabel jl1,jl2,jl3;
JTextField jt1;
JButton jb1,jb2;
JPasswordField jp1;
public A(){
super("文本框");
jl3=new JLabel("0");
jl1=new JLabel("用户名称");
jl1.setBounds(30,30,100,30);
jl2=new JLabel("注册密码");
jl2.setBounds(30,60,100,30);
jt1=new JTextField("",20);
jt1.setBounds(130,30,100,30);
jp1=new JPasswordField("",20);
jp1.setBounds(130,60,100,30);
jb1=new JButton("注册");
jb1.setBounds(30,90,100,30);
jb1.addActionListener(this);
jb2=new JButton("退出");
jb2.setBounds(130,90,100,30);
jb2.addActionListener(this);
Container w=this.getContentPane();
w.setLayout(null);
w.add(jl1);
w.add(jl2);
w.add(jt1);
w.add(jb1);
w.add(jb2);
w.add(jp1);
this.setSize(300,300);
this.setLocation(300, 300);
this.setVisible(true);
}
public static void main(String[] args){
w1=new A();
}
public void actionPerformed(ActionEvent e){
////////这是是判断是否为相同字符的算法
int y=0;
String c=jp1.getText();///这里讲字符串转换成数组
char[]cc; ///这里讲字符串转换成数组
cc=c.toCharArray(); ///这里讲字符串转换成数组
for(int i=0;i<jp1.getText().length();i++){
if(cc[0]==cc[i])y++;
if(y==jp1.getText().length())jl3.setText("相同");
else jl3.setText("不相同");
}
/////////
String a="@";
if(e.getSource()==jb1){
////////String a="@"; 这里判断大的字符串里面是否含有@如果有 这返回值大于0
if(jt1.getText().indexOf(a)>0){
JOptionPane.showMessageDialog(w1,"用户含有非法字符!","错误提示",2);
}
////////
else if(jp1.getText().length()<4||jp1.getText().length()>6){
JOptionPane.showMessageDialog(w1,"密码长度应为4-6位!","错误提示",2);
}
else if(jl3.getText().equals("相同")){
JOptionPane.showMessageDialog(w1,"密码不能为相同的字符!","错误提示",2);
}
else if(jt1.getText().equals("java")&&jp1.getText().equals("1234")){
MyDialog m1=new Dialog(w1,"登录成功"); ///这里有问题
}
}
else if(e.getSource()==jb2){
int an=JOptionPane.showConfirmDialog(w1,"你确定退出吗?", "信息提示",0,0);
if(an==0)System.exit(0);
}
}
}
class Mydialog extends JDialog implements ActionListener{
JLabel jl1,jl2,jl3,jl4;
JTextField jt1;
JPasswordField jp1;
JButton jb1;
A frames;
public MyDialog(A frame1,String title){ ///这里有问题
super(frame1,title); ///这里有问题.
frames=frame1;
Container w=this.getContentPane();
jl1=new JLabel("用户登录成功!");
jl1.setBounds(15,25,60,30);
jl2=new JLabel("用户名:");
jl2.setBounds(15,55,60,30);
jl3=new JLabel(frames.jt1.getText());
jl3.setBounds(25,55,60,30);
jl4=new JLabel("密码:1234");
jl4.setBounds(15,75,60,30);
jb1=new JButton("确定");
jb1.setBounds(25,95,60,30);
jb1.addActionListener(this);
w.setLayout(null);
w.add(jl1);
w.add(jl2);
w.add(jl3);
w.add(jl4);
w.add(jb1);
this.setSize(250,140);
this.setVisible(true);
}
public void actionPerformed(ActionEvent e){
if(e.getSource()==jb1)
System.exit(0);
}
}