-1

Our goal is to create a slot machine with 2 panels and it seemed that the button is not working and not displaying anything in the text field on the panel. The text field in the panel should display the random number generator when the button is clicked. I tried the Action Listener code but it is not doing anything as well. Please help! Thank you!

Here is my output

package slotMachineMain;


import java.awt.Color;
import java.awt.Dimension;
import java.awt.Image;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Random;

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



public class slot1 extends JFrame implements ActionListener {
    
    JButton button1;
    JTextField text1;
    
    slot1(){
        
        Image icon = Toolkit.getDefaultToolkit().getImage("D:\\11.png");
        ImageIcon image1 = new ImageIcon("D:\\12.png");
        ImageIcon image2 = new ImageIcon("D:\\13.gif");
        ImageIcon image3 = new ImageIcon("D:\\14.png");

        
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.setTitle("Take your Chance Here!");
        
        JLabel label1 = new JLabel();
        label1.setIcon(image1);
        label1.setBounds(120, 10, 138, 87);
        
        JLabel label2 = new JLabel();
        label2.setIcon(image2);
        label2.setBounds(0, 0, 400, 290);
        

        JTextField text1 = new JTextField();
        text1.setPreferredSize(new Dimension(80,80));
        text1.setEditable(false);
        
        JTextField text2 = new JTextField();
        text2.setPreferredSize(new Dimension(80,80));
        text2.setEditable(false);

        
        JPanel panel1 = new JPanel();
        panel1.setBackground(Color.white);
        panel1.setBounds(83, 100, 90, 90);
        panel1.add(text1);
        
        JPanel panel2 = new JPanel();
        panel2.setBackground(Color.white);
        panel2.setBounds(207, 100, 90, 90);
        panel2.add(text2);
        
        JButton button1 = new JButton();
        button1.setBounds(152, 200, 75, 25);
        button1.setIcon(image3);
        button1.addActionListener(this);
        
        this.setResizable(false);
        this.setSize(400, 290);
        this.setLayout(null);
        this.setIconImage(icon);
        this.add(panel1);
        this.add(panel2);
        this.add(button1);
        this.add(label1);
        this.add(label2);
        this.setVisible(true);

    }

    @Override
    public void actionPerformed(ActionEvent e) {
        Random rn = new Random(); 
        if(e.getSource()==button1) {
            text1.setText(Integer.toString(rn.nextInt()));
        }
        
    }

}

  • Java GUIs have to work on different OS', screen size, screen resolution etc. using different PLAFs in different locales. As such, they are not conducive to pixel perfect layout. Instead use layout managers, or [combinations of them](http://stackoverflow.com/a/5630271/418556) along with layout padding and borders for [white space](http://stackoverflow.com/a/17874718/418556). – Andrew Thompson Feb 24 '21 at 11:01

1 Answers1

0

You referenced text1 and button1 in the constructor. So, when you click on the button the button is not button1, and text1 is null (their scope didn't changed when the constructor flow is over).

You need to change

JTextField text1 = new JTextField();

to

text1 = new JTextField();

and

JButton button1 = new JButton();

to

button1 = new JButton();
Roie Shlosberg
  • 189
  • 1
  • 7