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.


Sign In
Create Account


Back to top









