Java概念理念

5

** 已解决 **

我对Java比较新,但到目前为止我很喜欢它!

所以我想问一下,有没有人能提供帮助。我现在正在开发一个可以与我的本地网站(更改标题、内容等)交互的应用程序。因此,我想显示一个JOptionPane.showConfirmDialog并输入用户名和密码。

如果用户名或密码错误,我想显示JOptionPane.showMessageDialog,但是当他们单击确定来告诉他们信息错误时,showConfirmDialog消失!

有什么好主意吗?这是我的代码。

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.sql.*;

public class javaTesting extends JFrame {
    public JFrame mrFrame;
    public int enter;
    public JPanel mrPanel;

    public javaTesting() throws Exception
    {
            Class.forName("com.mysql.jdbc.Driver");
            try {
                Connection con =  DriverManager.getConnection("jdbc:mysql://localhost:3306/cms","root","");
            } catch (SQLException e){
                System.out.println(e.getMessage());
            }
            mrFrame = new JFrame();
            mrPanel = new JPanel();

            mrPanel.setLayout(new GridLayout(4,1));

            JLabel user = new JLabel("Username");
            mrPanel.add(user);

            JTextField user_input = new JTextField(30);
            mrPanel.add(user_input);

            JLabel pass = new JLabel("Password");
            mrPanel.add(pass);

            JTextField pw_input = new JPasswordField(30);
            mrPanel.add(pw_input);

            mrFrame.setSize(700,700);
            mrFrame.setLocationRelativeTo(null);
            mrFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            //mrFrame.setVisible(true);
            mrFrame.setResizable(false);

            input();

            if(enter == JOptionPane.OK_OPTION)
            {
                JOptionPane.showMessageDialog(null, "You clicked ok!");
                input();
            } else {
                System.exit(1);
            }
    }
    public void input()
    {
         enter = (int) JOptionPane.showConfirmDialog(mrFrame,mrPanel,"Login Cridantiels",JOptionPane.OK_CANCEL_OPTION,JOptionPane.QUESTION_MESSAGE);
    }
    public static void main(String agrs[]) throws Exception
    {
        new javaTesting();
    }
}

这是我所做的,对于我来说似乎很好,不知道是否正确。但它能够正常工作:]

 do{
        input();

        if(enter == JOptionPane.OK_OPTION)
        {
            JOptionPane.showMessageDialog(null, "You clicked ok!");
        } else {
            System.exit(1);
        }
     } while(enter != JOptionPane.CANCEL_OPTION);

5
基本上,你需要一个循环来处理它... - shan
@shan 谢谢,我用了 do{}while() 循环,现在可以正常工作了。感谢! - Timothy Montanez
1
还有一个提示。在Java中,所有的类名都应该以大写字母开头 :) 如果你是新手,这很重要哦 ;) - christian.vogel
1个回答

3
正如Shan所建议的那样,您需要循环收集凭据的部分代码。以下是一个示例:
JPanel pnlDetails = new JPanel(new GridBagLayout());
JTextField userNameField = new JTextField(10);
JPasswordField passwordField = new JPasswordField(10);

GridBagConstraints gbc = new GridBagConstraints();
gbc.gridx = 0;
gbc.gridy = 0;
gbc.anchor = GridBagConstraints.WEST;
gbc.insets = new Insets(2, 2, 2, 2);

pnlDetails.add(new JLabel("User name:"), gbc);
gbc.gridy++;
pnlDetails.add(new JLabel("Password:"), gbc);

gbc.gridx = 1;
gbc.gridy = 0;
gbc.anchor = GridBagConstraints.EAST;
pnlDetails.add(userNameField, gbc);
gbc.gridy++;
pnlDetails.add(passwordField, gbc);

// The result from the dialog, will be OK or CANCEL
int operation;
// Flag used to determine if the credentials are okay or not
boolean badCredentials = true;
do {

    operation = JOptionPane.showConfirmDialog(null, pnlDetails, "Login", JOptionPane.OK_CANCEL_OPTION, JOptionPane.INFORMATION_MESSAGE);

    String userName = userNameField.getText();
    char[] password = passwordField.getPassword();

    // You would valid you credintals here :P
    if (userName.equals("super") && new String(password).equals("happy")) {

        badCredentials = false;

    } else if (operation != JOptionPane.CANCEL_OPTION) {

        JOptionPane.showMessageDialog(null, "Invalid Credentials", "Error", JOptionPane.ERROR_MESSAGE);

    }

} while (operation != JOptionPane.CANCEL_OPTION && badCredentials);

if (!badCredentials && operation != JOptionPane.CANCEL_OPTION) {

    System.out.println("Continue program");

} else {

    System.out.println("Exit program");

}

System.exit(0);

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接