代码的运行都没问题,只是调用这个类打开的是一个frame,我现在希望将frame改为模态对话框的方式,请大家帮忙改一下代码!本人刚学Swing,实在不熟悉。// UserCheck.java
// A simple label/field form panel
//
package org.jivesoftware.sparkimpl.plugin.filetransfer.transfer.ui;
import javax.swing.*;import org.jivesoftware.Spark;
import org.jivesoftware.resource.Res;
import org.jivesoftware.resource.SparkRes;
import org.jivesoftware.smack.util.StringUtils;
//import org.jivesoftware.smackx.filetransfer.FileTransferRequest;
import org.jivesoftware.spark.SparkManager;
import org.jivesoftware.spark.ui.ContactItem;
import org.jivesoftware.spark.ui.ContactList;
import org.jivesoftware.spark.util.ByteFormat;
import org.jivesoftware.spark.util.GraphicUtils;
import org.jivesoftware.spark.util.log.Log;import java.awt.*;
import java.awt.event.*;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Arrays;
import org.jivesoftware.smackx.filetransfer.FileTransferManager;
import org.jivesoftware.smackx.filetransfer.FileTransferRequest;
import org.jivesoftware.smackx.filetransfer.OutgoingFileTransfer;
/* PasswordDemo.java requires no other files. */public class UserCheck extends JPanel
                          implements ActionListener {
    private static String OK = "ok";
    private static String HELP = "cancel";  //  @jve:decl-index=0:    private JFrame controllingFrame; //needed for dialogs
    private JPasswordField passwordField;
    
    public int correct_value =0;
    public boolean isEnter =false;
    //final FileTransferRequest request;  //  @jve:decl-index=0:    public UserCheck(JFrame f) {
        //Use the default FlowLayout.
        controllingFrame = f;        //request = new FileTransferRequest();
        //Create everything.
        passwordField = new JPasswordField(10);
        passwordField.setActionCommand(OK);
        passwordField.addActionListener(this);        JLabel label = new JLabel("请输入密码: ");
        label.setLabelFor(passwordField);
        JComponent buttonPane = createButtonPanel();        //Lay out everything.
        JPanel textPane = new JPanel(new FlowLayout(FlowLayout.TRAILING));
        textPane.add(label);
        textPane.add(passwordField);        add(textPane);
        add(buttonPane);
    }    protected JComponent createButtonPanel() {
        JPanel p = new JPanel(new GridLayout(0,1));
        JButton okButton = new JButton("OK");
        //JButton helpButton = new JButton("Help");        okButton.setActionCommand(OK);
        //helpButton.setActionCommand(HELP);
        okButton.addActionListener(this);
        //helpButton.addActionListener(this);        p.add(okButton);
        //p.add(helpButton);        return p;
    }
    
    
    public void actionPerformed(ActionEvent e) {
        String cmd = e.getActionCommand();        
        if (OK.equals(cmd)) { //Process the password.
         //当前输入的密码
            char[] input = passwordField.getPassword();
         //String input = passwordField.getPassword();
            if (isPasswordCorrect(input)) {
                JOptionPane.showMessageDialog(controllingFrame,
                    "Success! You typed the right password.");
                                
              
                
            } else {
                JOptionPane.showMessageDialog(controllingFrame,
                    "Invalid password. Try again.",
                    "Error Message",
                    JOptionPane.ERROR_MESSAGE);
            }            //Zero out the possible password, for security.
            Arrays.fill(input, '0');            passwordField.selectAll();
            resetFocus();
        } else { //The user has asked for help.
                    
        }
           }    /**
     * Checks the passed-in array against the correct password.
     * After this method returns, you should invoke eraseArray
     * on the passed-in array.
     */
    private static boolean isPasswordCorrect(char[] input) {
        boolean isCorrect = true;
                String correctPassword;
        //获取当前用户的密码
        correctPassword = SparkManager.getSessionManager().getPassword();
       //转换为char类型
        char[] correctpwd=correctPassword.toCharArray(); 
        //输入的密码和当前用户的密码一致
        if (input.length != correctpwd.length) {
            isCorrect = false;
        } else {
            isCorrect = Arrays.equals (input, correctpwd);
        }        //Zero out the password.
        Arrays.fill(correctpwd,'0');        return isCorrect;
    }
        //Must be called from the event dispatch thread.
    protected void resetFocus() {
        passwordField.requestFocusInWindow();
    }    /**
     * Create the GUI and show it.  For thread safety,
     * this method should be invoked from the
     * event dispatch thread.
     */
    public static void createAndShowGUI() {
        //Create and set up the window.
        JFrame frame = new JFrame("用户验证");
        frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        frame.setLocationRelativeTo(null);
       
        //Create and set up the content pane.
        final UserCheck newContentPane = new UserCheck(frame);
        newContentPane.setOpaque(true); //content panes must be opaque
        frame.setContentPane(newContentPane);        //Make sure the focus goes to the right component
        //whenever the frame is initially given the focus.
        frame.addWindowListener(new WindowAdapter() {
            public void windowActivated(WindowEvent e) {
                newContentPane.resetFocus();
            }
        });        //Display the window.
        frame.pack();
        frame.setVisible(true);
    }}

解决方案 »

  1.   

    public static void createAndShowGUI(Frame parentFrame) {
            //Create and set up the window.
            JDialog frame = new JDialog ("用户验证", parentFrame);
            frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
            frame.setLocationRelativeTo(null);
         
            //Create and set up the content pane.
            final UserCheck newContentPane = new UserCheck(frame);
            newContentPane.setOpaque(true); //content panes must be opaque
            frame.getContentPane().add(newContentPane);        //Make sure the focus goes to the right component
            //whenever the frame is initially given the focus.
            frame.addWindowListener(new WindowAdapter() {
                public void windowActivated(WindowEvent e) {
                    newContentPane.resetFocus();
                }
            });        //Display the window.
            frame.pack();
            frame.setVisible(true);
        }
      

  2.   

    没试过,你测试一下吧。
    frame.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE); 
      

  3.   

    你将UserCheck类继承JFrame 改为JDialog