Joption游戏逻辑

时间:2014-04-20 18:07:48

标签: java

我已经建立了使用Joption的想法,如果有人能帮助我,我将不胜感激

 import javax.swing.ImageIcon;
import javax.swing.JOptionPane;

 public class TestOptionPane {

public static void main(String[] args) {
    ImageIcon icon = new ImageIcon(
            TestOptionPane.class.getResource("/resources/android.jpg"));
    String answer = (String)JOptionPane.showInputDialog(
            null, "Spell the Image", "Spell the Image", 
            JOptionPane.QUESTION_MESSAGE, icon, null, null);

    if (answer.equals("Android")) {
        System.out.println("Yayyy");
        System.exit(0);
    }
    }

2 个答案:

答案 0 :(得分:0)

这取决于您正在使用的平台。例如,如果您正在使用Android,则必须更改Word构造函数并添加Bitmap类型。例如,

public class Word{

private String name;

private String imagePath;

public Word(String _name, String _imagePath){
    this.name = _name;
    this.imagePath= _imagePath;
}

}

示例:

Word newWord = new Word("Pizza", "path/image.png");

但最后,您可以更改Bitmap类型以获取所需内容,然后将其添加到Map或您的LinkedList

编辑:我在评论中读到你正在做GUI。您必须使用String type,并将path添加到构建器中。

答案 1 :(得分:0)

由于您的程序只是一个控制台程序,并且您没有Swing的经验,因此我将为您提供一个简单的解决方案,以便在JOptionPane

中显示图像

您可以做的是Word采取ImageIcon

private ImageIcon icon;
private String name;

Word(String name, String type, ImageIcon icon) {
    this.icon = icon;
}

public ImageIcon getIcon() {
    return icon;
}

public String getName() {
    return name;
}

您可以在Word中创建WordRepository

ImageIcon appleIcon = new ImageIcon(WordRepository.class.getResource("/resources/apple.png"));
Word apple = new Word("apple", "food", appleIcon);

您的文件结构应该是什么样的

ProjectRoot
         src
             resources
                     apple.png

然后,当您想要在程序中显示它时,您只需在JOptionPane中显示它

Word word = wordRepository.getRandomWord();
ImageIcon icon = word.getIcon();

String answer = (String)JOptionPane.showInputDialog(
                 null, "Spell the Image", "Spell the Image", 
                 JOptionPane.QUESTION_MESSAGE, icon, null, null);

if (answer.equals(word.getName())) {
    System.out.println("Yayyy");
}

有关我传递给它的参数的详细信息,请参阅JOptionPane API


您可以将其用作简单的测试程序。只需正确更改图像的路径,并将输入的字符串等于

import javax.swing.ImageIcon;
import javax.swing.JOptionPane;

public class TestOptionPane {

    public static void main(String[] args) {
        ImageIcon icon = new ImageIcon(
                TestOptionPane.class.getResource("/resources/android.jpg"));
        String answer = (String)JOptionPane.showInputDialog(
                null, "Spell the Image", "Spell the Image", 
                JOptionPane.QUESTION_MESSAGE, icon, null, null);

        if (answer.equals("Android")) {
            System.out.println("Yayyy");
            System.exit(0);
        }
    }
}

enter image description here