3

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 的包装,我会很乐意使用它。

4

1 回答 1

1

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

于 2013-05-24T18:03:10.397 回答