JFrame居中显示问题

Pol 2007-11-17 10:38:03
我在网上找的JFrame居中代码,
setLocationRelativeTo(null);

Toolkit kit = Toolkit.getDefaultToolkit():
Dimension screenSize = kit.getScreenSize();
f.setLocation((screenSize.width-f.getWidth())/2, (screenSize.height-f.getHeight())/2);
或为:
this.setLocation(this.getToolkit().getScreenSize().width/2-this.getWidth()/2,this.getToolkit().getScreenSize().height/2-this.getHeight()/2);

但是我用的时,怎么是在右下方显示的了?我换了几个程序都是这样了.
这是怎么回事?
我的一个程序代码如下:
public class FunctionChoice extends JFrame {
JPanel contentPane;
ButtonGroup buttonGroup1 = new ButtonGroup();
..........
JTextPane jTextPane1 = new JTextPane();
public FunctionChoice() {

try {
setDefaultCloseOperation(EXIT_ON_CLOSE);
setLocationRelativeTo(null);//居中显示
jbInit();
} catch (Exception exception) {
exception.printStackTrace();
}
}
private void jbInit() throws Exception {
contentPane = (JPanel) getContentPane();
contentPane.setLayout(null);
......
...全文
687 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
Pol 2007-11-18
  • 打赏
  • 举报
回复
嗯 ,我想明白了.
Pol 2007-11-18
  • 打赏
  • 举报
回复
''一楼的高手您怎么知道要那样换一下了,我换了好象可以了,,
呵呵,您给我解决了两个问题了哟..
嗯,真是厉害哟...
damagegod 2007-11-18
  • 打赏
  • 举报
回复
import java.awt.Component;
import java.awt.Dimension;
import java.awt.Toolkit;

import javax.swing.JFrame;

public class TestFrame extends JFrame{

public TestFrame(Component component)
{
Dimension local_size=new Dimension(240,320);
setSize(local_size);
Dimension screensize=Toolkit.getDefaultToolkit().getScreenSize();
setLocation((screensize.width-local_size.width)/2,
(screensize.height-local_size.height)/2);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
getContentPane().add(component);
setVisible(true);
}

}
chenweionline 2007-11-17
  • 打赏
  • 举报
回复
你试一下构造函数里把jbinit()这个方法放到setLocationRelativeTo(null);的前面调用

62,616

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧