我在网上找的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);
......
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);
......
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);
}}