Java中的GUI问题。请帮忙。谢谢 [英] GUI problems in Java. Pls help. Thanks
问题描述
我已经制作了这段代码,输出应该显示一个要求输入密码的对话框。然后,如果用户正确输入它,则显示有效。否则它显示无效。然而它唯一不好的对话框输入密码。它没有显示是否有效。我犯了什么错?非常感谢。
PS:我还是java的初学者
这是我写的代码片段:
导入javax.swing。*;
import java.awt。*;
import java .awt.event。*;
import java.util.Arrays;
公共类GeographyQuiz_Menu
{
public static void main(String [] args)
{
JFrame jFrame;
jFrame = new JFrame();
>
JOptionPane.showMessageDialog(jFrame," This is a Geography Quiz");
JOptionPane.showMessageDialog(null,Good Luck);
String yourChoice;
char choice;
int i,choice1;
int密码;
扫描仪键盘=新扫描仪(System.in);
JOptionPane.showInputDialog(jFrame,输入密码);
密码= keyboard.nextInt();
if(密码== 123)
{
JOptionPane.showMessageDialog(null,有效。您输入了正确的密码。现在从以下菜单中选择);
}
else
{
JOptionPane.showMessageDialog(null," Invalid Password。再试一次。);
}
}
}
这里是你的问题:
密码= keyboard.nextInt();
u更好地使用包装类Integer函数parseInt。永远不要使用带有对话框的扫描仪,它不会工作。
它需要一个字符串并将其转换为数学操作的整数。
无论如何尝试这个,我希望它是什么你需要。
import javax.swing。*;
import java.awt。*;
import java.awt.event。*;
import java.util。*;
公共类GeographyQuiz_Menu
{
public static void main(String [] args)
{
JFrame jFrame;
jFrame = new JFrame();
JOptionPane.showMessageDialog(jFrame," This is a Geography Quiz");
JOptionPane.showMessageDialog(null,祝你好运);
String yourChoice;
char choice;
int i,choice1;
int密码;
字符串passString;
扫描仪键盘=新扫描仪(System.in);
passString = JOptionPane.showInputDialog (输入密码);
//密码= passString.nextInt();
密码= Integer.parseInt(passString );
if(密码== 123)
{
JOptionPane.showMessageDialog(null,"有效。您输入了正确的密码。现在从以下菜单中选择);
}
else
{
JOptionPane。 showMessageDialog(null,无效密码。再试一次。);
}
}
}
非常感谢!你很棒! : - )
非常感谢!你很棒! :-)
随时欢迎;)
I have made this piece of code where the output should display a dialogue box asking for a password. Then if the user enters it correctly it displays "Valid" or else it displays "Not Valid". However its only dispalying the dialogue box to enter the password. It isn''t displaying whether its valid or not. What have i made wrong? Thanks a lot.
PS: I''m still a beginner in java
This is the piece of code i have written:
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.Arrays;
public class GeographyQuiz_Menu
{
public static void main(String[] args)
{
JFrame jFrame;
jFrame = new JFrame();
JOptionPane.showMessageDialog(jFrame, "This is a Geography Quiz");
JOptionPane.showMessageDialog(null, "Good Luck");
String yourChoice;
char choice;
int i, choice1;
int Password;
Scanner keyboard = new Scanner(System.in);
JOptionPane.showInputDialog(jFrame, "Enter the Password");
Password = keyboard.nextInt();
if (Password == 123)
{
JOptionPane.showMessageDialog(null, "Valid. You typed the right password. Now choose from the following menu");
}
else
{
JOptionPane.showMessageDialog(null, "Invalid Password. Try Again.");
}
}
}
here is ur problem:
Password = keyboard.nextInt();
u better use the wrapper class Integer which has the function parseInt. Never use a scanner with dialog boxes, it wont work.
It takes a string and convert it into an integer for maths manipulations.
anyway try this and i hope its what u need.
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.*;
public class GeographyQuiz_Menu
{
public static void main(String[] args)
{
JFrame jFrame;
jFrame = new JFrame();
JOptionPane.showMessageDialog(jFrame, "This is a Geography Quiz");
JOptionPane.showMessageDialog(null, "Good Luck");
String yourChoice;
char choice;
int i, choice1;
int Password;
String passString;
Scanner keyboard = new Scanner(System.in);
passString = JOptionPane.showInputDialog("Enter the Password");
//Password = passString.nextInt();
Password = Integer.parseInt(passString);
if (Password == 123)
{
JOptionPane.showMessageDialog(null, "Valid. You typed the right password. Now choose from the following menu");
}
else
{
JOptionPane.showMessageDialog(null, "Invalid Password. Try Again.");
}
}
}
Thanks a lot! You''r Great! :-)
Thanks a lot! You''r Great! :-)ur welcome anytime ;)
这篇关于Java中的GUI问题。请帮忙。谢谢的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!