当使用GridbagLayout时,setSize()、setMaximumSize()和setMinimumSize()有什么区别?当我使用setMaximumSize()时,它会减小面板的高度。以下是需要翻译的内容:
我已经删除了所有未使用的字段。请在此行中查找使用setSize()和setMaximumSize()时的面板图像
使用setMaximumSize()的结果:
问题:
如果需要,我可以提供SSCCE
请查看SSCCE:public class EditUserPanel extends JPanel {
private static final long serialVersionUID = 1L;
private JLabel st_REQ_FIELDS = null;
private JCheckBox ck_ISMANAGER = null;
private JCheckBox ck_ISBPM = null;
private JComboBox cb_LANGUAGE = null;
private JList lb_TEAMS = null;
private JList lb_COUNTRY = null;
private JList lb_BRANDPM = null;
private JTextField ef_NAME = null;
private JTextField ef_USERID = null;
private JScrollPane scr_TEAMS = null;
private JScrollPane scr_COUNTRY = null;
private JScrollPane scr_BRANDPM = null;
private JPanel teamButtonPanel = null;
private JPanel countryButtonPanel = null;
private JPanel brandButtonPanel = null;
private JButton pb_ADD_TEAM = null;
private JButton pb_REMOVE_TEAM = null;
private JButton pb_ADD_COUNTRY = null;
private JButton pb_REMOVE_COUNTRY = null;
private JButton pb_ADD_BRAND = null;
private JButton pb_REMOVE_BRAND = null;
private static final Insets WEST_INSETS = new Insets(5, 0, 5, 5);
private static final Insets EAST_INSETS = new Insets(5, 5, 5, 0);
public EditUserPanel() {
initialize();
}
public void initialize() {
ck_ISMANAGER = new JCheckBox("Is a Manager");
ck_ISBPM = new JCheckBox("Brand Program Manager");
cb_LANGUAGE = new JComboBox();
lb_TEAMS = new JList();
lb_COUNTRY = new JList();
lb_BRANDPM = new JList();
ef_NAME = new JTextField();
ef_USERID = new JTextField();
scr_TEAMS = new JScrollPane(lb_TEAMS);
scr_COUNTRY = new JScrollPane(lb_COUNTRY);
scr_BRANDPM = new JScrollPane(lb_BRANDPM);
JPanel contentPane = new JPanel();
pb_ADD_TEAM = new JButton("Add Team");
pb_REMOVE_TEAM = new JButton("Remove Team");
pb_ADD_COUNTRY = new JButton("Add Country");
pb_REMOVE_COUNTRY = new JButton("Remove Country");
pb_ADD_BRAND = new JButton("Add Brand");
pb_REMOVE_BRAND = new JButton("Remove Brand");
st_REQ_FIELDS = new JLabel("* = These fields are required");
st_REQ_FIELDS.setForeground(Color.red);
setLayout(new BorderLayout());
contentPane.setLayout(new BoxLayout(contentPane, BoxLayout.LINE_AXIS));
contentPane.add(addBasicElements());
add(st_REQ_FIELDS,BorderLayout.PAGE_START);
add(contentPane, BorderLayout.CENTER);
}
private Component addBasicElements() {
// TODO Auto-generated method stub
JPanel basicPanel = new JPanel();
basicPanel.setLayout(new GridBagLayout());
GridBagConstraints gbc= new GridBagConstraints();
gbc=createGbc(0, 0); basicPanel.add(addFormPanel(new JPanel(), new JLabel("* Name : "),ef_NAME),gbc);
gbc=createGbc(1, 0); basicPanel.add(addFormPanel(new JPanel(), new JLabel("* UserID : "), ef_USERID),gbc);
gbc=createGbc(0, 1); basicPanel.add(addFormPanel(new JPanel(), new JLabel("* Language : "), cb_LANGUAGE),gbc);
gbc=createGbc(1, 1); basicPanel.add(addFormPanel(new JPanel(), new JLabel(" "),ck_ISMANAGER),gbc);
gbc=createGbc(0, 2); basicPanel.add(addFormPanel(new JPanel(),new JLabel("Brand Manager : "),(JPanel)addBrandManagerPanel()),gbc);
gbc=createGbc(1, 2); basicPanel.add(addFormPanel(new JPanel(),new JLabel("* Country : "),(JPanel)addCountryPanel()),gbc);
gbc=createGbc(0, 3); basicPanel.add(addFormPanel(new JPanel(), new JLabel("* Team : "), (JPanel)addTeamPanel()),gbc);
basicPanel.setSize(new Dimension((int)Toolkit.getDefaultToolkit().getScreenSize().getWidth()/2,
(int)Toolkit.getDefaultToolkit().getScreenSize().getHeight()));
basicPanel.setBorder(BorderFactory.createEtchedBorder());
return basicPanel;
}
private GridBagConstraints createGbc(int x, int y) {
// TODO Auto-generated method stub
GridBagConstraints gbc = new GridBagConstraints();
gbc.gridx = x; gbc.gridy = y;
gbc.gridwidth = 1; gbc.gridheight = 1;
gbc.anchor = (x == 0) ? GridBagConstraints.WEST : GridBagConstraints.EAST;
gbc.fill = GridBagConstraints.HORIZONTAL;
gbc.insets = (x == 0) ? WEST_INSETS : EAST_INSETS;
gbc.weightx = (x == 0) ? 0.1 : 1.0;
gbc.weighty = 1.0;
return gbc;
}
private Component addFormPanel(JComponent jPanel, JComponent label,
JComponent textField) {
// TODO Auto-generated method stub
jPanel.setLayout(new BoxLayout(jPanel, BoxLayout.X_AXIS));
jPanel.add(label);
jPanel.add(textField);
// jPanel.setBorder(BorderFactory.createEtchedBorder());
return jPanel;
}
private Component addTeamPanel() {
// TODO Auto-generated method stub
JPanel teamPanel = new JPanel();
teamPanel.setLayout(new BoxLayout(teamPanel, BoxLayout.Y_AXIS));
teamPanel.add(scr_TEAMS);
teamPanel.add(addTeamButtonPanel());
return teamPanel;
}
private Component addTeamButtonPanel() {
// TODO Auto-generated method stub
teamButtonPanel = new JPanel();
teamButtonPanel.setLayout(new BoxLayout(teamButtonPanel,BoxLayout.X_AXIS));
teamButtonPanel.add(pb_ADD_TEAM);
teamButtonPanel.add(pb_REMOVE_TEAM);
return teamButtonPanel;
}
private Component addCountryPanel() {
// TODO Auto-generated method stub
JPanel countryPanel = new JPanel();
countryPanel.setLayout(new BoxLayout(countryPanel, BoxLayout.Y_AXIS));
countryPanel.add(scr_COUNTRY);
countryPanel.add(addCountryButtonPanel());
return countryPanel;
}
private Component addCountryButtonPanel() {
// TODO Auto-generated method stub
countryButtonPanel = new JPanel();
countryButtonPanel.setLayout(new BoxLayout(countryButtonPanel,BoxLayout.X_AXIS));
countryButtonPanel.add(pb_ADD_COUNTRY);
countryButtonPanel.add(pb_REMOVE_COUNTRY);
return countryButtonPanel;
}
private Component addBrandManagerPanel() {
// TODO Auto-generated method stub
JPanel brandmanagerPanel = new JPanel();
brandmanagerPanel.setLayout(new BoxLayout(brandmanagerPanel, BoxLayout.Y_AXIS));
brandmanagerPanel.add(scr_BRANDPM);
brandmanagerPanel.add(addBrandButtonPanel());
return brandmanagerPanel;
}
private Component addBrandButtonPanel() {
// TODO Auto-generated method stub
brandButtonPanel = new JPanel();
brandButtonPanel.setLayout(new BoxLayout(brandButtonPanel,BoxLayout.X_AXIS));
brandButtonPanel.add(ck_ISBPM);
brandButtonPanel.add(pb_ADD_BRAND);
brandButtonPanel.add(pb_REMOVE_BRAND);
return brandButtonPanel;
}
public static void main(String[] args){
EditUserPanel panel = new EditUserPanel();
JFrame frame = new JFrame("SSCCE for stack overflow");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add(panel, BorderLayout.CENTER);
frame.setSize(new Dimension((int)Toolkit.getDefaultToolkit().getScreenSize().getWidth(),
(int)Toolkit.getDefaultToolkit().getScreenSize().getHeight()));
frame.setVisible(true);
}
} /* EditUserPanel */
我已经删除了所有未使用的字段。请在此行中查找使用setSize()和setMaximumSize()时的面板图像
basicPanel.setSize(new Dimension((int)Toolkit.getDefaultToolkit().getScreenSize().getWidth()/2,
(int)Toolkit.getDefaultToolkit().getScreenSize().getHeight()));
basicPanel.setBorder(BorderFactory.createEtchedBorder());
return basicPanel;
使用setSize()的结果:
![enter image description here](https://istack.dev59.com/vFYrf.webp)
![enter image description here](https://istack.dev59.com/y79r1.webp)
1.如何使它们显示得好看,因为我无法使用setSize()或setMaximumSize()实现它
更多信息:
我可以在JFrame上使用pack()进行正确设置,但在我的实际代码中,该面板出现在其他面板内,到目前为止,我找不到使用pack的位置。如果有任何类似于JPanel的pack的东西,我会很高兴使用它。