下面列出了java.awt.Window#getLocationOnScreen ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
public static void main(final String[] args) throws AWTException {
final Window window = new BackgroundIsNotUpdated(null);
window.setSize(300, 300);
window.setLocationRelativeTo(null);
window.setVisible(true);
sleep();
window.setBackground(Color.GREEN);
sleep();
final Robot robot = new Robot();
robot.setAutoDelay(200);
Point point = window.getLocationOnScreen();
Color color = robot.getPixelColor(point.x + window.getWidth() / 2,
point.y + window.getHeight() / 2);
window.dispose();
if (!color.equals(Color.GREEN)) {
throw new RuntimeException(
"Expected: " + Color.GREEN + " , Actual: " + color);
}
}
public static void main(final String[] args) throws AWTException {
final Window window = new BackgroundIsNotUpdated(null);
window.setSize(300, 300);
window.setLocationRelativeTo(null);
window.setVisible(true);
sleep();
window.setBackground(Color.GREEN);
sleep();
final Robot robot = new Robot();
robot.setAutoDelay(200);
Point point = window.getLocationOnScreen();
Color color = robot.getPixelColor(point.x + window.getWidth() / 2,
point.y + window.getHeight() / 2);
window.dispose();
if (!color.equals(Color.GREEN)) {
throw new RuntimeException(
"Expected: " + Color.GREEN + " , Actual: " + color);
}
}
/**
* shows the url in the static window, sizes and positions the window
* accordingly
*/
private void showInTextWindow() throws IOException {
window.setVisible(false);
// same height as parent window of the button, positioned next to it on left or right side
Window ancestor = SwingUtilities.getWindowAncestor(this);
window.setPreferredSize(new Dimension(WINDOW_WIDTH, Math.max(ancestor.getHeight(), WINDOW_HEIGHT)));
int screenEnd = ancestor.getGraphicsConfiguration().getBounds().width + ancestor.getGraphicsConfiguration().getBounds().x;
Point ancestorLocation = ancestor.getLocationOnScreen();
if(ancestorLocation.x + ancestor.getWidth() + window.getPreferredSize().width < screenEnd) {
window.setLocation(ancestorLocation.x + ancestor.getWidth(), ancestorLocation.y);
} else {
window.setLocation(ancestorLocation.x - window.getPreferredSize().width, ancestorLocation.y);
}
//set page shown in browser
if(url != null && !url.equals(htmlBrowser.getPage())) {
try {
htmlBrowser.setPage(url);
} catch(Exception e) {
htmlBrowser.setText("Could not load help file");
}
}
window.pack();
window.setVisible(true);
}
public static void main(final String[] args) throws AWTException {
final Window window = new BackgroundIsNotUpdated(null);
window.setSize(300, 300);
window.setLocationRelativeTo(null);
window.setVisible(true);
sleep();
window.setBackground(Color.GREEN);
sleep();
final Robot robot = new Robot();
robot.setAutoDelay(200);
Point point = window.getLocationOnScreen();
Color color = robot.getPixelColor(point.x + window.getWidth() / 2,
point.y + window.getHeight() / 2);
window.dispose();
if (!color.equals(Color.GREEN)) {
throw new RuntimeException(
"Expected: " + Color.GREEN + " , Actual: " + color);
}
}
public static void main(final String[] args) throws AWTException {
final Window window = new BackgroundIsNotUpdated(null);
window.setSize(300, 300);
window.setLocationRelativeTo(null);
window.setVisible(true);
sleep();
window.setBackground(Color.GREEN);
sleep();
final Robot robot = new Robot();
robot.setAutoDelay(200);
Point point = window.getLocationOnScreen();
Color color = robot.getPixelColor(point.x + window.getWidth() / 2,
point.y + window.getHeight() / 2);
window.dispose();
if (!color.equals(Color.GREEN)) {
throw new RuntimeException(
"Expected: " + Color.GREEN + " , Actual: " + color);
}
}
/**
* display the warning log
*/
void displayWarningLog(Window w) {
ToolDialog wd = new ToolDialog
(PolicyTool.getMessage("Warning"), tool, this, true);
// find the location of the PolicyTool gui
Point location = ((w == null) ?
getLocationOnScreen() : w.getLocationOnScreen());
//wd.setBounds(location.x + 50, location.y + 50, 500, 100);
wd.setLayout(new GridBagLayout());
JTextArea ta = new JTextArea();
ta.setEditable(false);
for (int i = 0; i < tool.warnings.size(); i++) {
ta.append(tool.warnings.elementAt(i));
ta.append(PolicyTool.getMessage("NEWLINE"));
}
addNewComponent(wd, ta, 0,
0, 0, 1, 1, 0.0, 0.0, GridBagConstraints.BOTH,
BOTTOM_PADDING);
ta.setFocusable(false);
JButton okButton = new JButton(PolicyTool.getMessage("OK"));
ActionListener okListener = new CancelButtonListener(wd);
okButton.addActionListener(okListener);
addNewComponent(wd, okButton, 1,
0, 1, 1, 1, 0.0, 0.0, GridBagConstraints.VERTICAL,
LR_PADDING);
wd.getRootPane().setDefaultButton(okButton);
wd.getRootPane().registerKeyboardAction(okListener, escKey, JComponent.WHEN_IN_FOCUSED_WINDOW);
wd.pack();
wd.setLocationRelativeTo(w);
wd.setVisible(true);
}
/**
* displays a dialog box describing an error which occurred.
*/
void displayErrorDialog(Window w, String error) {
ToolDialog ed = new ToolDialog
(PolicyTool.getMessage("Error"), tool, this, true);
// find where the PolicyTool gui is
Point location = ((w == null) ?
getLocationOnScreen() : w.getLocationOnScreen());
//ed.setBounds(location.x + 50, location.y + 50, 600, 100);
ed.setLayout(new GridBagLayout());
JLabel label = new JLabel(error);
addNewComponent(ed, label, 0,
0, 0, 1, 1, 0.0, 0.0, GridBagConstraints.BOTH);
JButton okButton = new JButton(PolicyTool.getMessage("OK"));
ActionListener okListener = new ErrorOKButtonListener(ed);
okButton.addActionListener(okListener);
addNewComponent(ed, okButton, 1,
0, 1, 1, 1, 0.0, 0.0, GridBagConstraints.VERTICAL);
ed.getRootPane().setDefaultButton(okButton);
ed.getRootPane().registerKeyboardAction(okListener, escKey, JComponent.WHEN_IN_FOCUSED_WINDOW);
ed.pack();
ed.setLocationRelativeTo(w);
ed.setVisible(true);
}
/**
* displays a dialog box describing the status of an event
*/
void displayStatusDialog(Window w, String status) {
ToolDialog sd = new ToolDialog
(PolicyTool.getMessage("Status"), tool, this, true);
// find the location of the PolicyTool gui
Point location = ((w == null) ?
getLocationOnScreen() : w.getLocationOnScreen());
//sd.setBounds(location.x + 50, location.y + 50, 500, 100);
sd.setLayout(new GridBagLayout());
JLabel label = new JLabel(status);
addNewComponent(sd, label, 0,
0, 0, 1, 1, 0.0, 0.0, GridBagConstraints.BOTH);
JButton okButton = new JButton(PolicyTool.getMessage("OK"));
ActionListener okListener = new StatusOKButtonListener(sd);
okButton.addActionListener(okListener);
addNewComponent(sd, okButton, 1,
0, 1, 1, 1, 0.0, 0.0, GridBagConstraints.VERTICAL);
sd.getRootPane().setDefaultButton(okButton);
sd.getRootPane().registerKeyboardAction(okListener, escKey, JComponent.WHEN_IN_FOCUSED_WINDOW);
sd.pack();
sd.setLocationRelativeTo(w);
sd.setVisible(true);
}
/**
* display the warning log
*/
void displayWarningLog(Window w) {
ToolDialog wd = new ToolDialog
(PolicyTool.getMessage("Warning"), tool, this, true);
// find the location of the PolicyTool gui
Point location = ((w == null) ?
getLocationOnScreen() : w.getLocationOnScreen());
//wd.setBounds(location.x + 50, location.y + 50, 500, 100);
wd.setLayout(new GridBagLayout());
JTextArea ta = new JTextArea();
ta.setEditable(false);
for (int i = 0; i < tool.warnings.size(); i++) {
ta.append(tool.warnings.elementAt(i));
ta.append(PolicyTool.getMessage("NEWLINE"));
}
addNewComponent(wd, ta, 0,
0, 0, 1, 1, 0.0, 0.0, GridBagConstraints.BOTH,
BOTTOM_PADDING);
ta.setFocusable(false);
JButton okButton = new JButton(PolicyTool.getMessage("OK"));
ActionListener okListener = new CancelButtonListener(wd);
okButton.addActionListener(okListener);
addNewComponent(wd, okButton, 1,
0, 1, 1, 1, 0.0, 0.0, GridBagConstraints.VERTICAL,
LR_PADDING);
wd.getRootPane().setDefaultButton(okButton);
wd.getRootPane().registerKeyboardAction(okListener, escKey, JComponent.WHEN_IN_FOCUSED_WINDOW);
wd.pack();
wd.setLocationRelativeTo(w);
wd.setVisible(true);
}
/**
* displays a dialog box describing an error which occurred.
*/
void displayErrorDialog(Window w, String error) {
ToolDialog ed = new ToolDialog
(PolicyTool.getMessage("Error"), tool, this, true);
// find where the PolicyTool gui is
Point location = ((w == null) ?
getLocationOnScreen() : w.getLocationOnScreen());
//ed.setBounds(location.x + 50, location.y + 50, 600, 100);
ed.setLayout(new GridBagLayout());
JLabel label = new JLabel(error);
addNewComponent(ed, label, 0,
0, 0, 1, 1, 0.0, 0.0, GridBagConstraints.BOTH);
JButton okButton = new JButton(PolicyTool.getMessage("OK"));
ActionListener okListener = new ErrorOKButtonListener(ed);
okButton.addActionListener(okListener);
addNewComponent(ed, okButton, 1,
0, 1, 1, 1, 0.0, 0.0, GridBagConstraints.VERTICAL);
ed.getRootPane().setDefaultButton(okButton);
ed.getRootPane().registerKeyboardAction(okListener, escKey, JComponent.WHEN_IN_FOCUSED_WINDOW);
ed.pack();
ed.setLocationRelativeTo(w);
ed.setVisible(true);
}
/**
* displays a dialog box describing the status of an event
*/
void displayStatusDialog(Window w, String status) {
ToolDialog sd = new ToolDialog
(PolicyTool.getMessage("Status"), tool, this, true);
// find the location of the PolicyTool gui
Point location = ((w == null) ?
getLocationOnScreen() : w.getLocationOnScreen());
//sd.setBounds(location.x + 50, location.y + 50, 500, 100);
sd.setLayout(new GridBagLayout());
JLabel label = new JLabel(status);
addNewComponent(sd, label, 0,
0, 0, 1, 1, 0.0, 0.0, GridBagConstraints.BOTH);
JButton okButton = new JButton(PolicyTool.getMessage("OK"));
ActionListener okListener = new StatusOKButtonListener(sd);
okButton.addActionListener(okListener);
addNewComponent(sd, okButton, 1,
0, 1, 1, 1, 0.0, 0.0, GridBagConstraints.VERTICAL);
sd.getRootPane().setDefaultButton(okButton);
sd.getRootPane().registerKeyboardAction(okListener, escKey, JComponent.WHEN_IN_FOCUSED_WINDOW);
sd.pack();
sd.setLocationRelativeTo(w);
sd.setVisible(true);
}
/**
* displays a dialog box describing the status of an event
*/
void displayStatusDialog(Window w, String status) {
ToolDialog sd = new ToolDialog
(PolicyTool.getMessage("Status"), tool, this, true);
// find the location of the PolicyTool gui
Point location = ((w == null) ?
getLocationOnScreen() : w.getLocationOnScreen());
//sd.setBounds(location.x + 50, location.y + 50, 500, 100);
sd.setLayout(new GridBagLayout());
JLabel label = new JLabel(status);
addNewComponent(sd, label, 0,
0, 0, 1, 1, 0.0, 0.0, GridBagConstraints.BOTH);
JButton okButton = new JButton(PolicyTool.getMessage("OK"));
ActionListener okListener = new StatusOKButtonListener(sd);
okButton.addActionListener(okListener);
addNewComponent(sd, okButton, 1,
0, 1, 1, 1, 0.0, 0.0, GridBagConstraints.VERTICAL);
sd.getRootPane().setDefaultButton(okButton);
sd.getRootPane().registerKeyboardAction(okListener, escKey, JComponent.WHEN_IN_FOCUSED_WINDOW);
sd.pack();
sd.setLocationRelativeTo(w);
sd.setVisible(true);
}
/**
* display the warning log
*/
void displayWarningLog(Window w) {
ToolDialog wd = new ToolDialog
(PolicyTool.getMessage("Warning"), tool, this, true);
// find the location of the PolicyTool gui
Point location = ((w == null) ?
getLocationOnScreen() : w.getLocationOnScreen());
//wd.setBounds(location.x + 50, location.y + 50, 500, 100);
wd.setLayout(new GridBagLayout());
JTextArea ta = new JTextArea();
ta.setEditable(false);
for (int i = 0; i < tool.warnings.size(); i++) {
ta.append(tool.warnings.elementAt(i));
ta.append(PolicyTool.getMessage("NEWLINE"));
}
addNewComponent(wd, ta, 0,
0, 0, 1, 1, 0.0, 0.0, GridBagConstraints.BOTH,
BOTTOM_PADDING);
ta.setFocusable(false);
JButton okButton = new JButton(PolicyTool.getMessage("OK"));
ActionListener okListener = new CancelButtonListener(wd);
okButton.addActionListener(okListener);
addNewComponent(wd, okButton, 1,
0, 1, 1, 1, 0.0, 0.0, GridBagConstraints.VERTICAL,
LR_PADDING);
wd.getRootPane().setDefaultButton(okButton);
wd.getRootPane().registerKeyboardAction(okListener, escKey, JComponent.WHEN_IN_FOCUSED_WINDOW);
wd.pack();
wd.setLocationRelativeTo(w);
wd.setVisible(true);
}
/**
* displays a dialog box describing the status of an event
*/
void displayStatusDialog(Window w, String status) {
ToolDialog sd = new ToolDialog
(PolicyTool.getMessage("Status"), tool, this, true);
// find the location of the PolicyTool gui
Point location = ((w == null) ?
getLocationOnScreen() : w.getLocationOnScreen());
//sd.setBounds(location.x + 50, location.y + 50, 500, 100);
sd.setLayout(new GridBagLayout());
JLabel label = new JLabel(status);
addNewComponent(sd, label, 0,
0, 0, 1, 1, 0.0, 0.0, GridBagConstraints.BOTH);
JButton okButton = new JButton(PolicyTool.getMessage("OK"));
ActionListener okListener = new StatusOKButtonListener(sd);
okButton.addActionListener(okListener);
addNewComponent(sd, okButton, 1,
0, 1, 1, 1, 0.0, 0.0, GridBagConstraints.VERTICAL);
sd.getRootPane().setDefaultButton(okButton);
sd.getRootPane().registerKeyboardAction(okListener, escKey, JComponent.WHEN_IN_FOCUSED_WINDOW);
sd.pack();
sd.setLocationRelativeTo(w);
sd.setVisible(true);
}
public static Point getTitlePoint(Window decoratedWindow) {
Point p = decoratedWindow.getLocationOnScreen();
Dimension d = decoratedWindow.getSize();
return new Point(p.x + (int)(d.getWidth()/2),
p.y + (int)(decoratedWindow.getInsets().top/2));
}
public static Point getTitlePoint(Window decoratedWindow) {
Point p = decoratedWindow.getLocationOnScreen();
Dimension d = decoratedWindow.getSize();
return new Point(p.x + (int)(d.getWidth()/2),
p.y + (int)(decoratedWindow.getInsets().top/2));
}
private static Point getCenterPoint(Window window) {
Point centerPoint = window.getLocationOnScreen();
centerPoint.translate(window.getWidth() / 2, window.getHeight() / 2);
return centerPoint;
}
public static Point getTitlePoint(Window decoratedWindow) {
Point p = decoratedWindow.getLocationOnScreen();
Dimension d = decoratedWindow.getSize();
return new Point(p.x + (int)(d.getWidth()/2),
p.y + (int)(decoratedWindow.getInsets().top/2));
}
public static Point getTitlePoint(Window decoratedWindow) {
Point p = decoratedWindow.getLocationOnScreen();
Dimension d = decoratedWindow.getSize();
return new Point(p.x + (int)(d.getWidth()/2),
p.y + (int)(decoratedWindow.getInsets().top/2));
}