gpt4 book ai didi

Java概念想法

转载 作者:搜寻专家 更新时间:2023-10-31 20:22:04 24 4
gpt4 key购买 nike

** 已解决 **

我是 Java 的新手,但到目前为止我很喜欢它!

所以我只是想问问是否有人有想法可以帮助我。所以这就是我想做的。

我现在正在开发的是一个可以与我的本地网站交互(更改标题、内容等)的应用程序。所以我喜欢做的是显示一个 JOptionPane.showConfirmDialog,然后输入用户名和密码。

所以基本上,如果用户名或密码错误,我想显示一个 JOptionPane.showMessageDialog 但是当他们单击 Ok 让他们知道信息有误时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);

最佳答案

正如 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);

关于Java概念想法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12136358/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com