简体   繁体   中英

What is the difference between setSize(), setMaximumSize(),setMinimumSize() when used with GridbagLayout

What is the difference between setSize(), setMaximumSize(),setMinimumSize() when used with GridbagLayout. When I am using setMaximumSize(), it is reducing the height of the panel.If needed I can put SSCCE

Please find the 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 */

I have removed all the unused field. Please also find the image when use setSize() and setMaximumSize() with the panel at this line basicPanel.setSize(new Dimension((int)Toolkit.getDefaultToolkit().getScreenSize().getWidth()/2, (int)Toolkit.getDefaultToolkit().getScreenSize().getHeight())); basicPanel.setBorder(BorderFactory.createEtchedBorder()); return basicPanel; basicPanel.setSize(new Dimension((int)Toolkit.getDefaultToolkit().getScreenSize().getWidth()/2, (int)Toolkit.getDefaultToolkit().getScreenSize().getHeight())); basicPanel.setBorder(BorderFactory.createEtchedBorder()); return basicPanel;

Result with setSize(): 在此输入图像描述

Result with setMaximumSize(): 在此输入图像描述

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

More Information: I can use pack() on JFrame to make it correct in this above but in my actual code this panel is appearing inside other panel and so far I could not find the place to use pack. If there is anything equivalent to pack for JPanel I would be happy to use that.

With setSize() you define the size of your grid. With setMaximumSize() you tell the grid that if the user enlarges his window, the grid won't increase more than the given size. With setMinimumSize() , it is the equivalent for shrinking the window.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM