1

I am making a guessing game in Netbeans Java. The result (message/ String Text) after comparing the guess number to the target number is not showing in the feedback panel TextArea. I passed the text from the MainFrame class using the appendText to the feedbackPanel class. However, the text is not showing in the feedbackPanel after pressing the guess button. Please help me.

MainFrame.java

public class MainFrame extends JFrame{
    FeedbackPanel formPanel;
    InputPanel textPanel;
    public static int target;
    JTextArea ta;
    
    public MainFrame()
    {
    super("HIgher-Lower Game");
        
    formPanel=new FeedbackPanel();
    add(formPanel, BorderLayout.EAST);
       
        textPanel=new InputPanel();
        add(textPanel, BorderLayout.WEST);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
       

        formPanel.setFormListener(new FormListener(){
            public void formEventOccurred(FormEvent e){
                int num = e.getNum();
                //int square=num*num;
                if (num == target)
                    {
                       formPanel.appendText("Congratulations, your guess is correct!");
                        //formPanel.setText("Congratulations, your guess is correct!");
                        //ta.setText("Congratulations, your guess is correct!");
                        //System.out.paMairintln("Congratulations, your guess is correct!");
                    }
                else if (num > target)
                    //if guess is higher than the target
                    {
                        formPanel.appendText("Your guess is higher than the target");
                        //formPanel.setText("Your guess is higher than the target");
                        //ta.setText("Your guess is higher than the target");
                        //System.out.println("Your guess is higher than the target");
                    }
                else if (num < target)
                    //if guess is lower than the target
                    {
                        formPanel.appendText("Your guess is lower than the target");
                        //formPanel.setText("Your guess is lower than the target");
                        //ta.setText("Your guess is lower than the target");
                        //System.out.println("Your guess is lower than the target");
                    }
                //textPanel.appendText(Integer.toString(square));
            } 
        });
        
        textPanel=new InputPanel();
        add(textPanel, BorderLayout.WEST);
                
        setSize(300,300);
    setVisible(true);
    this.pack();
    
    }
     
    public static void main(String[] args) {
        Random random = new Random();
        target = random.nextInt(100);
         
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
        new MainFrame();
            }
    });
    }
}

InputPanel.java

public class InputPanel extends JPanel {
    private JLabel label1;
    private JTextField tfIn;
    private JButton b;
    
    private FormListener formListener;

    public FormListener getTextListener() {
        return formListener;
    }

    public void setFormListener(FormListener formListener) {
        this.formListener = formListener;
    }

    public InputPanel() {
        GridBagLayout gb=new GridBagLayout();
        GridBagConstraints gbc=new GridBagConstraints();
        gbc.insets=new Insets(5,5,5,5);
        setLayout(gb);
        setBorder(BorderFactory.createTitledBorder("InputForm"));
        
        label1 = new JLabel ("Enter number [1-100]");
        gbc.anchor=GridBagConstraints.FIRST_LINE_END;
        gbc.gridx=1;
        gbc.gridy=1;
        gbc.gridheight=1;
        gbc.gridwidth=1;
        add(label1,gbc);
        
        tfIn = new JTextField (10);
        gbc.gridx=1;
        gbc.gridy=2;
        gbc.gridheight=1;
        gbc.gridwidth=1;
        add(tfIn,gbc);
        
        b = new JButton ("Guess");
        gbc.anchor=GridBagConstraints.CENTER;
        gbc.gridx=1;
        gbc.gridy=3;
        gbc.gridheight=1;
        gbc.gridwidth=2;
        add(b,gbc);
        
        b.addActionListener(new ActionListener(){
            public void actionPerformed(ActionEvent e){
                int num = Integer.parseInt(tfIn.getText());
                FormEvent ev=new FormEvent(this,num);
                if(formListener != null){
                    formListener.formEventOccurred(ev);
                }
            }
            
        });
    }
}

FeedbackPanel.java

public class FeedbackPanel extends JPanel{
    JTextArea ta;

    FormListener listen;
        
    public FeedbackPanel(){
        setBorder(BorderFactory.createTitledBorder("Feedback"));
        setLayout(new BorderLayout());
        ta=new JTextArea(5,20);
        
        
        add(new JScrollPane(ta), BorderLayout.CENTER);
    }
    
    public void appendText(String text){
        ta.append(text + "\n" + "\r");
    
    }

    public void setFormListener(FormListener formListener) {
        this.listen = formListener;
    }




 
}

FormEvent.java

public class FormEvent extends EventObject{
    private int num;

    public int getNum() {
        return num;
    }

    public void setNum(int num) {
        this.num = num;
    }
    
    public FormEvent(Object source){
        super(source);
    }
    
    public FormEvent(Object source, int num){
        super(source);
        this.num=num;
    }
}

FormListener.java

public interface FormListener extends EventListener{
    public void formEventOccurred(FormEvent e); 
    
}
Kirby Kode
  • 19
  • 4

1 Answers1

1

Problems in your code:

#1 is, that formEventOccurred() in MainFrame is never called. This problem occurs, because there are two panels, which shall communicate with each other. You solved this by creating an interface, which is correct. But the interface of the wrong class is called.

#2 is, that you created and added the textPanel twice. This should not cause the described problem, but this is not good.

#3 is, that the variables are not named well. Please take a look at naming conventions. This code is not easy to read.

#4 is not really a problem, but the keyword static should not be used if it is not needed.

#5 is, that primitive datatypes and objects should not be declared as class attributes (fields), if they can be local variables instead. This is a lot better for readability.

#6 is, that there is much redundancy in the code. This can occur, if the programmer has problems at reading his own code. If that is the case, that is not directly "bad", that is normal for beginners. The next time, try to write the code more readable by using variable names, which are "telling what their job is", etc.

#7 is, that in general, it is a good practice to make variables "private" until you need to make them public or protected, etc.

And now, have fun by learning!

Working code:

This is a full version of the working code:

Class: Mainframe

public class MainFrame extends JFrame {

    private int numberToGuess;

    public MainFrame() {
        /* general things */
        numberToGuess = new Random().nextInt(100);

        /* feedbackPanel */
        FeedbackPanel feedbackPanel = new FeedbackPanel();
        add(feedbackPanel, BorderLayout.EAST);

        /* inputPanel */
        InputPanel inputPanel = new InputPanel();
        inputPanel.setFormListener(new FormListener() {
            @Override
            public void formEventOccurred(FormEvent e) {
                int guessedNumber = e.getNum();
                if (guessedNumber == numberToGuess) {
                    feedbackPanel.appendText("Congratulations, your guess is correct!");
                } else if (guessedNumber > numberToGuess) {
                    feedbackPanel.appendText("Your guess is higher than the target");
                } else if (guessedNumber < numberToGuess) {
                    feedbackPanel.appendText("Your guess is lower than the target");
                }
            }
        });
        add(inputPanel, BorderLayout.WEST);

        /* frame settings */
        setTitle("Higher-Lower Game");
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setSize(300, 300);
        setVisible(true);
        this.pack();
    }

    public static void main(String[] args) {
        new MainFrame();
    }

}

Class: FeedbackPanel

public class FeedbackPanel extends JPanel {

    private JTextArea textArea;

    public FeedbackPanel() {
        setBorder(BorderFactory.createTitledBorder("Feedback"));
        setLayout(new BorderLayout());
        textArea = new JTextArea(5, 20);
        add(new JScrollPane(textArea), BorderLayout.CENTER);
    }

    public void appendText(String text) {
        textArea.append(text + "\n" + "\r");
    }

}

Class: InputPanel

public class InputPanel extends JPanel {

    private JLabel label1;
    private JTextField tfIn;
    private JButton button;
    private FormListener formListener;

    public InputPanel() {
        GridBagLayout gb = new GridBagLayout();
        GridBagConstraints gbc = new GridBagConstraints();
        gbc.insets = new Insets(5, 5, 5, 5);
        setLayout(gb);
        setBorder(BorderFactory.createTitledBorder("InputForm"));

        label1 = new JLabel("Enter number [1-100]");
        gbc.anchor = GridBagConstraints.FIRST_LINE_END;
        gbc.gridx = 1;
        gbc.gridy = 1;
        gbc.gridheight = 1;
        gbc.gridwidth = 1;
        add(label1, gbc);

        tfIn = new JTextField(10);
        gbc.gridx = 1;
        gbc.gridy = 2;
        gbc.gridheight = 1;
        gbc.gridwidth = 1;
        add(tfIn, gbc);

        button = new JButton("Guess");
        gbc.anchor = GridBagConstraints.CENTER;
        gbc.gridx = 1;
        gbc.gridy = 3;
        gbc.gridheight = 1;
        gbc.gridwidth = 2;
        add(button, gbc);

        button.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                int num = Integer.parseInt(tfIn.getText());
                FormEvent ev = new FormEvent(this, num);
                if (formListener != null) {
                    formListener.formEventOccurred(ev);
                }
            }

        });
    }

    public void setFormListener(FormListener formListener) {
        this.formListener = formListener;
    }
}

Class: FormEvent

public class FormEvent extends EventObject {

    private int num;

    public int getNum() {
        return num;
    }

    public void setNum(int num) {
        this.num = num;
    }

    public FormEvent(Object source) {
        super(source);
    }

    public FormEvent(Object source, int num) {
        super(source);
        this.num = num;
    }
}

Interface: FormListener

public interface FormListener extends EventListener {

    public void formEventOccurred(FormEvent e);

}
David Weber
  • 173
  • 9