Jump to content

How would I set the location of a window in java? e.g. the top corner of the monitor.

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
2 replies to this topic

#1
pathtotake

pathtotake

    Newbie

  • Members
  • PipPip
  • 13 posts
Ive tried

Dimension dim = Toolkit.getDefaultToolkit().getScreenSize();
  
// Get the center of you screen
int w = window.getSize().width;
int h = window.getSize().height;
int x = (dim.width-w)/2;
int y = (dim.height-h)/2;
    
// Move the window
window.setLocation(x, y); 
but doesnt compile.

Anyone know how its done?



Also have a newwww problem here.

I need to push north south east west buttons for the window to move to thr north or the south or the east or the west.
e.g. if i press button - north, the window moves tot he north of the monitor.

Here is the code I have so far, I have code for the north button so far but, I cant get any of it to work.

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
/**
 *
 * @author Sean
 */
public class Compass extends JApplet 
                        implements MouseListener, ActionListener
{
Compass c = new Compass();
JFrame window;

   

    public void actionPerformed(ActionEvent e){
    if (e.getSource() == myButton[0]){
     window = new JFrame();
window.setLocation( 44,44);
window.setVisible( true );

    }
    else if(e.getSource() == myButton[1]){
        
    }
    else if(e.getSource() == myButton[2]){
        
    }
    else if (e.getSource()== myButton[3]){
        
    }
    
}

    public void mouseReleased(MouseEvent e) {
        JButton b =  (JButton)e.getSource();
        
    }
        private JButton myButton[];


    public void init()
    {
        
        
            getContentPane().setLayout(null);

        //myButton = new JButton();      
             getContentPane().setBackground(new Color(20, 205, 239));
             
for (int i =0; i<= myButton.length; i ++){
             myButton[i].addMouseListener(this);
}
             
        myButton = new JButton[5];

        myButton[0] = new JButton("North");
        getContentPane().add(myButton[0]).setSize(100, 50);
        myButton[0].setLocation(130, 10);

        myButton[1] = new JButton("South");
        getContentPane().add(myButton[1]).setSize(100, 50);
        myButton[1].setLocation(130, 120);

        myButton[2] = new JButton("East");
        getContentPane().add(myButton[2]).setSize(100, 50);
        myButton[2].setLocation(260, 60);

        myButton[3] = new JButton("West");
        getContentPane().add(myButton[3]).setSize(100, 50);
        myButton[3].setLocation(0, 60);

        /*myButton[3] = new JButton("Centre");
        getContentPane().add(myButton[3]).setSize(100, 30);
        myButton[3].setLocation(130, 80);*/

    }
}


Edited by pathtotake, 31 March 2010 - 12:13 PM.


#2
pathtotake

pathtotake

    Newbie

  • Members
  • PipPip
  • 13 posts
Well I got the action and mouse listeners working, all i need now is how to set the location of the current window/ frame. Ive got code that creates a new window and sets ITS location but i need to know how to set the location of the window when I am in it and no create a new one.

Any one know?

#3
tate

tate

    Learning Programmer

  • Members
  • PipPipPip
  • 90 posts
Well you are extending JApplet which has its own setLocation method so you can call it for example:
public void setWindowLocation(int x, int y){
    this.setLocation(x,y);
}
"this" is basically just referring to itself which is the JApplet.
Unless i'm misunderstanding what you are trying to do this should work. If you are trying to move the JFrame which you have named window it is still the basic same thing which would be window.setLocation(x,y)
twas brillig