Tell me more ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I have my GUI set up the way I want it now I'm trying to output a string called AnswerKey i have predefined in dashReplace() I tried to draw the string and use JLabel but i just can't seem to find the right method of doing such.

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

}
share|improve this question

1 Answer

up vote 2 down vote accepted

The method is called answerKey, not AnswerKey. In fact, this surely won't compile as you've currently written it - you're trying to assign a variable to something while using that variable as a constructor parameter. It should be:

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

To make this work, however, you'll need to make the method non-static. Also, having a variable named AnswerKey and a method named answerKey is just crying out for confusion - I'd suggest a better name for the JLabel.

share|improve this answer
 
Thank you so much worked like a charm :) –  Sage1216 May 9 at 12:53
1  
@Sage1216: As Yuushi points out (1+ to his answer) you will want to learn and adhere to Java naming conventions, including being sure that variable and method names begin with a lower-case letter and class names with an upper-case letter. This is important, especially if you are desiring that others (i.e., your instructors and us) easily and fully understand your code so that we can more easily help you (or grade you). Good luck! –  Hovercraft Full Of Eels May 9 at 12:58
 
thank you... I will try to adhere to Java naming conventions in the future at the moment though i only have three day to get this done and I've never used a GUI interface before... I don't know if i have time to rename my methods and classes –  Sage1216 May 9 at 13:37

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.