So I've checked out the other post which didn't help on here, I'm trying to get my frame with it's message to randomly appear on an area on the screen but when I run it, it says x and y cannot be resolved to a variable, here's the code:
public class MyFrame extends JFrame{
MyFrame(int width, int height, int x, int y){
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setTitle("R and Ts Main Frame");
setSize(width, height);
Random random = new Random();
x = random.nextInt();
y = random.nextInt();
setLocation(x, y);
JLabel label = new JLabel("Random Message");
label.setFont(new Font("Impact", Font.BOLD|Font.PLAIN, height/3));
label.setForeground(Color.BLUE);
getContentPane().add(label);
}
}
and this is my main:
public class OurMain {
public static void main(String[] args) {
Dimension sSize = Toolkit.getDefaultToolkit().getScreenSize();
int w = sSize.width;
JFrame f = new MyFrame(w/3, 100, x, y); //my errors are underlined here under the x and y
f.setVisible(true);
}
}
You should just add some attributes and replace some things:
MyFrame:
OurMain:
EDIT:
However,
random.nextInt();
is not a good idea, because your screen width&heigth has a little less then 2^32 pixels... so I would limit it to e.g.:EDIT of EDIT:
This will solve your problem. You did not declare/create
x
andy
before using them, but you don't need them, so just use them locally.Result (it is loaded in a different location each time):