Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I make this JButton work

I am working on a code that will generate a random number when you press a button and output that number. I have wrote this code and it compiles but when I press the button nothing works. Can someone please help. Here is some of my code.

public class slotmachine extends JApplet {

    JButton b1 = new JButton("START");
    JPanel p;
    int Int1;

    public slotmachine() {
        init();

    }

    public void init() {

        this.setLayout(null);
        this.setSize(1000, 1000);

        JButton b1 = new JButton("START");
        b1.setBounds(100, 100, 100, 100);

        getContentPane().add(b1);
        repaint();

    }

    public void run() {
        b1.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent e) {
                Random random1 = new Random();
                int Int1 = random1.nextInt(11);

            }

        });
    }

    public void paint(Graphics g) {

        g.drawString("Your number is" + Int1, 30, 30);

    }
}
like image 596
gooly bug Avatar asked Jan 01 '26 01:01

gooly bug


1 Answers

  1. Avoid using null layouts, pixel perfect layouts are an illusion within modern ui design. There are too many factors which affect the individual size of components, none of which you can control. Swing was designed to work with layout managers at the core, discarding these will lead to no end of issues and problems that you will spend more and more time trying to rectify
  2. You create a local variable of Int1 within the ActionListener for the button. This has no relationship to the Int1 of the class.
  3. You never tell the UI to update
  4. You break the paint chain by failing to call super.paint (be ready for some seriously weird and wonderful graphics glitches)
  5. You've made the same mistake with b1 as you have with Int1. You create an instance level field, but shadow it with a local variable in init, meaning when start is called, b1 is null, which will result in a NullPointerxception

Instead, add a JLabel to your applet, use it's setText method to display the random value

b1.addActionListener(new ActionListener() {

    public void actionPerformed(ActionEvent e) {
        Random random1 = new Random();
        int Int1 = random1.nextInt(11);
        lable.setText(Integer.toString(Int1));
    }

});

Also, if possible, I'd avoid using JApplet, they have their own set of issues which can make life more difficult then it needs to be when learning the Swing API. Instead, try using a JPanel for your main container and then add it to an instance of a JFrame.

Also, take a look at:

  • Understanding Class Members for more information about local and class/instance context for variables
  • How to Use Labels
  • Why is it frowned upon to use a null layout in SWING? and Laying Out Components Within a Container
  • And if your really interested in how painting works, Performing Custom Painting and Painting in AWT and Swing

for more details

like image 141
MadProgrammer Avatar answered Jan 04 '26 22:01

MadProgrammer



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!