我正在与 JFrame 内的 JPanel 创建一个接口,其中 JPanel 的宽度设置为等于 JFrame 的宽度。与我的预期相反,当我运行程序查看显示时,JPanel 显示为一个非常小的框,小于所需大小,其宽度应等于 JFrame 的宽度。但是,当我将 JPanel 的宽度减小大约 6 时,它可以很好地显示它在 JFrame 中的确切大小。在我看来,JFrame 可能有某种边距或填充,但我真的不知道。请帮助并说明为什么我不能使 JPanel 宽度与父 JFrame 的宽度相同,以及如果可能的话如何做到这一点。我真的希望它们具有相同的宽度,因为这就是我的设计设置方式。我的代码如下:
`import java.awt.*; //importing awt package
import javax.swing.*; //importing swing package
//class definition
public class PMSysClient {
//declaration of variables
private JFrame lgFrame; //login frame
private JFrame fdFrame; //frame for front desk
private JFrame docFrame; //frame for doctor
private JPanel logHeadPan; //header panel in login
private JPanel logAreaPan; //login area panel
//constructor definition
public PMSysClient() {
initialise();
}//end of constructor
//method to initialise class
public void initialise() {
lgFrame = new JFrame();
Container cont = lgFrame.getContentPane();
lgFrame.setSize(863, 569);
lgFrame.setLocationRelativeTo(null);
lgFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
cont.setBackground(new Color(231, 228, 204));
cont.setLayout(new GridBagLayout());
logHeadPan = new JPanel();
logHeadPan.setBackground(new Color(204, 153, 255));
logHeadPan.setPreferredSize(new Dimension(863, 210));
GridBagConstraints c1 = new GridBagConstraints();
c1.weighty = 0.1;
c1.weightx=0.1;
c1.gridx = 0;
c1.gridy = 0;
c1.anchor = GridBagConstraints.FIRST_LINE_START;
lgFrame.add(logHeadPan, c1);
lgFrame.setVisible(true);
}//end of initialise
//main method definition
public static void main(String[] args) {
try {
UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
} catch (Exception e) {
}
new PMSysClient();
}//end of main method
}`