0

Help is needed :) My friends and I are coding hangman in school for a project, and we're really stumped on one part. We made a JFrame and an outer JPanel (Background), then divided it into two columns (the left column is functional as we like). Our problem lies in the right column, rightPanel. We need to set up a JLabel so we can have the lines correlating to the word length a player chooses, but our JLabel doesn't work. I've tried to just do a simple System.out.println("test"); and nothing shows up within the panel (blankSpaces). The layout has been changed as well just to try and make something show up. We also tried to set the JLabel within a method which would be called in the panel blankSpaces, but nothing seems to work. These are just two of the classes we have for the game, so if any other code is needed for explanation please let me know :)

//the rightPanel

import java.awt.*;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.*;

public class RightPanel extends JPanel {
    //unnecessary code rn
    Guess dalia = new Guess();
    WordSelection clementine = new WordSelection();
    private String letterGuess = "";
    public JTextField jt;
    //where we tried to make a test or just declare the label
    private JLabel label3;
    JLabel test; 
    private JPanel parent;

    JPanel panel3;
    //constructor
    public RightPanel() {
        this.setLayout(new GridLayout(0, 1));
        this.add(letters(parent));
        this.add(lettersGuessed(parent));
        this.add(blankSpaces(parent));
        this.add(notLetterButtons(parent));

    }
    //where the alphabet is supposed to be but we just made it using coordinates
    public JPanel letters(JPanel Parent) {
        GridLayout layout1 = new GridLayout(1, 1);
        JPanel panel1 = new JPanel(layout1);

        return panel1;
    }

    // where the player will guess letters

    public JPanel lettersGuessed(JPanel parent) {
        GridLayout layout2 = new GridLayout(1, 1);
        JPanel panel2 = new JPanel(layout2);
        panel2.add(generateTextField());

        return panel2;
    }
    //textfield that is currently useless
    public JTextField generateTextField() {
        // TODO Auto-generated method stub
        // JLabel jl = new JLabel();
        jt = new JTextField(1);
        jt.setFont(new Font("Verdana", 1, 30));
        jt.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent arg0) {
                    // TODO Auto-generated method stub
                letterGuess = jt.getText().substring(0, 1);
                // jl.setText(input);
            }
        });

        return jt;
    }
    //where we coded the alphabet
    @Override
    public void paint(Graphics g) {

        int y = this.getHeight();
        int x = this.getWidth();
        // super.paint(g);
        String firstLine = "A B C D E F G";
        String secondLine = "H I J K L M N";
        String thirdLine = "O P Q R S T U";
        String fourthLine = "V W X Y Z";

        g.setFont(new Font("Verdana", 1, 30));
        // alphabet
        g.drawString(firstLine, x / 3, y / 25);
        g.drawString(secondLine, x / 3, y / 12);
        g.drawString(thirdLine, x / 3, y / 8);
        g.drawString(fourthLine, (int) (x / 2.65), y / 6);

    }

    // where the blank spaces in the amount of letters to be guessed will appear
    public JPanel blankSpaces(JPanel parent) {
        GridLayout layout3 = new GridLayout(1, 1);
        panel3 = new JPanel(layout3);
    //    panel3.add(label3);

        /*test = new JLabel("dalia is my bffl");
        test.setLayout(new BorderLayout());

        test.setVerticalTextPosition(JLabel.CENTER);
        test.setHorizontalTextPosition(JLabel.CENTER);
        test.setVisible(true);
        panel3.add(test);*/
        return panel3;
    }
    // where player will be able to choose if they want a new word

    public JPanel notLetterButtons(JPanel parent) {
        // int rows = 2; int cols = 4;
        GridLayout layout4 = new GridLayout(2, 4);
        JPanel panel4 = new JPanel(layout4);
        /*

        JPanel[][] parent = new JPanel[2][4];
        Container f1 = new Container();
        f1.setLayout(new GridLayout(2, 4));
        for(int m = 0; m < 2; m++) {
            for(int n = 0; n < 4; n++) {
                parent[m][n] = new JPanel();
                f1.add(parent[m][n]);
            }
        }*/

        int y = this.getHeight();
        int x = this.getWidth();

        //new button
        JButton button1 = new JButton();
        button1.setText("QUIT");
        button1.addActionListener(e -> quitAction() );
        panel4.add(button1);

        //new button
        JButton button2 = new JButton();
        button2.setText("SUBMIT");
        button2.addActionListener(e -> submitAction() );
        panel4.add(button2);

        //new button
        JButton button3 = new JButton();
        button3.setText("QUOTE");

        panel4.add(button3);
        //new button
        JButton button4 = new JButton();
        button4.setText("FRENCH");

        panel4.add(button4);

        //new button
        JButton button5 = new JButton();
        button5.setText("5");
        button5.addActionListener(e -> fiveAction() );
        panel4.add(button5);

        //new button
        JButton button6 = new JButton();
        button6.setText("6");
        button6.addActionListener(e -> sixAction() );
        panel4.add(button6);

        //new button
        JButton button7 = new JButton();
        button7.setText("7");
        button7.addActionListener(e -> sevenAction() );
        panel4.add(button7);

        //new button
        JButton button8 = new JButton();
        button8.setText("RESTART");

        panel4.add(button8);


        return panel4;

        // panel4.add(gt);
        // gt.pack();
        // gt.setVisible(true);
    //    return f1;
    }

    private Object fiveAction() {
        clementine.wordPick(5);
        //String underline = "";
        //for (int x = 0; x < 5; x++) {
        //    underline += "_ ";
        //}
        JLabel lbl = new JLabel ( "i hate this");
         label3.setText("_ _ _ _ _");
        panel3.add(label3);
        return label3;
    }
    private Object sixAction() {
        clementine.wordPick(6);
        JLabel label6 = new JLabel("_ _ _ _ _ _");

        return label6;
    }
    private Object sevenAction() {
        clementine.wordPick(7);
        return null;
    }
    private JButton submitAction() {

        String letterGuess = jt.getText();
        dalia.runThrough();
        return null;
    }

    private Object quitAction() {
        System.exit(0);
        return null;
    }

}

//Background class

import javax.imageio.ImageIO;
import javax.swing.*;
import java.awt.*;
import java.awt.image.*;

public class Background extends JPanel {

    private static final int FRAME = 400;

    //private Dimension rightPanelSize;

    HangmanPicture hangmanPicture = new HangmanPicture();
    RightPanel rightPanel = new RightPanel();

    public Background(JFrame frame) {

        this.setLayout(new GridLayout(0, 2));
        this.setPreferredSize(frame.getSize());
        this.add(hangmanPicture);
        this.setPreferredSize(frame.getSize());
        Dimension frameSize = frame.getSize();

        this.add(rightPanel);
        this.setPreferredSize(frame.getSize());
        //double up = height / 4;
        //double down = height * (3 / 4);

    //  int width = (int) (frameSize.getWidth()/2);
        //int height = (int) (frameSize.getHeight());
        //this.rightPanelSize = new Dimension(width, height);

    }// heyy 
    enter code here


}

Thank you!!

Turkey
  • 1
  • A lot of code, not a real question, but: You're overriding `public void paint(Graphics g)`, which will destroy the painting mechanism. You should usually override `paintComponent` to do custom painting, and usually, the first line should then be `super.paintComponent(g)`. (This might already solve the issue, but until now, it's only a hint/guess...) – Marco13 May 13 '18 at 22:00
  • Sorry, I guess I didn't really ask a question. We've tried to override paintComponent but overriding paint works better for what we're doing for some reason. My question: is there something in the code we have that is not allowing the JLabel to function or are we missing something? Thanks – Turkey May 14 '18 at 02:10
  • Frankly, the code is just a mess. If you have a question or a specific problem, try to create a [MCVE] – Marco13 May 14 '18 at 06:58

0 Answers0