Using setLocation to move the JFrame around Windows, Java

Tom C picture Tom C · Feb 20, 2014 · Viewed 11k times · Source

I am trying to move a JFrame around in Windows using 5 buttons (North, East, South, West, and Centre) At the moment all the current code is in place and it works when using;

public void actionPerformed(ActionEvent e)
{
    if(e.getSource()==northButton)
    {
       setLocation(500,500);
    }

} //works

public void actionPerformed(ActionEvent e)
{
    if(e.getSource()== northButton)
    {
       setLocation(north);
    }

} //doesn't work

However as part of the task I need to use the Java Toolkit to getScreenSize width and height and using calculations to work out the boundaries of the screen and send 'north' to setLocation() (like above). However, using this method it throws an error "No suitable method found" I'm unsure of how to fix this. The calculation code is below just for north at the moment.

int screenWidth = Toolkit.getDefaultToolkit().getScreenSize().width;
int screenHeight = Toolkit.getDefaultToolkit().getScreenSize().height;

int width = this.getWidth();
int height = this.getHeight();

int north = ((screenWidth - width)/2);

Any help will be greatly appreciated. Thanks!

Answer

martinez314 picture martinez314 · Feb 20, 2014

Here you are passing two parameters, X and Y:

setLocation(500,500);

Here you are passing one, but which is it? X or Y? If X, where's Y?:

int north = ((screenWidth - width)/2);
setLocation(north);

The compiler is telling you it doesn't have a setLocation() method that takes one parameter. It wants a location in 2D space: that's an X and Y. Probably what you want is:

setLocation(north, 0);