JOptinPane的使用求助

xtuxucj 2012-08-11 11:31:01
看下面的代码,主要是目的是有一个密码输入校验。想实现的情况是,如果两次密码输入不对,弹出一个错误对话框,点击确认后,输入密码的那个对话框不会消失,而让你继续输入密码。我实现不了,忘高手帮忙。

package ser;

import java.awt.GridLayout;

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JPasswordField;

public class FramTest extends JFrame {

/**
*
*/
private static final long serialVersionUID = 1L;
/**
* @param args
*/
JPanel jp2;
JLabel lb1;
JLabel lb2;
JPasswordField tx1;
JPasswordField tx2;
public FramTest(){
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
this.setSize(600, 400);
this.setLocationRelativeTo(null);
this.setVisible(true);
jp2 = new JPanel();
jp2.setLayout(new GridLayout(2,2));
lb1 = new JLabel("Enter new Password:");
lb2 = new JLabel("Confirm new Password:");
tx1 = new JPasswordField();
tx2 = new JPasswordField();
jp2.add(lb1);
jp2.add(tx1);
jp2.add(lb2);
jp2.add(tx2);

int value = JOptionPane.showOptionDialog(this, jp2,
"Set Keystore Type", JOptionPane.OK_CANCEL_OPTION,
JOptionPane.PLAIN_MESSAGE,null,null,null);
switch (value) {
case JOptionPane.CLOSED_OPTION:
break;
case JOptionPane.OK_OPTION:
//do something
if(!verifyPass(tx1.getPassword(), tx2.getPassword()))
{
JOptionPane.showMessageDialog(this, "The password is null or do not mach!",
"Set key store password", JOptionPane.ERROR_MESSAGE);
}
break;
case JOptionPane.CANCEL_OPTION:
break;
}

}

private boolean verifyPass(char[] p1,char[] p2){
boolean flag = true;
if(p1.length == p2.length){
for(int i = 0; i < p1.length;i++){
if(p1[i] != p2[i]){
flag = false;
break;
}
flag = true;
}
}else{
flag = false;
}
return flag;

}

public static void main(String[] args) {
new FramTest();
}

}
...全文
156 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
xtuxucj 2012-08-12
  • 打赏
  • 举报
回复
自己搞定了。哎
xtuxucj 2012-08-12
  • 打赏
  • 举报
回复
private void saveKeystoreMIActionPer() {
jp2 = new JPanel();
jp2.setLayout(new GridLayout(3, 3));
JLabel lb0 = new JLabel("set alias:");
JLabel lb1 = new JLabel("Enter new Password:");
JLabel lb2 = new JLabel("Confirm new Password:");
tx0 = new JTextField();
tx1 = new JPasswordField();
tx2 = new JPasswordField();
jp2.add(lb0);
jp2.add(tx0);
jp2.add(lb1);
jp2.add(tx1);
jp2.add(lb2);
jp2.add(tx2);
optionPane = new JOptionPane(jp2,
JOptionPane.PLAIN_MESSAGE, JOptionPane.OK_CANCEL_OPTION, null,
options);

dialog = new JDialog(this, "Set keystore password", true);

dialog.setContentPane(optionPane);


optionPane.addPropertyChangeListener(new PropertyChangeListener(){

@Override
public void propertyChange(PropertyChangeEvent e) {
String prop = e.getPropertyName();

if (isVisible()
&& (e.getSource() == optionPane)
&& (JOptionPane.VALUE_PROPERTY.equals(prop))){
Object value = optionPane.getValue();


if (value == JOptionPane.UNINITIALIZED_VALUE) {
//ignore reset
return;
}
optionPane.setValue(
JOptionPane.UNINITIALIZED_VALUE);

if ("OK".equals(value)){
getPassword();
}else if ("CANCEL".equals(value)){
dialog.setVisible(false);
}


}

}

});

dialog.setSize(310, 160);
dialog.setLocationRelativeTo(this);
dialog.setResizable(false);
dialog.setVisible(true);


}
pl3121605999 2012-08-11
  • 打赏
  • 举报
回复

public class FramTest extends JFrame {

/**
*
*/
private static final long serialVersionUID = 1L;
/**
* @param args
*/
JPanel jp2;
JLabel lb1;
JLabel lb2;
JPasswordField tx1;
JPasswordField tx2;

public FramTest() {
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
this.setSize(600, 400);
this.setLocationRelativeTo(null);
this.setVisible(true);
jp2 = new JPanel();
jp2.setLayout(new GridLayout(2, 2));
lb1 = new JLabel("Enter new Password:");
lb2 = new JLabel("Confirm new Password:");
tx1 = new JPasswordField();
tx2 = new JPasswordField();
jp2.add(lb1);
jp2.add(tx1);
jp2.add(lb2);
jp2.add(tx2);

int key = 1; //这里添加
while (key-- > 0) { //这里添加

int value = JOptionPane.showOptionDialog(this, jp2,
"Set Keystore Type", JOptionPane.OK_CANCEL_OPTION,
JOptionPane.PLAIN_MESSAGE, null, null, null);
switch (value) {
case JOptionPane.CLOSED_OPTION:
key++;
break;
case JOptionPane.OK_OPTION:
//do something
if (!verifyPass(tx1.getPassword(), tx2.getPassword())) {
JOptionPane.showMessageDialog(this, "The password is null or do not mach!",
"Set key store password", JOptionPane.ERROR_MESSAGE);
key++; //这里添加
}
break;
case JOptionPane.CANCEL_OPTION:
key++;
break;
}
}

}

private boolean verifyPass(char[] p1, char[] p2) {
boolean flag = true;
if (p1.length == p2.length) {
for (int i = 0; i < p1.length; i++) {
if (p1[i] != p2[i]) {
flag = false;
break;
}
flag = true;
}
} else {
flag = false;
}
return flag;

}

public static void main(String[] args) {
new FramTest();
}
}
xtuxucj 2012-08-11
  • 打赏
  • 举报
回复
可是可以,但是如果不想让int value = JOptionPane.showOptionDialog(this, jp2,
"Set Keystore Type", JOptionPane.OK_CANCEL_OPTION,
JOptionPane.PLAIN_MESSAGE,null,null,null);
消失就不行,最好让点了按钮后不消失
zqfddqr 2012-08-11
  • 打赏
  • 举报
回复
这个 和 JOptinPane 没什么关系吧
这样的行么

package 网上玩的;

import java.awt.GridLayout;

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JPasswordField;

public class FramTest extends JFrame {

/**
*
*/
private static final long serialVersionUID = 1L;
/**
* @param args
*/
JPanel jp2;
JLabel lb1;
JLabel lb2;
JPasswordField tx1;
JPasswordField tx2;

public FramTest() {
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
this.setSize(600, 400);
this.setLocationRelativeTo(null);
this.setVisible(true);
jp2 = new JPanel();
jp2.setLayout(new GridLayout(2, 2));
lb1 = new JLabel("Enter new Password:");
lb2 = new JLabel("Confirm new Password:");
tx1 = new JPasswordField();
tx2 = new JPasswordField();
jp2.add(lb1);
jp2.add(tx1);
jp2.add(lb2);
jp2.add(tx2);

int key = 1; //这里添加
while (key-- > 0) { //这里添加

int value = JOptionPane.showOptionDialog(this, jp2,
"Set Keystore Type", JOptionPane.OK_CANCEL_OPTION,
JOptionPane.PLAIN_MESSAGE, null, null, null);
switch (value) {
case JOptionPane.CLOSED_OPTION:
break;
case JOptionPane.OK_OPTION:
//do something
if (!verifyPass(tx1.getPassword(), tx2.getPassword())) {
JOptionPane.showMessageDialog(this, "The password is null or do not mach!",
"Set key store password", JOptionPane.ERROR_MESSAGE);

key++; //这里添加

}
break;
case JOptionPane.CANCEL_OPTION:
break;
}
}

}

private boolean verifyPass(char[] p1, char[] p2) {
boolean flag = true;
if (p1.length == p2.length) {
for (int i = 0; i < p1.length; i++) {
if (p1[i] != p2[i]) {
flag = false;
break;
}
flag = true;
}
} else {
flag = false;
}
return flag;

}

public static void main(String[] args) {
new FramTest();
}
}
tinym87 2012-08-11
  • 打赏
  • 举报
回复
自己写一个继承自JDialog的类,用它作为登录对话框。感觉JOptionPane很难实现你说的效果。

62,615

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧