-5

I have

public void mouseClicked(MouseEvent arg0) {
            Number rs = new Number(); 
            textField_tf.setText(rs.getRandom());
            }

Seperate class

public class Number {

public int getRandom(){ 

Random rand = new Random();

return rand.nextInt(10) + 1;
}


}

When I click the button nothing is generated in the TextField. I'm quite new to programming but if anything could be pointed out where I'm going wrong would be appreciated.

Jdzero
  • 1
  • 1

2 Answers2

1

Based on you limited, out of context, code snippets, I'd suggest that the code can't even compile, as JTextField#setText expects a String not an int

For example...

import java.awt.EventQueue;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.Random;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import javax.swing.border.EmptyBorder;

public class Test {

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

    public Test() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                    ex.printStackTrace();
                }

                JFrame frame = new JFrame("Testing");
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.add(new TestPane());
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }

    public class TestPane extends JPanel {

        private JTextField field;

        public TestPane() {
            setBorder(new EmptyBorder(10, 10, 10, 10));
            field = new JTextField(2);
            JButton btn = new JButton("Randomise");

            setLayout(new GridBagLayout());
            GridBagConstraints gbc = new GridBagConstraints();
            gbc.gridwidth = GridBagConstraints.REMAINDER;

            add(field, gbc);
            add(btn, gbc);

            btn.addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    Number rs = new Number();
                    field.setText(Integer.toString(rs.getRandom()));
                }
            });

            addMouseListener(new MouseAdapter() {
                @Override
                public void mouseClicked(MouseEvent e) {
                    Number rs = new Number();
                    field.setText(Integer.toString(rs.getRandom()));
                }
            });
        }

    }

    public class Number {

        public int getRandom() {
            Random rand = new Random();
            return rand.nextInt(10) + 1;
        }

    }

}
MadProgrammer
  • 343,457
  • 22
  • 230
  • 366
0

You need to move the declaration of 'Ran' outside of main. It needs to be a class level variable in order for the 'getRan' function to be able to access it.

On a separate note, Java convention would be to name that variable with a lowercase first letter.

Edit. That answer applied to a previous version of the question.

Given the new modification to the question:
public class Number {
    public int getRandom() { 
        Random rand = new Random();
        return rand.nextInt(10) + 1;
    }
}

The main problem with this one is declaring a new Random object for each call. You should move the line 'Random rand = new Random()' out of the function, to the class level.

Oscar
  • 334
  • 1
  • 10
  • Ok, so what would I put in the main instead? – Jdzero Mar 05 '17 at 22:44
  • It would look like this: public class Number { Random rand = new Random(); public int getRandom() { return rand.nextInt(10) + 1; } } – Oscar Mar 06 '17 at 23:48
  • Try that code again:`public class Number { Random rand = new Random(); public int getRandom() { return rand.nextInt(10) + 1; } }` – Oscar Mar 06 '17 at 23:56
  • I guess the carriage returns just don't come through in the comments? – Oscar Mar 06 '17 at 23:58