gpt4 book ai didi

java - 将 doClick 放入 JOptionPane 中

转载 作者:行者123 更新时间:2023-12-02 05:38:53 26 4
gpt4 key购买 nike

我是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/

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