代码来自Java核心技术卷一的p398;
就看加了注释那里就好;
我的问题是,showDialog方法里ok一开始设为false,然后在方法里ok的值一直没变,那什么时候这个方法会return true呢?
估计问题很简单,可咱理解不了,呵
谢谢先
import javax.swing.*;
import java.awt.event.*;
import java.awt.*;
import java.awt.geom.*;
import java.util.*;public class FirstExample
{
public static void main(String[] args)
{
    EventQueue.invokeLater(new Runnable()
    {
     public void run()
     {
     ExFrame frame = new ExFrame();
     frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
     frame.setVisible(true);
     }
    });
}
}class ExFrame extends JFrame
{
public ExFrame()
{
setTitle("lala");
setSize(500, 500);

JMenuBar mbar = new JMenuBar();
setJMenuBar(mbar);
JMenu fileMenu = new JMenu("File");
mbar.add(fileMenu);

JMenuItem connectItem = new JMenuItem("Connect");
connectItem.addActionListener(new ConnectAction());
fileMenu.add(connectItem);

JMenuItem exitItem = new JMenuItem("Exit");
exitItem.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent event)
{
System.exit(0);
}
});
fileMenu.add(exitItem);

textArea = new JTextArea();
add(new JScrollPane(textArea), BorderLayout.CENTER);
}

private PasswordChooser dialog = null;
private JTextArea textArea;

private class ConnectAction implements ActionListener
{
public void actionPerformed(ActionEvent event)
{
if (dialog == null) dialog = new PasswordChooser();

dialog.setUser(new User("yourname", null));

//什么时候返回true?
if (dialog.showDialog(ExFrame.this, "Connect"))
{
User u = dialog.getUser();
textArea.append("user name = " + u.getName() + ", password = " + (new String(u.getPassword())) + "\n");
}
}
}
}class PasswordChooser extends JPanel
{
public PasswordChooser()
{
setLayout(new BorderLayout());

JPanel panel = new JPanel();
panel.setLayout(new GridLayout(2, 2));
panel.add(new JLabel("User name:"));
panel.add(username = new JTextField(""));
panel.add(new JLabel("Password:"));
panel.add(password = new JPasswordField(""));
add(panel, BorderLayout.CENTER);

okButton = new JButton("Ok");
okButton.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent event)
{
ok = true;
dialog.setVisible(false);
}
});

JButton cancelButton = new JButton("Cancel");
cancelButton.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent event)
{
dialog.setVisible(false);
}
});

JPanel buttonPanel = new JPanel();
buttonPanel.add(okButton);
buttonPanel.add(cancelButton);
add(buttonPanel, BorderLayout.SOUTH);
}

public void setUser(User u)
{
username.setText(u.getName());
}

public User getUser()
{
return new User(username.getText(), password.getPassword());
}

public boolean showDialog(Component parent, String title)
{
ok = false;

Frame owner = null;
if (parent instanceof Frame) owner = (Frame) parent;
else owner = (Frame) SwingUtilities.getAncestorOfClass(Frame.class, parent);

if (dialog == null || dialog.getOwner() != owner)
{
dialog = new JDialog(owner, true);
dialog.add(this);
dialog.getRootPane().setDefaultButton(okButton);
dialog.pack();
}

dialog.setTitle(title);
dialog.setVisible(true);
return ok; //ok的值在方法里一直没变,怎么会返回true呢?
}

private JTextField username;
private JPasswordField password;
private JButton okButton;
private boolean ok;
private JDialog dialog;
}class User
{
public User(String aName, char[] aPassword)
{
name = aName;
password = aPassword;
}

public String getName()
{
return name;
}

public char[] getPassword()
{
return password;
}

public void setName(String aName)
{
name = aName;
}

public void setPassword(char[] aPassword)
{
password = aPassword;
}

private String name;
private char[] password;
}

解决方案 »

  1.   

    注意看这一段代码:        okButton.addActionListener(new ActionListener()
            {
                public void actionPerformed(ActionEvent event)
                {
                    ok = true;
                    dialog.setVisible(false);
                }
            });
    你给弹出的对话框上的“OK”按钮增加了一个action,在这里,只要按钮被点下,OK值就被设为true了再看这一段代码:        dialog.setTitle(title);
            dialog.setVisible(true);
            return ok; //ok的值在方法里一直没变,怎么会返回true呢?
    在 dialog.setVisible(true);之后。程序并不是立即返回“OK”,而是等待用户动作,然后才返回就在这个用户的动作之中改变了OK的值