1

I have a question about this GUI code. How to show up on the JLabel the random words?

Basically the purpose of this program is to create a GUI button that when clicked selects a random word from an array and places it into a JLabel.

What I'm having trouble with is getting the words to show up on the JLabel, I'm not quite sure how it should be done.

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

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;

public class GuiQuiz {
    public static void main(String[] args) {

        JFrame frame = new JFrame("Button Example");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        JPanel mainPanel = new JPanel();
        mainPanel.setBackground(Color.BLUE);
        mainPanel.setPreferredSize(new Dimension(500, 500));

        JPanel subPanel1 = new JPanel();
        subPanel1.setBackground(new Color(134, 179, 0));
        subPanel1.setPreferredSize(new Dimension(150, 100));
        subPanel1.add(new JLabel("Random word here"));

        String[] names = { "Class", "Charger", "Pencil", "Dog", "Robot", "Ninja", "Teacher", "Video", "Book",
            "Computer" };


        JButton button = new JButton("Generates a random word");

        button.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                String name = names[(int) (Math.random() * names.length)]; 
                ((JButton) e.getSource()).setText(name);
            }
        });

        mainPanel.add(button);
        mainPanel.add(subPanel1);


        frame.getContentPane().add(mainPanel);
        frame.pack();
        frame.setVisible(true);
    }

}
Frakcool
  • 10,915
  • 9
  • 50
  • 89
Lui
  • 13
  • 3

1 Answers1

1
  1. You're declaring an anonymous JLabel when writing:

    subPanel1.add(new JLabel("Random word here"));
    

    You need to set it to a variable:

    JLabel label = new JLabel("Random word here");
    subPanel1.add(label);
    
  2. Right now you have your button have its own ActionListener:

    button.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            String name = names[(int) (Math.random() * names.length)]; 
            ((JButton) e.getSource()).setText(name);
        }
    });
    

    And you're setting the JButton's text, not the label, so we need to change that as:

    button.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            String name = names[(int) (Math.random() * names.length)]; 
            label.setText(name);
        }
    });
    

That should do what you're trying to do, I haven't tried it but it works in my mind.


Additional tips:

Frakcool
  • 10,915
  • 9
  • 50
  • 89
  • If the answer solved your question be sure to [accept it](https://meta.stackexchange.com/questions/5234/how-does-accepting-an-answer-work) @Luis – Frakcool Apr 30 '19 at 16:53