gpt4 book ai didi

java - 与 GridbagLayout 一起使用时,setSize()、setMaximumSize()、setMinimumSize() 之间有什么区别

转载 作者:塔克拉玛干 更新时间:2023-11-02 20:09:02 27 4
gpt4 key购买 nike

与 GridbagLayout 一起使用时,setSize()、setMaximumSize()、setMinimumSize() 之间有什么区别。当我使用 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(新维度((int)Toolkit.getDefaultToolkit().getScreenSize().getWidth()/2,
(int)Toolkit.getDefaultToolkit().getScreenSize().getHeight()));
basicPanel.setBorder(BorderFactory.createEtchedBorder());
返回基本面板;

setSize() 的结果: enter image description here

setMaximumSize() 的结果: enter image description here

问题:1.如何很好地显示它们,因为我无法使用 setSize() 或 setMaximumSize() 实现它

更多信息:我可以在 JFrame 上使用 pack() 来使其在上面正确,但在我的实际代码中,这个面板出现在其他面板中,到目前为止我找不到使用 pack 的地方。如果有任何与 JPanel 的 pack 等效的东西,我会很乐意使用它。

最佳答案

使用 setSize() 您可以定义网格的大小。使用 setMaximumSize(),您可以告诉网格,如果用户放大他的窗口,网格不会增加超过给定的大小。使用setMinimumSize(),相当于缩小窗口。

关于java - 与 GridbagLayout 一起使用时,setSize()、setMaximumSize()、setMinimumSize() 之间有什么区别,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16740445/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com