【问题标题】:What is the difference between setSize(), setMaximumSize(),setMinimumSize() when used with GridbagLayoutsetSize(), setMaximumSize(),setMinimumSize() 与 GridbagLayout 一起使用时有什么区别
【发布时间】:2013-05-20 09:04:37
【问题描述】:

setSize(), setMaximumSize(),setMinimumSize() 与 GridbagLayout 一起使用时有什么区别。当我使用 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() 的结果:

setMaximumSize() 的结果:

问题: 1.How to display them nicely because I am not able to achieve it either with setSize() or setMaximumSize()

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

【问题讨论】:

  • SSCE,是的,它有助于为您提供更好的答案
  • @mKorbel SSCE 已添加。

标签: java swing gridbaglayout


【解决方案1】:

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

【讨论】:

  • 关于 GridBagLayout,我不认为这是正确的。给定网格的大小由给定列中所有组件的首选大小的最大宽度和给定行中所有组件的首选大小的最大高度决定。此外,GBL 会忽略 getMaximumSize()。
猜你喜欢
  • 2013-09-05
  • 1970-01-01
  • 2012-04-05
  • 2013-08-08
  • 2016-08-26
  • 2018-05-09
  • 1970-01-01
  • 1970-01-01
  • 2022-11-04
相关资源
最近更新 更多