swing的问题急求大神指教啊
import java.util.Random;public class TestRandom {
public static String test(int length) {
StringBuffer bufferSecretRange=new StringBuffer("0123456789");
int range=bufferSecretRange.length();
Random rd=new Random();
StringBuffer secretBuffer=new StringBuffer();
for(int i=0;i<length;i++){
secretBuffer.append(bufferSecretRange.charAt(rd.nextInt(range)));
}
return secretBuffer.toString();
}
}
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.util.Random;
import javax.swing.*;
public class Login extends JFrame implements ActionListener {
private JPasswordField txtPwd;
private JButton btnLogin,btnClose,btnpo;
private int x=5;
public Login()
{
super("一次性口令");
Container con = getContentPane();
con.setLayout(null);
JLabel head = new JLabel();
head.setBounds(0,0,380,280);
con.add(head);
JLabel lblpwd;
lblpwd = new JLabel("口令:");
lblpwd.setBackground(Color.pink);
head.add(lblpwd);
lblpwd.setBounds(80, 140, 70, 25);
JLabel lblself = new JLabel();
lblself.setBounds(10,110,60,60);
txtPwd = new JPasswordField(10);
txtPwd.setBounds(150, 140,120, 26);
btnpo = new JButton("提示");
btnpo.addActionListener(this);
btnpo.setBounds(270, 140, 80, 25);
head.add(txtPwd);
head.add(btnpo);
head.add(lblself);
btnLogin = new JButton(" 登 录 ");
btnLogin.setMargin(new Insets(0,0,0,0));
btnLogin.addActionListener(this);
btnClose = new JButton(" 关 闭 ");
btnClose.addActionListener(this);
btnLogin.setBounds(150, 190, 80, 25);
btnClose.setBounds(240, 190, 80, 25);
head.add(btnLogin);
head.add(btnClose);
setSize(380,280);
setVisible(true);
setLocationRelativeTo(null);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
Login login = new Login();
}
public void actionPerformed(ActionEvent event) {
TestRandom trd=new TestRandom();
if(event.getSource()==btnpo){
JOptionPane.showMessageDialog(this,trd.test(x));
}
if(event.getSource()== btnLogin)
{
if(txtPwd.getText().equals(""))
{
JOptionPane.showMessageDialog(this,"请输入口令");
return;
}
if(txtPwd.getText().equals(trd.test(x))){
JOptionPane.showMessageDialog(this,"合法用户!");
return;
}else{
JOptionPane.showMessageDialog(this,"非法用户!");
}
}
if(event.getSource()== btnClose)
{
System.exit(0);
}
}
}
怎么运行的时候按提示输入的数字后,怎么还是非法用户求大神指教啊 swing的问题,急求大神指教 --------------------编程问答-------------------- 把你TestRandom类里面的Random rd=new Random(); 改成Random rd=new Random(100);就可以了,这样每次随机的数是一样的。 --------------------编程问答-------------------- test(int length) 这个方法的作用是生成一个随机数并返回吧?
调用了两次那就是两个随机数了。
public void actionPerformed(ActionEvent event) {
TestRandom trd=new TestRandom();
String password=this,trd.test(x);
if(event.getSource()==btnpo){
JOptionPane.showMessageDialog(password);
}
if(event.getSource()== btnLogin)
{
if(txtPwd.getText().equals(""))
{
JOptionPane.showMessageDialog(this,"请输入口令");
return;
}
if(txtPwd.getText().equals(password)){
JOptionPane.showMessageDialog(this,"合法用户!");
return;
}else{
JOptionPane.showMessageDialog(this,"非法用户!");
}
}
改成这样。
补充:Java , Java SE