弹出的窗口跑到左上角了。
我直接运行你的代码,弹出的对话框的确是自动居中的,而且本来弹出对话框就是默认居中的。你也可以试试强制指定位置
btnNewButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
JOptionPane jp=new JOptionPane();
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize(); //获取当前屏幕大小
Dimension frameSize = jp.getPreferredSize();//获取当前窗口大小
jp.setLocation((screenSize.width - frameSize.width)/2, (screenSize.height - frameSize.height));
jp.showConfirmDialog(null, "33");
}
});
弹出的窗口跑到左上角了。
我直接运行你的代码,弹出的对话框的确是自动居中的,而且本来弹出对话框就是默认居中的。你也可以试试强制指定位置
btnNewButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
JOptionPane jp=new JOptionPane();
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize(); //获取当前屏幕大小
Dimension frameSize = jp.getPreferredSize();//获取当前窗口大小
jp.setLocation((screenSize.width - frameSize.width)/2, (screenSize.height - frameSize.height));
jp.showConfirmDialog(null, "33");
}
});加上这句全屏后就出现问题了:
frame.getGraphicsConfiguration().getDevice().setFullScreenWindow(frame);
public class Test extends JFrame { private JPanel contentPane; /**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
Test frame = new Test();
frame.setVisible(true);
frame.getGraphicsConfiguration().getDevice().setFullScreenWindow(frame);
} catch (Exception e) {
e.printStackTrace();
}
}
});
} /**
* Create the frame.
*/
public Test() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 450, 300);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
contentPane.setLayout(new BorderLayout(0, 0));
setContentPane(contentPane);
JButton btnNewButton = new JButton("New button");
btnNewButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
JOptionPane.showConfirmDialog(null, "33");
}
});
contentPane.add(btnNewButton, BorderLayout.NORTH);
}
}
是居中的