JFrame에서 폭이 다른 두 개의 패널을 만들려고합니다. 오른쪽 패널의 왼쪽 패널 두께가 두 배가되기를 바랍니다. 내 leftpanel에 gridwidth = 1, rightboardel에 gridwidth = 2를 사용하여 GridBagLayout을 사용하려고합니다. 하지만 너비가 달라지는 대신 동일한 너비의 패널이 두 개 있습니다. 무엇을 바꾸어야합니까? 어떤 도움을 주시면 감사하겠습니다.JFrame에서 GridBagLayout을 사용하여 여러 너비의 JPanel
import javax.swing.*;
import java.awt.*;
public class AppFrame2 extends JFrame {
GridBagLayout gbl;
GridBagConstraints gbc;
JPanel leftpanel;
JPanel rightpanel;
public AppFrame2() {
this.setSize(500, 500);
this.setResizable(false);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setVisible(true);
this.getContentPane().setBackground(Color.BLACK);
// Settning Frame Layout
gbl = new GridBagLayout();
gbc = new GridBagConstraints();
this.setLayout(gbl);
gbc.fill = GridBagConstraints.BOTH;
gbc.weightx = 1.0;
gbc.weighty = 1.0;
// Add Panels
leftpanel = new JPanel();
leftpanel.setBackground(Color.green);
this.addPanels(0, 0, 1, 1, leftpanel); // row, col, height, width component
rightpanel = new JPanel();
rightpanel.setBackground(Color.red);
this.addPanels(0, 1, 1, 2, rightpanel);
}
private void addPanels(int row, int col, int height, int width,
Component com) {
gbc.gridx = col;
gbc.gridy = row;
gbc.gridheight = height;
gbc.gridwidth = width;
gbl.setConstraints(com, gbc);
this.getContentPane().add(com);
}
public static void main(String[] args) {
new AppFrame2();
}
}
내부 패딩 사용을 고려 했습니까? – TNT
크기가 조정되는 이유는 GridBagLayout이 사물을 같은 크기로 참조하기 때문입니다. 더 좋은 예를 들어 이것을 참조하십시오 http://stackoverflow.com/questions/4112886/components-in-gridlayout-wit-jpanel-fills-the-grid-inclurectly – Dacotah
gbc.insets (2, 2, 2, 2)를 사용했습니다. , 그러나 나는 그것을 제거했다. 나는 다른 패딩에 대해 모른다. 예를 들어 주시겠습니까? –