就像标题所说的那样,我希望将一个800x600的画布放在1920x1080的屏幕上 我希望画布在JFrame中居中
public class Window extends JFrame {
private static final long serialVersionUID = 7045103465799258651L;
Dimension d;
public Window(int w, int h, String title,Launcher launch){
setTitle(title);
d = new Dimension(w,h);
setMinimumSize(d);
setMaximumSize(d);
setPreferredSize(d);
setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
pack();
setFocusable(true);
setLocationRelativeTo(null);
add(launch);
launch.start();
}
}
答案 0 :(得分:3)
一种方式:
例如:
import java.awt.Color;
import java.awt.Dimension;
import java.awt.GridBagLayout;
import javax.swing.*;
public class TestCenterLayout extends JPanel{
private static final long serialVersionUID = 1L;
private static final int PREF_W = 800;
private static final int PREF_H = 600;
private static final Color BACKGROUND = Color.pink;
public TestCenterLayout() {
setBorder(BorderFactory.createTitledBorder("800 x 600 Panel"));
setBackground(BACKGROUND);
}
@Override
public Dimension getPreferredSize() {
if (isPreferredSizeSet()) {
return super.getPreferredSize();
}
return new Dimension(PREF_W, PREF_H);
}
private static void createAndShowGui() {
JFrame frame = new JFrame("Test Center Layout");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().setLayout(new GridBagLayout());
frame.getContentPane().add(new TestCenterLayout());
frame.pack();
frame.setExtendedState(JFrame.MAXIMIZED_BOTH);
frame.setVisible(true);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGui();
}
});
}
}