首页 > 解决方案 > 为什么我的字符串数组在我输入到 JLabel 时不起作用?

问题描述

我正在尝试创建一个类似于俄勒冈小道这样的路径选择游戏。我试图找出为什么当我尝试创建一个 JLabel 来显示文本时,我将其设置为的文本是另一个类中的一个数组,称为StoryLine. 每当我将标签添加到我的框架时,文本都不会在屏幕上弹出。我已将文本更改为"hey"并且它有效,但由于某种原因它不适用于我的数组。

考虑这段代码:

public static void intro() {
        JFrame frame = new JFrame("Lost Boy");
        JLabel textbox = Components.Textbox();
        JLabel bed = Components.Bedroom();
        JLabel text = Components.TextFormat(StoryLine.story[2]);

        frame.setSize(1000, 600);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        text.setBounds(10, -70,400,200);

        frame.add(text);
        frame.add(textbox);
        frame.add(bed);
        frame.setLayout(null);
        frame.setVisible(true);
    }

Components.TextFormat

 public static JLabel TextFormat(String stry) {
        JLabel text = new JLabel();
        text.setText(stry);
        text.setFont(MyFont());
        text.setForeground(Color.WHITE);
        return text;
    }

StoryLine.string[]

 public static void Script() {

        story[2] = "You're just putting on your shoes before you walk out the\n"
                + "door. You see your dog sitting at the door wagging his tail\n"
                + "expecting to come. Do you choose to bring him?"; 

标签: javaswingjlabel

解决方案


推荐阅读