将doClick放在JOptionPane中 [英] Put a doClick inside a JOptionPane

查看:72
本文介绍了将doClick放在JOptionPane中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是Java的初学者,在制作doClick按钮时遇到了一些麻烦.我不明白它是如何工作的,我在代码中需要它.

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.

推荐答案

您可以使用更好的JOptionPane,即JOptionPane.showOptionDialog:

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.

您需要做的所有事情都放在if/else块中,或者说是更好的选择中,一条switch语句根据选择的0、1或2输出正确的String.

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.

例如

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.

编辑2
Ordous的建议是,使用3个按钮创建一个JPanel,为它们提供ActionListener,然后将该JPanel放入JOptionPane或JDialog中,然后显示它.

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.

这篇关于将doClick放在JOptionPane中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆