当前位置: 技术问答>java相关
做了一个ABOUT对话框,如何让它在打开时只显示在窗口的正中,而不管窗口的位置在哪里。
来源: 互联网 发布时间:2015-02-28
本文导语: | import java.awt.*; import java.awt.event.*; import javax.swing.*; public class splash extends JFrame { Toolkit kit = Toolkit.getDefaultToolkit(); JWindow jw = new JWindow(); JLabel la = new JLabel(new ImageIcon("a.jpg")); public sp...
|
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class splash extends JFrame
{
Toolkit kit = Toolkit.getDefaultToolkit();
JWindow jw = new JWindow();
JLabel la = new JLabel(new ImageIcon("a.jpg"));
public splash()
{
la.setBorder(BorderFactory.createRaisedBevelBorder());
jw.getContentPane().add(la,BorderLayout.CENTER);
centerWindow();
jw.show();
jw.addMouseListener(new MouseAdapter()
{
public void mousePressed(MouseEvent e)
{
jw.dispose();
System.exit(0);
}
});
}
private void centerWindow()
{
Dimension sc = kit.getScreenSize();
Dimension ls = la.getPreferredSize();
int law = ls.width;
int lah = ls.height;
jw.setLocation(sc.width/2 - (law/2),sc.height/2 - (lah/2));
jw.pack();
}
public static void main(String args[])
{
JFrame fr = new splash();
}
}
import java.awt.event.*;
import javax.swing.*;
public class splash extends JFrame
{
Toolkit kit = Toolkit.getDefaultToolkit();
JWindow jw = new JWindow();
JLabel la = new JLabel(new ImageIcon("a.jpg"));
public splash()
{
la.setBorder(BorderFactory.createRaisedBevelBorder());
jw.getContentPane().add(la,BorderLayout.CENTER);
centerWindow();
jw.show();
jw.addMouseListener(new MouseAdapter()
{
public void mousePressed(MouseEvent e)
{
jw.dispose();
System.exit(0);
}
});
}
private void centerWindow()
{
Dimension sc = kit.getScreenSize();
Dimension ls = la.getPreferredSize();
int law = ls.width;
int lah = ls.height;
jw.setLocation(sc.width/2 - (law/2),sc.height/2 - (lah/2));
jw.pack();
}
public static void main(String args[])
{
JFrame fr = new splash();
}
}
|
Center a Frame/Dialog
// centers the dialog within the screen [1.1]
// (put that in the Frame/Dialog class)
public void centerScreen() {
Dimension dim = getToolkit().getScreenSize();
Rectangle abounds = getBounds();
setLocation((dim.width - abounds.width) / 2,
(dim.height - abounds.height) / 2);
super.setVsible(true);
requestFocus();
}
// centers the dialog within the parent container [1.1]
// (put that in the Dialog class)
public void centerParent () {
int x;
int y;
// Find out our parent
Container myParent = getParent();
Point topLeft = myParent.getLocationOnScreen();
Dimension parentSize = myParent.getSize();
Dimension mySize = getSize();
if (parentSize.width > mySize.width)
x = ((parentSize.width - mySize.width)/2) + topLeft.x;
else
x = topLeft.x;
if (parentSize.height > mySize.height)
y = ((parentSize.height - mySize.height)/2) + topLeft.y;
else
y = topLeft.y;
setLocation (x, y);
super.setVsible(true);
requestFocus();
}
// centers the dialog within the screen [1.1]
// (put that in the Frame/Dialog class)
public void centerScreen() {
Dimension dim = getToolkit().getScreenSize();
Rectangle abounds = getBounds();
setLocation((dim.width - abounds.width) / 2,
(dim.height - abounds.height) / 2);
super.setVsible(true);
requestFocus();
}
// centers the dialog within the parent container [1.1]
// (put that in the Dialog class)
public void centerParent () {
int x;
int y;
// Find out our parent
Container myParent = getParent();
Point topLeft = myParent.getLocationOnScreen();
Dimension parentSize = myParent.getSize();
Dimension mySize = getSize();
if (parentSize.width > mySize.width)
x = ((parentSize.width - mySize.width)/2) + topLeft.x;
else
x = topLeft.x;
if (parentSize.height > mySize.height)
y = ((parentSize.height - mySize.height)/2) + topLeft.y;
else
y = topLeft.y;
setLocation (x, y);
super.setVsible(true);
requestFocus();
}
您可能感兴趣的文章:
本站(WWW.)旨在分享和传播互联网科技相关的资讯和技术,将尽最大努力为读者提供更好的信息聚合和浏览方式。
本站(WWW.)站内文章除注明原创外,均为转载、整理或搜集自网络。欢迎任何形式的转载,转载请注明出处。
本站(WWW.)站内文章除注明原创外,均为转载、整理或搜集自网络。欢迎任何形式的转载,转载请注明出处。