- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我是java初学者,在制作doClick按钮时遇到一些麻烦。我不明白它是如何工作的,我的代码中需要它。
JButton easyb = new JButton("Easy");
JButton mediumb = new JButton("Medium");
JButton hardb = new JButton("Hard");
String fn = JOptionPane.showInputDialog("Choose your level \n", easyb.doClick() ,mediumb.doClick(), hardb.doClick() , "\n \n Easy are words you know everyday \n Medium are words that you have seen before \n Hard are words that you have rarely seen in your life");
我希望输出如下所示:
Choose your level
easy medium hard ( you can click on the words)
Easy are words you have said everyday
Medium are words that you have seen before
Hard are words that you have rarely seen in your life
更新: 这是我的完整代码,以防您需要其他内容
import java.util.*;
import javax.swing.*;
import java.awt.*;
public class SWG1 {
public static void main(String[] args) {
int pizza = 0;
while(pizza == 0){
String[] options = { "Easy", "Medium", "Hard" };
JOptionPane.showMessageDialog(null, "Scrable", "Game", JOptionPane.PLAIN_MESSAGE);
String fn = JOptionPane.showInputDialog("Choose your level \n", easyb.doClick() ,mediumb.doClick(), hardb.doClick() , "\n \n Easy are words you know everyday \n Medium are words that you have seen before \n Hard are words that you have rarley seen in your life");
String[] easy = {"the","car","jump","that","have","with","on","to","you","this","from","game","video","ball","about","which","know","people","year","get","also"};
String[] medium = {"abolish","abuse","absurb","access","accomplish","achievement","aggressive","bland","bungle","challenge","combine","crave","frigid","gorge","hazy","oasis","perish","retire","seldom","tropical","vivid","withdraw"};
String[] hard = {"allusion","bard","characterization","indirect","direct","colloquial","dialect","elegy","farce","genre","humor","jargon","tone","vernacular","unbashed"};
int x = 0;
int chelp = 0;
while(x == 0){
if(fn.equals("Easy") || fn.equals("easy")){
int index = new Random().nextInt(easy.length);
String randomWord = easy[index];
//System.out.println("Random word: '" + randomWord + "'. It is of length: " + randomWord.length());
int c = 0;
while(c == 0){
Random r = new Random();
randomWord = scramble( r, randomWord );
if(randomWord.equals(easy[index])){
c = 0;
}
else{
c = 1;
}
}
int b = 0;
while(b == 0){
String gn = JOptionPane.showInputDialog(randomWord);
if(gn.equals(easy[index])){
JOptionPane.showMessageDialog(null, "Correct", "Good Job", JOptionPane.PLAIN_MESSAGE);
b = 1;
}
else
{
chelp++;
JOptionPane.showMessageDialog(null, "Sorry, you are wrong", "Bad Job", JOptionPane.PLAIN_MESSAGE);
String hn = JOptionPane.showInputDialog("Would you like to quit \n Yes \n No");
if(hn.equals("Yes") || hn.equals("yes")){
b = 1;
}
if(chelp == 3){
}
}
}
}
if(fn.equals("Medium") || fn.equals("medium")){
int index1 = new Random().nextInt(medium.length);
String randomWord1 = medium[index1];
///System.out.println("Random word: '" + randomWord1 + "'. It is of length: " + randomWord1.length());
int q = 0;
while(q == 0){
Random r1 = new Random();
randomWord1 = scramble( r1, randomWord1 );
if(randomWord1.equals(easy[index1])){
q = 0;
}
else{
q = 1;
}
}
int z = 0;
while(z == 0){
String gn1 = JOptionPane.showInputDialog(randomWord1);
if(gn1.equals(easy[index1])){
JOptionPane.showMessageDialog(null, "Correct", "Good Job", JOptionPane.PLAIN_MESSAGE);
z = 1;
}
else
{
JOptionPane.showMessageDialog(null, "Sorry, you are wrong", "Bad Job", JOptionPane.PLAIN_MESSAGE);
String hn1 = JOptionPane.showInputDialog("Would you like to quit \n Yes \n No");
if(hn1.equals("Yes") || hn1.equals("yes")){
z = 1;
}
}
}
}
if(fn.equals("Hard") || fn.equals("hard")){
int index2 = new Random().nextInt(hard.length);
String randomWord2 = hard[index2];
//System.out.println("Random word: '" + randomWord2 + "'. It is of length: " + randomWord2.length());
int h = 0;
while(h == 0){
Random r2 = new Random();
randomWord2 = scramble( r2, randomWord2 );
if(randomWord2.equals(easy[index2])){
h = 0;
}
else{
h = 1;
}
}
int y = 0;
while(y == 0){
String gn2 = JOptionPane.showInputDialog(randomWord2);
if(gn2.equals(easy[index2])){
JOptionPane.showMessageDialog(null, "Correct", "Good Job", JOptionPane.PLAIN_MESSAGE);
y = 1;
}
else
{
JOptionPane.showMessageDialog(null, "Sorry, you are wrong", "Bad Job", JOptionPane.PLAIN_MESSAGE);
String hn2 = JOptionPane.showInputDialog("Would you like to quit \n Yes \n No");
if(hn2.equals("Yes") || hn2.equals("yes")){
y = 1;
}
}
}
}
String quitn = JOptionPane.showInputDialog("Would you like to continue \n Yes \n No");
if(quitn.equals("No") || quitn.equals("no")){
x = 1;
}
}
String quitn1 = JOptionPane.showInputDialog("Would you like to quit the game \n Yes \n No");
if(quitn1.equals("Yes") || quitn1.equals("yes")){
System.exit(1);
}
}
}
public static String scramble( Random random, String inputString )
{
char a[] = inputString.toCharArray();
for( int i=0 ; i<a.length-1 ; i++ )
{
int j = random.nextInt(a.length-1);
// Swap letters
char temp = a[i]; a[i] = a[j]; a[j] = temp;
}
return new String( a );
}}
如果您有任何问题,请在下面评论。
最佳答案
您可以使用更好的 JOptionPane,即 JOptionPane.showOptionDialog
:
String[] options = { "Easy", "Medium", "Hard" };
int selection = JOptionPane.showOptionDialog(null, "Choose Something", "Title",
JOptionPane.OK_CANCEL_OPTION, JOptionPane.PLAIN_MESSAGE, null,
options, options[0]);
// ***** oops! this should be >= 0, not > 0. Corrected!
if (selection >= 0) {
System.out.println("Selected: " + options[selection]);
}
<小时/>
编辑
根据您的评论:
The button part works but I have two problems. The first is that I don't think it will show how I want the output to be like I said in my question. The words need to be there as well.
您需要做的就是放入 if/else block 或更好的 switch 语句,该语句根据选择的选项(0、1 或 2)输出正确的字符串。
例如,
if (selection >= 0) {
String word = options[selection];
case selection 0:
// output stuff based on easy
break;
case selection 1:
// output stuff based on medium selection,....
break;
//.... etc.../
}
The second problem will show the whole code and then you can see the problem.
我看到了您的整个代码,但没有看到与之相关的具体问题。
<小时/>编辑2
Ordous 建议您使用 3 个按钮创建一个 JPanel,为它们提供 ActionListener,然后将该 JPanel 放入 JOptionPane 或 JDialog 中,然后显示它。
关于java - 将 doClick 放入 JOptionPane 中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24699590/
我正在使用 JOptionPane 来获取字符串。单击“确定”即可保存字符串。现在我希望弹出第二个 JOptionPane 以输入第二个必要的字符串。是否有机会将 ActionListener 添加到
我有一个显示来自 JOptionPane 的确认消息的过程。此过程从 JMenuItem 的 Actionlistener 内的 SwingUtilities.invokeLater(runnable
我使用以下代码来获取InputDialog: String c = JOptionPane.showInputDialog("Select number",JOptionPane.OK_OPTION)
正如你从我上面的主题中看到的, 我想知道我怎么能解雇一个 JOptionPane 因为另一个 JOptionPane 而变得无关紧要,因为用户出于某种原因没有通过自己点击确定按钮(例如)解雇第一个。
没有收到“文件已成功接收”消息,请帮助我。将文件从客户端传输到服务器时,即使文件已收到,也不会显示该消息。 这是使用套接字将文件从客户端传输到服务器的程序,接口(interface)也是使用java
JOptionPane.showInputDialog 有一个不需要父组件参数的表单。 JOptionPane.showConfirmDialog 确实如此。这是为什么? 最佳答案 JOptionPa
我正在用 Java 创建一个基于 Tic Tac Toe GUI 的游戏,并且正在努力将 2D 数组与 JOptionPane 结合使用。到目前为止,我已经能够创建可供选择的按钮: import ja
使用 JOprionPane 时,光标出现了一些问题。我将光标设置到 pharent 框架,然后使用这个显示一个对话框: Object[] possibilities = {"ham", "spam"
我创建了一个打开 JOptionPane 的按钮。它允许用户输入 string..>> String str = JOptionPane.showInputDialog 如何获取用户输入到 jopti
该代码正在运行。但问题是,当选择 NO_OPTION 时,窗口就会被释放。我想在选择 NO_OPTION 时保留窗口?你能给点建议吗? int dialogButton = JOptionPa
我正在 Java 上开发 Tic Tac Toe 游戏(eclipse)。在我的计算机上,我的对话框非常小。我一直在努力把它做得更大。我没有任何运气。我希望这里有人能引导我走向正确的方向。下面的代码是
我正在使用脚本 API 为我玩的游戏的机器人制作脚本,但是每当我覆盖机器人管理器时,就会出现一个 JOptionPane 来阻止执行,直到我关闭它,但是我想在不运行该脚本的情况下运行此脚本人为干预,所
我正在用 Java 开发一个游戏,我试图在 JOptionPane 确认框打开时暂停它。这是代码: window.addWindowListener(new WindowAdapter() {
这个问题已经有答案了: Calling one JFrame from another using Timer without any buttons (1 个回答) 已关闭 9 年前。 需要帮助制作
很抱歉代码这么长,我只需要知道我缺少什么来初始化我的所有决定。编译器提示我的变量没有被初始化。谢谢你。 import javax.swing.JOptionPane; public class Sem
我想使用 JOptionPane 处理一些异常。这是主要方法: public class MainRun { public static void main(String args[]){
不允许使用数组,该函数正在工作,但只是返回 0,就好像它没有计算正确的输入字符一样,但现在它给了我一个“字符串超出范围:3” 这应该运行,打开一个窗口,要求我输入一个字符串,在本例中是一个单词,然后打
我正在使用 Jfilechooser,如果我选择文件,它将计算文件名的字符数,但是如果文件超过 3kb,它将限制 Joptionpane 将显示。我的问题是即使文件是0kb,Joptionpane也会
我创建了一个应用于框架的名为 addSupplier 的按钮,然后创建了一个操作监听器,因此一旦按下 addSupplier 按钮,它将创建一个 JOptionPane,其中有一个附加了 JTextF
我知道解决方案是使用 for 循环来逐步遍历数组并显示在 Pane 中。但是我没有找到对此的直接解释。我需要一个下一个和一个上一个按钮来显示每个数组元素,并且在按下下一个按钮时到达第一个元素后仅返回到
我是一名优秀的程序员,十分优秀!