Put a doClick inside a JOptionPane

家住魔仙堡 提交于 2020-01-03 05:58:07

问题


I am a beginner to java and I have some trouble on making the doClick buttons. I don't understand how it works and I need it in my code.

     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"); 

I want the output to look like this:

    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

Update: Here is my whole code in case you need anything else

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 );
    }}

If you have any questions comment below.


回答1:


You can use a better JOptionPane, the 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]);
  }

Edit
From your comments:

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.

All you need to do is put in if/else blocks or perhaps better, a switch statement that outputs the correct String based on which option was selected, 0, 1, or 2.

e.g.,

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.

I see your whole code, but I don't see a specific question pertaining to it.


Edit 2
What Ordous is suggesting is that you create a JPanel with your 3 buttons, give them ActionListeners, and place that JPanel into a JOptionPane or into a JDialog, and display it.



来源:https://stackoverflow.com/questions/24699590/put-a-doclick-inside-a-joptionpane

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!