【问题标题】:using a predefined String in a GUI在 GUI 中使用预定义的字符串
【发布时间】:2013-05-09 12:31:14
【问题描述】:

我已经按照我想要的方式设置了我的 GUI 现在我正在尝试输出一个名为 AnswerKey 的字符串,我在 dashReplace() 中预定义了我尝试绘制字符串并使用 JLabel 但我似乎找不到这样做的正确方法。

import java.awt.*;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;

import javax.swing.*;



public class HangmanPanel extends JPanel {
    private static final long serialVersionUID = -5793357804828609325L;

    public static String answerKey() {
        //get random array element
        String array[] = new String[10];
        array[0] = "hamlet";
        array[1] = "mysts of avalon";
        array[2] = "the iliad";
        array[3] = "tales from edger allan poe";
        array[4] = "the children of hurin";
        array[5] = "the red badge of courage";
        array[6] = "of mice and men";
        array[7] =  "utopia"; 
        array[8] =  "chariots of the gods";
        array[9] =  "a brief history of time";

        ArrayList<String> list = new ArrayList<String>(Arrays.asList(array));
        Collections.shuffle(list);
        String s = list.get(0);
        return s;
    }

    public static StringBuilder dashReplace(String s) {
        //replace non-white space char with dashes and creates StringBuilder Object
        String tW = s.replaceAll("\\S", "-"); 
        System.out.print(tW + "\n");  
        StringBuilder AnswerKey = new StringBuilder(tW);
        return AnswerKey;
    }




    public HangmanPanel(){
        this.setLayout(null);

        JLabel heading = new JLabel("Welcome to the Hangman App");
        JButton Button = new JButton("Ok");
        //Button.addActionListener((ActionListener) this);  
        JLabel tfLable = new JLabel("Please Enter a Letter:");

        //trying to out put predefined string
        JLabel AnswerKey = new JLabel(AnswerKey);

        JTextField text = new JTextField(10);
        //String input = text.getText();

        heading.setSize(200, 50);
        tfLable.setSize(150, 50);
        text.setSize(50, 30);
        Button.setSize(60, 20);

        heading.setLocation(300, 10);
        tfLable.setLocation(50, 40);
        text.setLocation(50, 80);
        Button.setLocation(100, 85);

        this.add(heading);
        this.add(tfLable);
        this.add(text);
        this.add(Button);
    }

}

【问题讨论】:

    标签: java string user-interface output


    【解决方案1】:

    该方法称为answerKey,而不是AnswerKey。事实上,这肯定不会像您当前编写的那样编译 - 您正在尝试将变量分配给某物,同时将该变量用作构造函数参数。应该是:

    JLabel AnswerKey = new JLabel(dashReplace(answerKey()).toString());
    

    但是,要使此工作正常进行,您需要将方法设为非static。此外,拥有一个名为 AnswerKey 的变量和一个名为 answerKey 的方法只会让人感到困惑 - 我建议为 JLabel 取一个更好的名称。

    【讨论】:

    • @Sage1216:正如 Yuushi 指出的那样(他的回答是 1+),您需要学习并遵守 Java 命名约定,包括确保变量和方法名称以小写字母开头,并且带有大写字母的类名。这一点很重要,尤其是如果您希望其他人(即您的教师和 我们)能够轻松且完全地理解您的代码,以便我们可以更轻松地帮助您(或给您评分)。祝你好运!
    • 谢谢...我将在未来尝试遵守 Java 命名约定,尽管我只有三天的时间来完成这项工作,而且我以前从未使用过 GUI 界面.. . 我不知道我是否有时间重命名我的方法和类
    猜你喜欢
    • 2012-11-18
    • 1970-01-01
    • 2011-08-04
    • 2018-08-20
    • 1970-01-01
    • 2019-06-06
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多