当前位置: 技术问答>java相关
简单问题,问题简简单单
来源: 互联网 发布时间:2015-06-22
本文导语: String kk=new String(jPasswordField1.getPassword()); if(kk.equals("")){ JOptionPane.showMessageDialog(this,"请输入密码","提示", JOptionPane. ERROR_MESSAGE); System.out.println("AAA"); jPasswordField1.requestFocus(); } 为什么弹出对话框后...
String kk=new String(jPasswordField1.getPassword());
if(kk.equals("")){
JOptionPane.showMessageDialog(this,"请输入密码","提示", JOptionPane. ERROR_MESSAGE);
System.out.println("AAA");
jPasswordField1.requestFocus();
}
为什么弹出对话框后,关不掉呢?好象是if语句一直循环似的
每次确定,总是显示AAA
怎么办??
if(kk.equals("")){
JOptionPane.showMessageDialog(this,"请输入密码","提示", JOptionPane. ERROR_MESSAGE);
System.out.println("AAA");
jPasswordField1.requestFocus();
}
为什么弹出对话框后,关不掉呢?好象是if语句一直循环似的
每次确定,总是显示AAA
怎么办??
|
这可能是由于密码文本框反复失去焦点造成的,很可能和一些焦点转移事件及其相关代码有关。建议你仔细分析一下requestFocus()方法的实现机理,另外仔细检查所有事件Listener的实现代码。
|
你如果没有在密码框中输入数据,必定弹出这个对话框,
确定后输出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();
}
}
}
确定后输出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();
}
}
}
|
代码没有问题的,你看看我的代码!!!
import java.awt.*;
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();
}
}
}
import java.awt.*;
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里那样指定焦点顺序,估计就是按添加顺序。
jPanel1.add(jButton1, new XYConstraints(153, 189, -1, -1));
和 jPanel1.add(jPasswordField1, new XYConstraints(132, 78, 128, -1));换一下就可以了,就是先添加jPasswordField1,再添加别的,java没有象vc里那样指定焦点顺序,估计就是按添加顺序。
|
在jPasswordField1.requestFocus();之前先判断一下对话框的返回值,然后在让jPasswordField1得到焦点。
您可能感兴趣的文章:
本站(WWW.)旨在分享和传播互联网科技相关的资讯和技术,将尽最大努力为读者提供更好的信息聚合和浏览方式。
本站(WWW.)站内文章除注明原创外,均为转载、整理或搜集自网络。欢迎任何形式的转载,转载请注明出处。
本站(WWW.)站内文章除注明原创外,均为转载、整理或搜集自网络。欢迎任何形式的转载,转载请注明出处。