将一个doClick放在JOptionPane中

我是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]); } 

编辑
从你的评论:

按钮部分有效,但我有两个问题。 首先,我认为它不会显示我希望输出如我在问题中所说的那样。 这些词也必须存在。

您需要做的只是放入if / else块或者更好的,一个switch语句根据选择的选项0,1或2输出正确的String。

例如,

 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.../ } 

第二个问题将显示整个代码,然后您可以看到问题。

我看到了你的整个代码,但我没有看到与之相关的具体问题。


编辑2
Ordous的建议是用你的3个按钮创建一个JPanel,给它们ActionListeners,然后将JPanel放到JOptionPane或JDialog中,然后显示它。