Java 奇怪的帧大小
因此,我设置了设置大小(500500)。。添加一些面板,面板Y的总和是500,就像JFrame一样,但是执行它显示Y的计数是525,我是否遗漏了什么Java 奇怪的帧大小,java,swing,jframe,size,Java,Swing,Jframe,Size,因此,我设置了设置大小(500500)。。添加一些面板,面板Y的总和是500,就像JFrame一样,但是执行它显示Y的计数是525,我是否遗漏了什么 JPanel panel = new JPanel(); panel.setLayout(null); getContentPane().add(panel); //-------------------- JPanel top_panel = new JPanel(); top_panel.setLayout(n
JPanel panel = new JPanel();
panel.setLayout(null);
getContentPane().add(panel);
//--------------------
JPanel top_panel = new JPanel();
top_panel.setLayout(null);
top_panel.setBackground(Color.blue);
top_panel.setBounds(0, 0, 500, 40);
panel.add(top_panel);
//------------------------------
JPanel middle_panel = new JPanel();
middle_panel.setLayout(null);
middle_panel.setBackground(Color.yellow);
middle_panel.setBounds(0, 40, 500, 385);
panel.add(middle_panel);
//-----------------------------
JPanel bottom_panel = new JPanel();
bottom_panel.setLayout(null);
bottom_panel.setBackground(Color.black);
bottom_panel.setBounds(0, 425, 500, 75);
panel.add(bottom_panel);
setSize(500,500);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setResizable(false);
setLocationRelativeTo(null);
40+385+75=500,但要显示所有面板,我必须
setSize(500,525);
那就合适了
这是一张图片:
框架大小是包括标题栏在内的边界外的浅蓝色矩形。您的面板显示在内部边界中,该边界的框架大小小于框架边界和框架标题栏。您是否看到底部标记的空间与标题栏的高度奇怪地相同 将面板/组件添加到框架后,在调用frame.setVisible(true)之前,调用frame.pack() 如果您使用布局管理器(如FlowLayout),并且在必要时调用setPreferredSize并让布局管理器进行布局,那么这也是更好的选择。通常,人们会调用setPreferredSize而不是setBound、setSize、setMininumSize、setMaximumSize
import javax.swing.*;
import java.awt.*;
public class FrameSize {
private JFrame frame;
FrameSize create() {
frame = createFrame();
frame.getContentPane().add(createContent());
return this;
}
private JFrame createFrame() {
JFrame frame = new JFrame(getClass().getName());
frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
return frame;
}
void show() {
// frame.setSize(500, 500);
frame.setResizable(false);
frame.setLocationRelativeTo(null);
frame.pack();
frame.setVisible(true);
}
private Component createContent() {
JPanel panel = new JPanel(null);
JPanel topPanel = new JPanel(null);
topPanel.setBackground(Color.blue);
topPanel.setBounds(0, 0, 500, 40);
panel.add(topPanel);
JPanel middlePanel = new JPanel(null);
middlePanel.setBackground(Color.yellow);
middlePanel.setBounds(0, 40, 500, 385);
panel.add(middlePanel);
JPanel bottomPanel = new JPanel(null);
bottomPanel.setBackground(Color.black);
bottomPanel.setBounds(0, 425, 500, 75);
panel.add(bottomPanel);
panel.setPreferredSize(new Dimension(500, topPanel.getBounds().height + middlePanel.getBounds().height + bottomPanel.getBounds().height));
return panel;
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
new FrameSize().create().show();
}
});
}
}
框架大小是包括标题栏在内的边界外的浅蓝色矩形。您的面板显示在内部边界中,该边界的框架大小小于框架边界和框架标题栏。您是否看到底部标记的空间与标题栏的高度奇怪地相同 将面板/组件添加到框架后,在调用frame.setVisible(true)之前,调用frame.pack() 如果您使用布局管理器(如FlowLayout),并且在必要时调用setPreferredSize并让布局管理器进行布局,那么这也是更好的选择。通常,人们会调用setPreferredSize而不是setBound、setSize、setMininumSize、setMaximumSize
import javax.swing.*;
import java.awt.*;
public class FrameSize {
private JFrame frame;
FrameSize create() {
frame = createFrame();
frame.getContentPane().add(createContent());
return this;
}
private JFrame createFrame() {
JFrame frame = new JFrame(getClass().getName());
frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
return frame;
}
void show() {
// frame.setSize(500, 500);
frame.setResizable(false);
frame.setLocationRelativeTo(null);
frame.pack();
frame.setVisible(true);
}
private Component createContent() {
JPanel panel = new JPanel(null);
JPanel topPanel = new JPanel(null);
topPanel.setBackground(Color.blue);
topPanel.setBounds(0, 0, 500, 40);
panel.add(topPanel);
JPanel middlePanel = new JPanel(null);
middlePanel.setBackground(Color.yellow);
middlePanel.setBounds(0, 40, 500, 385);
panel.add(middlePanel);
JPanel bottomPanel = new JPanel(null);
bottomPanel.setBackground(Color.black);
bottomPanel.setBounds(0, 425, 500, 75);
panel.add(bottomPanel);
panel.setPreferredSize(new Dimension(500, topPanel.getBounds().height + middlePanel.getBounds().height + bottomPanel.getBounds().height));
return panel;
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
new FrameSize().create().show();
}
});
}
}
您不应该设置大小或调用
setSize(…)
或setBounds(…)
,因为这会使您将来遇到类似的问题,或者在不同平台上显示GUI时遇到更糟糕的问题。相反,让组件的首选尺寸和布局管理器为您完成这项工作。如果绝对必须设置组件的大小,请重写getPreferredSize()
,并返回一个为您计算的维度。是的,根据javajon,在显示JFrame之前,应该在JFrame上调用pack()
有关空布局的更多讨论,请阅读本网站上最好的Swing专家之一MadProgrammer在其回答中所说的。您不应该设置大小或调用
setSize(…)
或setBounds(…)
,因为这会让您将来遇到类似问题,当您尝试在不同的平台上显示GUI时,可能会出现更严重的问题。相反,让组件的首选尺寸和布局管理器为您完成这项工作。如果绝对必须设置组件的大小,请重写getPreferredSize()
,并返回一个为您计算的维度。是的,根据javajon,在显示JFrame之前,应该在JFrame上调用pack()
有关空布局的更多讨论,请阅读本网站最好的Swing专家之一MadProgrammer在其回答中所说的内容。我称之为pack(),但现在它看起来像是一个奇怪的框架,有100x25。。。你确定标题栏上的边框大小吗?是的。frame.setSize设置整个框架大小,而不是内部contentpane大小。请参阅上面修改的代码。布局管理器和setPreferredSize是一种方法。同样的建议也适用于JavaFX。我调用pack(),但现在它看起来是一个奇怪的框架,有100x25。。。你确定标题栏上的边框大小吗?是的。frame.setSize设置整个框架大小,而不是内部contentpane大小。请参阅上面修改的代码。布局管理器和setPreferredSize是一种方法。同样的建议也适用于JavaFX。