getLocation() problem when using -Dawt.toolkit=sun.awt.X11.XToolkit
under JDK1.5.0-p2
Huang wen hui
hwh at gddsn.org.cn
Tue Dec 27 06:44:34 PST 2005
hi,
When JFrame resize, JFrame.getLocation() will return same location.
using MToolkit or using JDK1.4.2 do
not have this problem. there is the little samples:
#cat WelcomeFrame.java
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class WelcomeFrame extends JFrame {
JPanel contentPane;
BorderLayout borderLayout1 = new BorderLayout();
JMenuBar jMenuBar1 = new JMenuBar();
JMenu jMenuFile = new JMenu();
JMenuItem jMenuFileExit = new JMenuItem();
JMenu jMenuHelp = new JMenu();
JMenuItem jMenuHelpAbout = new JMenuItem();
/**
* Construct the frame
*/
public WelcomeFrame() {
enableEvents(AWTEvent.WINDOW_EVENT_MASK);
try {
jbInit();
}
catch (Exception e) {
e.printStackTrace();
}
}
/**
* Component initialization
*
* @throws Exception exception
*/
private void jbInit() throws Exception {
contentPane = (JPanel) this.getContentPane();
contentPane.setLayout(borderLayout1);
this.setTitle("Welcome to JBuilder");
this.addComponentListener(new ComponentAdapter() {
public void componentResized(ComponentEvent e) {
this_componentResized(e);
}
});
this.setJMenuBar(jMenuBar1);
this.setSize(new Dimension(400, 300));
jMenuFile.setText("File");
jMenuFileExit.setText("Exit");
jMenuFileExit.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(ActionEvent e) {
jMenuFileExit_actionPerformed(e);
}
});
jMenuFile.add(jMenuFileExit);
jMenuHelp.setText("Help");
jMenuHelpAbout.setText("About");
jMenuHelpAbout.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(ActionEvent e) {
jMenuHelpAbout_actionPerformed(e);
}
});
jMenuHelp.add(jMenuHelpAbout);
jMenuBar1.add(jMenuFile);
jMenuBar1.add(jMenuHelp);
}
/**
* Overridden so we can exit when window is closed
*
* @param e WindowEvent
*/
protected void processWindowEvent(WindowEvent e) {
super.processWindowEvent(e);
if (e.getID() == WindowEvent.WINDOW_CLOSING) {
System.exit(0);
}
}
/**
* File | Exit action performed
*
* @param e ActionEvent
*/
public void jMenuFileExit_actionPerformed(ActionEvent e) {
System.exit(0);
}
/**
* Help | About action performed
*
* @param e ActionEvent
*/
public void jMenuHelpAbout_actionPerformed(ActionEvent e) {
}
public void this_componentResized(ComponentEvent e) {
if(this.isShowing()){
System.out.println("JFrame location: " + this.getLocation());
System.out.println("JFrame locationOnScreen: " +
this.getLocationOnScreen());
}
}
}
#cat WelcomeApp.java
import java.awt.*;
import javax.swing.UIManager;
public class WelcomeApp {
boolean packFrame = false;
/**
* Construct the application
*/
public WelcomeApp() {
WelcomeFrame frame = new WelcomeFrame();
//Pack frames that have useful preferred size info, e.g. from their layout
//Validate frames that have preset sizes
if (packFrame)
frame.pack();
else
frame.validate();
// Center the frame
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
Dimension frameSize = frame.getSize();
if (frameSize.height > screenSize.height)
frameSize.height = screenSize.height;
if (frameSize.width > screenSize.width)
frameSize.width = screenSize.width;
frame.setLocation((screenSize.width - frameSize.width) / 2,
(screenSize.height - frameSize.height) / 2);
frame.setVisible(true);
}
/**
* Main method
*
* @param args String[]
*/
static public void main(String[] args) {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
}
catch(Exception e) {
e.printStackTrace();
}
new WelcomeApp();
}
}
output:
JFrame location: java.awt.Point[x=600,y=450]
JFrame locationOnScreen: java.awt.Point[x=600,y=450]
JFrame location: java.awt.Point[x=600,y=450]
JFrame locationOnScreen: java.awt.Point[x=0,y=34]
JFrame location: java.awt.Point[x=600,y=450]
JFrame locationOnScreen: java.awt.Point[x=600,y=450]
More information about the freebsd-java
mailing list