有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

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/安卓.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) 个答案

  1. # 1 楼答案

    因为您的程序只是一个控制台程序,而且您没有Swing方面的经验,所以我将为您提供一个在JOptionPane中显示图像的简单解决方案

    你能做的就是让你WordImageIcon

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

    您可以在WordRepository类中创建Word

    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

  2. # 2 楼答案

    这取决于你工作的平台。例如,如果您正在为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添加到构造函数的映像中