下面列出了java.awt.Dialog#setBounds ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
/**
* Positions the specified dialog at a position relative to its parent.
*
* @param dialog the dialog to be positioned.
* @param horizontalPercent the relative location.
* @param verticalPercent the relative location.
*/
public static void positionDialogRelativeToParent(final Dialog dialog,
final double horizontalPercent,
final double verticalPercent) {
final Container parent = dialog.getParent();
if (parent == null)
{
centerFrameOnScreen(dialog);
return;
}
final Dimension d = dialog.getSize();
final Dimension p = parent.getSize();
final int baseX = parent.getX();
final int baseY = parent.getY();
final int x = baseX + (int) (horizontalPercent * p.width);
final int y = baseY + (int) (verticalPercent * p.height);
// make sure the dialog fits completely on the screen...
final Rectangle s = parent.getGraphicsConfiguration().getBounds();
final Rectangle r = new Rectangle(x, y, d.width, d.height);
dialog.setBounds(r.intersection(s));
}
/**
* Positions the specified dialog at a position relative to its parent.
*
* @param dialog the dialog to be positioned.
* @param horizontalPercent the relative location.
* @param verticalPercent the relative location.
*/
public static void positionDialogRelativeToParent(Dialog dialog,
double horizontalPercent,
double verticalPercent) {
Dimension d = dialog.getSize();
Container parent = dialog.getParent();
Dimension p = parent.getSize();
int baseX = parent.getX() - d.width;
int baseY = parent.getY() - d.height;
int w = d.width + p.width;
int h = d.height + p.height;
int x = baseX + (int) (horizontalPercent * w);
int y = baseY + (int) (verticalPercent * h);
// make sure the dialog fits completely on the screen...
Rectangle s = getMaximumWindowBounds();
x = Math.min(x, (s.width - d.width));
x = Math.max(x, 0);
y = Math.min(y, (s.height - d.height));
y = Math.max(y, 0);
dialog.setBounds(x + s.x, y + s.y, d.width, d.height);
}
private void createAWTComponents() {
Frame f1 = new Frame("F1");
f1.setBounds(100, 300, 100, 100);
f1.setVisible(true);
try {
Thread.sleep(500);
} catch (Exception ex) {
}
f1.setExtendedState(Frame.ICONIFIED);
Frame g1 = new Frame("G1");
g1.setBounds(150, 350, 100, 100);
g1.setVisible(true);
final Dialog d1 = new Dialog((Frame) null, "D1", Dialog.ModalityType.APPLICATION_MODAL);
d1.setBounds(200, 400, 100, 100);
new Thread(new Runnable() {
public void run() {
d1.setVisible(true);
}
}).start();
}
/**
* Positions the specified dialog at a position relative to its parent.
*
* @param dialog the dialog to be positioned.
* @param horizontalPercent the relative location.
* @param verticalPercent the relative location.
*/
public static void positionDialogRelativeToParent(Dialog dialog,
double horizontalPercent,
double verticalPercent) {
Dimension d = dialog.getSize();
Container parent = dialog.getParent();
Dimension p = parent.getSize();
int baseX = parent.getX() - d.width;
int baseY = parent.getY() - d.height;
int w = d.width + p.width;
int h = d.height + p.height;
int x = baseX + (int) (horizontalPercent * w);
int y = baseY + (int) (verticalPercent * h);
// make sure the dialog fits completely on the screen...
Rectangle s = getMaximumWindowBounds();
x = Math.min(x, (s.width - d.width));
x = Math.max(x, 0);
y = Math.min(y, (s.height - d.height));
y = Math.max(y, 0);
dialog.setBounds(x + s.x, y + s.y, d.width, d.height);
}
public static void main(String[] args)
{
Frame frame1 = new Frame("frame 1");
frame1.setBounds(0, 0, 100, 100);
frame1.setVisible(true);
Util.waitForIdle(null);
Frame frame2 = new Frame("frame 2");
frame2.setBounds(150, 0, 100, 100);
frame2.setVisible(true);
Util.waitForIdle(null);
Frame frame3 = new Frame("frame 3");
final Dialog dialog = new Dialog(frame3, "dialog", true);
dialog.setBounds(300, 0, 100, 100);
EventQueue.invokeLater(new Runnable() {
public void run() {
dialog.setVisible(true);
}
});
try {
EventQueue.invokeAndWait(new Runnable() { public void run() {} });
Util.waitForIdle(null);
EventQueue.invokeAndWait(new Runnable() {
public void run() {
dialog.dispose();
}
});
}
catch (InterruptedException ie) {
throw new RuntimeException(ie);
}
catch (InvocationTargetException ite) {
throw new RuntimeException(ite);
}
frame1.dispose();
frame2.dispose();
frame3.dispose();
}
public static void main(String[] args) {
Robot robot = Util.createRobot();
Frame frame = new Frame("Frame");
frame.setBackground(Color.BLUE);
frame.setBounds(200, 50, 300, 300);
frame.setVisible(true);
Dialog dialog1 = new Dialog(frame, "Dialog 1", false);
dialog1.setBackground(Color.RED);
dialog1.setBounds(100, 100, 200, 200);
dialog1.setVisible(true);
Dialog dialog2 = new Dialog(frame, "Dialog 2", false);
dialog2.setBackground(Color.GREEN);
dialog2.setBounds(400, 100, 200, 200);
dialog2.setVisible(true);
Util.waitForIdle(robot);
Util.clickOnComp(dialog2, robot);
Util.waitForIdle(robot);
Point point = dialog1.getLocationOnScreen();
int x = point.x + (int)(dialog1.getWidth() * 0.9);
int y = point.y + (int)(dialog1.getHeight() * 0.9);
try {
if (!Util.testPixelColor(x, y, dialog1.getBackground(), 10, 100, robot)) {
throw new RuntimeException("Test FAILED: Dialog is behind the frame");
}
} finally {
frame.dispose();
dialog1.dispose();
dialog2.dispose();
}
}
public static void main(String[] args)
{
Frame frame1 = new Frame("frame 1");
frame1.setBounds(0, 0, 100, 100);
frame1.setVisible(true);
Util.waitForIdle(null);
Frame frame2 = new Frame("frame 2");
frame2.setBounds(150, 0, 100, 100);
frame2.setVisible(true);
Util.waitForIdle(null);
Frame frame3 = new Frame("frame 3");
final Dialog dialog = new Dialog(frame3, "dialog", true);
dialog.setBounds(300, 0, 100, 100);
EventQueue.invokeLater(new Runnable() {
public void run() {
dialog.setVisible(true);
}
});
try {
EventQueue.invokeAndWait(new Runnable() { public void run() {} });
Util.waitForIdle(null);
EventQueue.invokeAndWait(new Runnable() {
public void run() {
dialog.dispose();
}
});
}
catch (InterruptedException ie) {
throw new RuntimeException(ie);
}
catch (InvocationTargetException ite) {
throw new RuntimeException(ite);
}
frame1.dispose();
frame2.dispose();
frame3.dispose();
}
public static void main(String[] args) {
Robot robot = Util.createRobot();
Frame frame = new Frame("Frame");
frame.setBackground(Color.BLUE);
frame.setBounds(200, 50, 300, 300);
frame.setVisible(true);
Dialog dialog1 = new Dialog(frame, "Dialog 1", false);
dialog1.setBackground(Color.RED);
dialog1.setBounds(100, 100, 200, 200);
dialog1.setVisible(true);
Dialog dialog2 = new Dialog(frame, "Dialog 2", false);
dialog2.setBackground(Color.GREEN);
dialog2.setBounds(400, 100, 200, 200);
dialog2.setVisible(true);
Util.waitForIdle(robot);
Util.clickOnComp(dialog2, robot);
Util.waitForIdle(robot);
Point point = dialog1.getLocationOnScreen();
int x = point.x + (int)(dialog1.getWidth() * 0.9);
int y = point.y + (int)(dialog1.getHeight() * 0.9);
try {
if (!Util.testPixelColor(x, y, dialog1.getBackground(), 10, 100, robot)) {
throw new RuntimeException("Test FAILED: Dialog is behind the frame");
}
} finally {
frame.dispose();
dialog1.dispose();
dialog2.dispose();
}
}
public static void main(String[] args)
{
Frame frame1 = new Frame("frame 1");
frame1.setBounds(0, 0, 100, 100);
frame1.setVisible(true);
Util.waitForIdle(null);
Frame frame2 = new Frame("frame 2");
frame2.setBounds(150, 0, 100, 100);
frame2.setVisible(true);
Util.waitForIdle(null);
Frame frame3 = new Frame("frame 3");
final Dialog dialog = new Dialog(frame3, "dialog", true);
dialog.setBounds(300, 0, 100, 100);
EventQueue.invokeLater(new Runnable() {
public void run() {
dialog.setVisible(true);
}
});
try {
EventQueue.invokeAndWait(new Runnable() { public void run() {} });
Util.waitForIdle(null);
EventQueue.invokeAndWait(new Runnable() {
public void run() {
dialog.dispose();
}
});
}
catch (InterruptedException ie) {
throw new RuntimeException(ie);
}
catch (InvocationTargetException ite) {
throw new RuntimeException(ite);
}
frame1.dispose();
frame2.dispose();
frame3.dispose();
}
public static void main(String[] args)
{
Frame frame1 = new Frame("frame 1");
frame1.setBounds(0, 0, 100, 100);
frame1.setVisible(true);
Util.waitForIdle(null);
Frame frame2 = new Frame("frame 2");
frame2.setBounds(150, 0, 100, 100);
frame2.setVisible(true);
Util.waitForIdle(null);
Frame frame3 = new Frame("frame 3");
final Dialog dialog = new Dialog(frame3, "dialog", true);
dialog.setBounds(300, 0, 100, 100);
EventQueue.invokeLater(new Runnable() {
public void run() {
dialog.setVisible(true);
}
});
try {
EventQueue.invokeAndWait(new Runnable() { public void run() {} });
Util.waitForIdle(null);
EventQueue.invokeAndWait(new Runnable() {
public void run() {
dialog.dispose();
}
});
}
catch (InterruptedException ie) {
throw new RuntimeException(ie);
}
catch (InvocationTargetException ite) {
throw new RuntimeException(ite);
}
frame1.dispose();
frame2.dispose();
frame3.dispose();
}
public static void main(String[] args)
{
Frame frame1 = new Frame("frame 1");
frame1.setBounds(0, 0, 100, 100);
frame1.setVisible(true);
Util.waitForIdle(null);
Frame frame2 = new Frame("frame 2");
frame2.setBounds(150, 0, 100, 100);
frame2.setVisible(true);
Util.waitForIdle(null);
Frame frame3 = new Frame("frame 3");
final Dialog dialog = new Dialog(frame3, "dialog", true);
dialog.setBounds(300, 0, 100, 100);
EventQueue.invokeLater(new Runnable() {
public void run() {
dialog.setVisible(true);
}
});
try {
EventQueue.invokeAndWait(new Runnable() { public void run() {} });
Util.waitForIdle(null);
EventQueue.invokeAndWait(new Runnable() {
public void run() {
dialog.dispose();
}
});
}
catch (InterruptedException ie) {
throw new RuntimeException(ie);
}
catch (InvocationTargetException ite) {
throw new RuntimeException(ite);
}
frame1.dispose();
frame2.dispose();
frame3.dispose();
}
public static void main(String[] args)
{
Frame frame1 = new Frame("frame 1");
frame1.setBounds(0, 0, 100, 100);
frame1.setVisible(true);
Util.waitForIdle(null);
Frame frame2 = new Frame("frame 2");
frame2.setBounds(150, 0, 100, 100);
frame2.setVisible(true);
Util.waitForIdle(null);
Frame frame3 = new Frame("frame 3");
final Dialog dialog = new Dialog(frame3, "dialog", true);
dialog.setBounds(300, 0, 100, 100);
EventQueue.invokeLater(new Runnable() {
public void run() {
dialog.setVisible(true);
}
});
try {
EventQueue.invokeAndWait(new Runnable() { public void run() {} });
Util.waitForIdle(null);
EventQueue.invokeAndWait(new Runnable() {
public void run() {
dialog.dispose();
}
});
}
catch (InterruptedException ie) {
throw new RuntimeException(ie);
}
catch (InvocationTargetException ite) {
throw new RuntimeException(ite);
}
frame1.dispose();
frame2.dispose();
frame3.dispose();
}
public static void main(String[] args)
{
Frame frame1 = new Frame("frame 1");
frame1.setBounds(0, 0, 100, 100);
frame1.setVisible(true);
Util.waitForIdle(null);
Frame frame2 = new Frame("frame 2");
frame2.setBounds(150, 0, 100, 100);
frame2.setVisible(true);
Util.waitForIdle(null);
Frame frame3 = new Frame("frame 3");
final Dialog dialog = new Dialog(frame3, "dialog", true);
dialog.setBounds(300, 0, 100, 100);
EventQueue.invokeLater(new Runnable() {
public void run() {
dialog.setVisible(true);
}
});
try {
EventQueue.invokeAndWait(new Runnable() { public void run() {} });
Util.waitForIdle(null);
EventQueue.invokeAndWait(new Runnable() {
public void run() {
dialog.dispose();
}
});
}
catch (InterruptedException ie) {
throw new RuntimeException(ie);
}
catch (InvocationTargetException ite) {
throw new RuntimeException(ite);
}
frame1.dispose();
frame2.dispose();
frame3.dispose();
}
public static void main(String[] args)
{
Frame frame1 = new Frame("frame 1");
frame1.setBounds(0, 0, 100, 100);
frame1.setVisible(true);
Util.waitForIdle(null);
Frame frame2 = new Frame("frame 2");
frame2.setBounds(150, 0, 100, 100);
frame2.setVisible(true);
Util.waitForIdle(null);
Frame frame3 = new Frame("frame 3");
final Dialog dialog = new Dialog(frame3, "dialog", true);
dialog.setBounds(300, 0, 100, 100);
EventQueue.invokeLater(new Runnable() {
public void run() {
dialog.setVisible(true);
}
});
try {
EventQueue.invokeAndWait(new Runnable() { public void run() {} });
Util.waitForIdle(null);
EventQueue.invokeAndWait(new Runnable() {
public void run() {
dialog.dispose();
}
});
}
catch (InterruptedException ie) {
throw new RuntimeException(ie);
}
catch (InvocationTargetException ite) {
throw new RuntimeException(ite);
}
frame1.dispose();
frame2.dispose();
frame3.dispose();
}
public static void main(String[] args) {
Robot robot = Util.createRobot();
Frame frame = new Frame("Frame");
frame.setBackground(Color.BLUE);
frame.setBounds(200, 50, 300, 300);
frame.setVisible(true);
Dialog dialog1 = new Dialog(frame, "Dialog 1", false);
dialog1.setBackground(Color.RED);
dialog1.setBounds(100, 100, 200, 200);
dialog1.setVisible(true);
Dialog dialog2 = new Dialog(frame, "Dialog 2", false);
dialog2.setBackground(Color.GREEN);
dialog2.setBounds(400, 100, 200, 200);
dialog2.setVisible(true);
Util.waitForIdle(robot);
Util.clickOnComp(dialog2, robot);
Util.waitForIdle(robot);
Point point = dialog1.getLocationOnScreen();
int x = point.x + (int)(dialog1.getWidth() * 0.9);
int y = point.y + (int)(dialog1.getHeight() * 0.9);
try {
if (!Util.testPixelColor(x, y, dialog1.getBackground(), 10, 100, robot)) {
throw new RuntimeException("Test FAILED: Dialog is behind the frame");
}
} finally {
frame.dispose();
dialog1.dispose();
dialog2.dispose();
}
}
public static void main(String[] args) {
Robot robot = Util.createRobot();
Frame frame = new Frame("Frame");
frame.setBackground(Color.BLUE);
frame.setBounds(200, 50, 300, 300);
frame.setVisible(true);
Dialog dialog1 = new Dialog(frame, "Dialog 1", false);
dialog1.setBackground(Color.RED);
dialog1.setBounds(100, 100, 200, 200);
dialog1.setVisible(true);
Dialog dialog2 = new Dialog(frame, "Dialog 2", false);
dialog2.setBackground(Color.GREEN);
dialog2.setBounds(400, 100, 200, 200);
dialog2.setVisible(true);
Util.waitForIdle(robot);
Util.clickOnComp(dialog2, robot);
Util.waitForIdle(robot);
Point point = dialog1.getLocationOnScreen();
int x = point.x + (int)(dialog1.getWidth() * 0.9);
int y = point.y + (int)(dialog1.getHeight() * 0.9);
try {
if (!Util.testPixelColor(x, y, dialog1.getBackground(), 10, 100, robot)) {
throw new RuntimeException("Test FAILED: Dialog is behind the frame");
}
} finally {
frame.dispose();
dialog1.dispose();
dialog2.dispose();
}
}
public static void main(String[] args)
{
Frame frame1 = new Frame("frame 1");
frame1.setBounds(0, 0, 100, 100);
frame1.setVisible(true);
Util.waitForIdle(null);
Frame frame2 = new Frame("frame 2");
frame2.setBounds(150, 0, 100, 100);
frame2.setVisible(true);
Util.waitForIdle(null);
Frame frame3 = new Frame("frame 3");
final Dialog dialog = new Dialog(frame3, "dialog", true);
dialog.setBounds(300, 0, 100, 100);
EventQueue.invokeLater(new Runnable() {
public void run() {
dialog.setVisible(true);
}
});
try {
EventQueue.invokeAndWait(new Runnable() { public void run() {} });
Util.waitForIdle(null);
EventQueue.invokeAndWait(new Runnable() {
public void run() {
dialog.dispose();
}
});
}
catch (InterruptedException ie) {
throw new RuntimeException(ie);
}
catch (InvocationTargetException ite) {
throw new RuntimeException(ite);
}
frame1.dispose();
frame2.dispose();
frame3.dispose();
}
public static void main(String[] args) {
Robot robot = Util.createRobot();
Frame frame = new Frame("Frame");
frame.setBackground(Color.BLUE);
frame.setBounds(200, 50, 300, 300);
frame.setVisible(true);
Dialog dialog1 = new Dialog(frame, "Dialog 1", false);
dialog1.setBackground(Color.RED);
dialog1.setBounds(100, 100, 200, 200);
dialog1.setVisible(true);
Dialog dialog2 = new Dialog(frame, "Dialog 2", false);
dialog2.setBackground(Color.GREEN);
dialog2.setBounds(400, 100, 200, 200);
dialog2.setVisible(true);
Util.waitForIdle(robot);
Util.clickOnComp(dialog2, robot);
Util.waitForIdle(robot);
Point point = dialog1.getLocationOnScreen();
int x = point.x + (int)(dialog1.getWidth() * 0.9);
int y = point.y + (int)(dialog1.getHeight() * 0.9);
try {
if (!Util.testPixelColor(x, y, dialog1.getBackground(), 10, 100, robot)) {
throw new RuntimeException("Test FAILED: Dialog is behind the frame");
}
} finally {
frame.dispose();
dialog1.dispose();
dialog2.dispose();
}
}
@RandomlyFails // NB-Core-Build #1189
public void testLeakingNbPresenterDescriptor () throws InterruptedException, InvocationTargetException {
try {
Class.forName("java.lang.AutoCloseable");
} catch (ClassNotFoundException ex) {
// this test is known to fail due to JDK bugs 7070542 & 7072167
// which are unlikely to be fixed on JDK6. Thus, if AutoCloseable
// is not present, skip the test
return;
}
WizardDescriptor wizardDescriptor = new WizardDescriptor(getPanels(), null);
wizardDescriptor.setModal (false);
Dialog dialog = DialogDisplayer.getDefault ().createDialog (wizardDescriptor);
WeakReference<WizardDescriptor> w = new WeakReference<WizardDescriptor> (wizardDescriptor);
SwingUtilities.invokeAndWait (new EDTJob(dialog, true));
assertShowing("button is visible", true, dialog);
SwingUtilities.invokeAndWait (new EDTJob(dialog, false));
assertShowing("button is no longer visible", false, dialog);
boolean cancelled = wizardDescriptor.getValue() !=
WizardDescriptor.FINISH_OPTION;
Dialog d = new JDialog();
// workaround for JDK bug 6575402
JPanel p = new JPanel();
d.setLayout(new BorderLayout());
d.add(p, BorderLayout.CENTER);
JButton btn = new JButton("Button");
p.add(btn, BorderLayout.NORTH);
SwingUtilities.invokeAndWait (new EDTJob(d, true));
assertShowing("button is visible", true, btn);
dialog.setBounds(Utilities.findCenterBounds(dialog.getSize()));
SwingUtilities.invokeAndWait (new EDTJob(d, false));
assertShowing("button is no longer visible", false, btn);
assertNull ("BufferStrategy was disposed.", dialog.getBufferStrategy ());
RepaintManager rm = RepaintManager.currentManager(dialog);
rm.setDoubleBufferingEnabled(!rm.isDoubleBufferingEnabled());
rm.setDoubleBufferingEnabled(!rm.isDoubleBufferingEnabled());
dialog = null;
wizardDescriptor = null;
SwingUtilities.invokeAndWait (new Runnable() {
@Override
public void run() {
Frame f = new Frame();
f.setPreferredSize( new Dimension(100,100));
f.setVisible( true );
JDialog dlg = new JDialog(f);
dlg.setVisible(true);
}
});
assertGC ("Dialog disappears.", w);
}
private Dialog createDialog( final FileSearchPanel panel) {
openBtn = new JButton();
Mnemonics.setLocalizedText(openBtn, NbBundle.getMessage(FileSearchAction.class, "CTL_Open"));
openBtn.getAccessibleContext().setAccessibleDescription(openBtn.getText());
openBtn.setEnabled( false );
final Object[] buttons = new Object[] { openBtn, DialogDescriptor.CANCEL_OPTION };
String title = NbBundle.getMessage(FileSearchAction.class, "MSG_FileSearchDlgTitle");
DialogDescriptor dialogDescriptor = new DialogDescriptor(
panel,
title,
true,
buttons,
openBtn,
DialogDescriptor.DEFAULT_ALIGN,
HelpCtx.DEFAULT_HELP,
new DialogButtonListener(panel));
dialogDescriptor.setClosingOptions(buttons);
Dialog d = DialogDisplayer.getDefault().createDialog(dialogDescriptor);
d.getAccessibleContext().setAccessibleName(NbBundle.getMessage(FileSearchAction.class, "AN_FileSearchDialog"));
d.getAccessibleContext().setAccessibleDescription(NbBundle.getMessage(FileSearchAction.class, "AD_FileSearchDialog"));
// Set size
d.setPreferredSize( new Dimension( FileSearchOptions.getWidth(),
FileSearchOptions.getHeight() ) );
// Center the dialog after the size changed.
Rectangle r = Utilities.getUsableScreenBounds();
int maxW = (r.width * 9) / 10;
int maxH = (r.height * 9) / 10;
Dimension dim = d.getPreferredSize();
dim.width = Math.min(dim.width, maxW);
dim.height = Math.min(dim.height, maxH);
initialDimension = dim;
d.setBounds(Utilities.findCenterBounds(dim));
d.addWindowListener(new WindowAdapter() {
public @Override void windowClosed(WindowEvent e) {
cleanup(false);
}
});
return d;
}