3

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);

    }
}
gooly bug
  • 135
  • 1
  • 6
  • Don't use `null` layouts. – John May 14 '15 at 00:05
  • Try removing JButton b1 = new JButton("START"); from the init() method as you have already declared it inside the class. Or instead, declare the variable as it is, but inside init() initialise it. Also inside your action performed method. Don't initialise a new variable, instead assign a value to that already declare inside the class. You also need to call repaint() to update the app – Alan Kavanagh May 14 '15 at 00:06
  • I could not find a layout that includes the ability of setting the coordinates and sizes of buttons. Also Alan, I tried everything you said but it still will not work. Still, thanks for your help. – gooly bug May 14 '15 at 00:21
  • It has worked for me by simply reusing the variables you are declaring and adding the listener and assignments to the correct objects. Remove the JButton b1 = new JButton() from the init() method and move your action listener underneath b1.setBounds(). Call repaint() inside your action listener and add super.paintComponents(g) to the paint method – Alan Kavanagh May 14 '15 at 00:31
  • 2
    @Alan NEVER suggest to ANYBODY to call `super.paintComponents` from within any method other then `paintComponents`, you've just circumvented the painting process which could result in unwanted painting glicthes – MadProgrammer May 14 '15 at 00:34
  • thankyou Alan, this worked very well! – gooly bug May 14 '15 at 00:37
  • @goolybug although my solution worked - take note of what MadProgrammer has suggested (particularly regarding not calling paintComponents inside the paint method). – Alan Kavanagh May 14 '15 at 00:40
  • I will make sure to look into it. – gooly bug May 14 '15 at 00:48

1 Answers1

4
  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:

for more details

Community
  • 1
  • 1
MadProgrammer
  • 343,457
  • 22
  • 230
  • 366