当前位置: 技术问答>java相关
请问如何让Dialog弹出时能在屏幕的中央?谢谢
来源: 互联网 发布时间:2015-07-24
本文导语: 如题 | import java.awt.*; class testd extends Frame { Dialog dialog; testd(String str) { super(str); dialog=new Dialog(this,"mydialog"); dialog.setSize(100,100); int x=(int)((1024-dialog.getWidth())/2)...
如题
|
import java.awt.*;
class testd extends Frame
{
Dialog dialog;
testd(String str)
{
super(str);
dialog=new Dialog(this,"mydialog");
dialog.setSize(100,100);
int x=(int)((1024-dialog.getWidth())/2);
int y=(int)((768-dialog.getHeight())/2);
dialog.setLocation(x,y);
dialog.show();
}
public static void main(String [] args)
{
testd dobj=new testd("myFrame");
dobj.setSize(1024,768); //根据你的屏幕的分辨率定,如800*600
dobj.setVisible(true);
}
}
class testd extends Frame
{
Dialog dialog;
testd(String str)
{
super(str);
dialog=new Dialog(this,"mydialog");
dialog.setSize(100,100);
int x=(int)((1024-dialog.getWidth())/2);
int y=(int)((768-dialog.getHeight())/2);
dialog.setLocation(x,y);
dialog.show();
}
public static void main(String [] args)
{
testd dobj=new testd("myFrame");
dobj.setSize(1024,768); //根据你的屏幕的分辨率定,如800*600
dobj.setVisible(true);
}
}
|
在你的扩展Dailog的class的init()方法下任何地方调用均可。
CenterShowDialog csd = new CenterShowDialog(this);
import java.awt.*;
/**
* 描述:进入参数Dialog,使显示的图形界面显示居中
**/
public class CenterShowDialog {
public CenterShowDialog(Dialog dialog) {//目标显示为对话框
//Center the window
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
Dimension dialogSize = dialog.getSize();
if (dialogSize.height > screenSize.height) {
dialogSize.height = screenSize.height;
}
if (dialogSize.width > screenSize.width) {
dialogSize.width = screenSize.width;
}
dialog.setLocation((screenSize.width - dialogSize.width) / 2,
(screenSize.height - dialogSize.height) / 2);
}
}
//end
CenterShowDialog csd = new CenterShowDialog(this);
import java.awt.*;
/**
* 描述:进入参数Dialog,使显示的图形界面显示居中
**/
public class CenterShowDialog {
public CenterShowDialog(Dialog dialog) {//目标显示为对话框
//Center the window
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
Dimension dialogSize = dialog.getSize();
if (dialogSize.height > screenSize.height) {
dialogSize.height = screenSize.height;
}
if (dialogSize.width > screenSize.width) {
dialogSize.width = screenSize.width;
}
dialog.setLocation((screenSize.width - dialogSize.width) / 2,
(screenSize.height - dialogSize.height) / 2);
}
}
//end
|
FrmConvert_AboutBox dlg = new FrmConvert_AboutBox(this);
Dimension dlgSize = dlg.getPreferredSize();
Dimension frmSize = getSize();
Point loc = getLocation();
dlg.setLocation((frmSize.width - dlgSize.width) / 2 + loc.x, (frmSize.height - dlgSize.height) / 2 + loc.y);
dlg.setModal(true);
dlg.show();
Dimension dlgSize = dlg.getPreferredSize();
Dimension frmSize = getSize();
Point loc = getLocation();
dlg.setLocation((frmSize.width - dlgSize.width) / 2 + loc.x, (frmSize.height - dlgSize.height) / 2 + loc.y);
dlg.setModal(true);
dlg.show();
|
dig.setbounds((800-dig.getX()),(600-dig.getY()),dig.getX(),dig.getY())
---其中
dig.getX() 为dialog的宽