忘写了一部分,重写如下
void jPasswordField1_focusLost(FocusEvent e) {
String kk=new String(jPasswordField1.getPassword()); if(kk.equals("")){
JOptionPane.showMessageDialog(this,"请输入密码","提示", JOptionPane.ERROR_MESSAGE); System.out.println("AAA");
jPasswordField1.requestFocus(); }
}
void jPasswordField1_focusLost(FocusEvent e) {
String kk=new String(jPasswordField1.getPassword()); if(kk.equals("")){
JOptionPane.showMessageDialog(this,"请输入密码","提示", JOptionPane.ERROR_MESSAGE); System.out.println("AAA");
jPasswordField1.requestFocus(); }
}
void jPasswordField1_focusLost(FocusEvent e) {String kk=new String(jPasswordField1.getPassword());if(kk.equals("")){
JOptionPane.showMessageDialog(this,"请输入密码","提示", JOptionPane. ERROR_MESSAGE); System.out.println("AAA"); jPasswordField1.requestFocus();
}
}
确定后输出AAA,然后退出if,焦点返回到密码框
这段代码没有错。
我试过了,没有出现你所说的对话框关不掉的问题
奇怪了
我给你我试过的代码,点击jbutton2触发你给的代码。
jbutton1退出界面。
程序没有错误
import javax.swing.*;
import java.awt.*;
import com.borland.jbcl.layout.*;
import java.awt.event.*;
public class Test extends JDialog {
JPanel jPanel1 = new JPanel();
XYLayout xYLayout1 = new XYLayout();
JButton jButton1 = new JButton();
JPasswordField jPasswordField1 = new JPasswordField();
JButton jButton2 = new JButton();
public static void main(String[] args) {
Test alpha = new Test();
alpha.show();
} public Test() {
try {
this.setSize(400,300);
jbInit();
}
catch(Exception e) {
e.printStackTrace();
}
}
private void jbInit() throws Exception {
jPanel1.setLayout(xYLayout1);
jButton1.setText("jButton1");
jButton1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(ActionEvent e) {
jButton1_actionPerformed(e);
}
}); jButton2.setText("jButton2");
jButton2.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(ActionEvent e) {
jButton2_actionPerformed(e);
}
});
this.getContentPane().add(jPanel1, BorderLayout.CENTER);
jPanel1.add(jButton1, new XYConstraints(153, 189, -1, -1));
jPanel1.add(jPasswordField1, new XYConstraints(132, 78, 128, -1));
jPanel1.add(jButton2, new XYConstraints(42, 80, -1, -1));
}
void jButton1_actionPerformed(ActionEvent e) {
this.dispose();
System.exit(0);
} void jButton2_actionPerformed(ActionEvent e) {
String kk = new String(jPasswordField1.getPassword());
if(kk.equals("")){
JOptionPane.showMessageDialog(this,"请输入密码","提示", JOptionPane. ERROR_MESSAGE);
System.out.println("AAA");
jPasswordField1.requestFocus();
}
}
}
让焦点一开始就在jPasswordField1上??
要不你将jPanel1.add(jPasswordField1,null)这条添加语句放在所有的add语句之前就行。程序默认第一个add添加的控件获得焦点
import java.awt.event.*;
import javax.swing.*;public class backframe extends JFrame
{
public static void main(String[] args)
{
backframe frame = new backframe();
frame.setDefaultCloseOperation(DISPOSE_ON_CLOSE);
frame.addWindowListener(new WindowAdapter()
{
public void windowClosed(WindowEvent e)
{
System.exit(0);
}
});
}
JPasswordField jp = new JPasswordField();
JButton btn = new JButton("test");
public backframe()
{
getContentPane().setLayout(new FlowLayout(FlowLayout.CENTER));
getContentPane().add(jp);
jp.setColumns(10);
getContentPane().add(btn);
this.setTitle("Test Password");
this.setVisible(true);
this.pack();
//this.setBounds(30,30,450,350);
btn.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
aa();
}
});
}
void aa()
{
String kk=new String(jp.getPassword());
if(kk.equals(""))
{
JOptionPane.showMessageDialog(this,"请输入密码","提示",JOptionPane.ERROR_MESSAGE);
System.out.println("AAA");
jp.requestFocus();
}
}
}
jPanel1.add(jButton1, new XYConstraints(153, 189, -1, -1));
和 jPanel1.add(jPasswordField1, new XYConstraints(132, 78, 128, -1));换一下就可以了,就是先添加jPasswordField1,再添加别的,java没有象vc里那样指定焦点顺序,估计就是按添加顺序。