java swing gridlayout看起来不像我期望的那样

rsl1atfo  于 2021-06-29  发布在  Java
关注(0)|答案(1)|浏览(303)

我有一个gui,它是一个网格布局,有6行4列,顺序应该是:label-radiobutton-radiobutton-combobox
这对于第一行是成功的。然而之后的每一行都是一团糟。请参考图形用户界面的图像。
请忽略注解代码,因为我将在稍后完成gui布局后设置可见性。谢谢。

JFrame frame = new JFrame("Zoho DNC List Updater");

        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setSize(height, width);

        // creating menu bar
        JMenuBar mb = new JMenuBar();
        // mb.setLayout(new FlowLayout());
        JMenu m1 = new JMenu("File");
        JMenu m2 = new JMenu("Help");
        mb.add(m1);
        mb.add(m2);

        JMenuItem contact = new JMenuItem("Contact Us");
        JMenuItem help = new JMenuItem("Help");
        JMenuItem openFile = new JMenuItem("Open File");
        JMenuItem saveFile = new JMenuItem("Save File");
        JMenuItem newWindow = new JMenuItem("New window");

        m2.add(contact);
        m2.add(help);
        m1.add(openFile);
        m1.add(saveFile);
        m1.add(newWindow);

        JPanel panel = new JPanel(); // the panel is not visible in output
        JLabel label = new JLabel("Update Records");
        // JTextField tf = new JTextField(10); // accepts upto 10 characters
        JButton send = new JButton("Update");
        JButton reset = new JButton("Quit");
        panel.add(label); // Components Added using Flow Layout
        // panel.add(tf);
        panel.add(send);
        panel.add(reset);

        JPanel panel2 = new JPanel(new GridLayout(6, 4));

        JRadioButton radio1 = new JRadioButton("Yes");
        JRadioButton radio2 = new JRadioButton("No");
        JLabel custom = new JLabel("Customize Call Report?");
        ButtonGroup btnGrp = new ButtonGroup();
        radio1.setBounds(120, 30, 120, 50);
        radio2.setBounds(120, 30, 120, 50);
        custom.setBounds(120, 30, 120, 50);
        btnGrp.add(radio1);
        btnGrp.add(radio2);

        JComboBox campsList = new JComboBox(camps);
        campsList.setSelectedIndex(5);
        campsList.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent r){
                JComboBox cb = (JComboBox)r.getSource();
                String campName = (String)cb.getSelectedItem();

             }
        });
        JComboBox inboundsList = new JComboBox(inbounds);
        inboundsList.setSelectedIndex(5);
        inboundsList.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent r){
                JComboBox cb = (JComboBox)r.getSource();
                String campName = (String)cb.getSelectedItem();

             }
        });
        JComboBox statList = new JComboBox(stat);
        statList.setSelectedIndex(5);
        statList.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent r){
                JComboBox cb = (JComboBox)r.getSource();
                String campName = (String)cb.getSelectedItem();

             }
        });

    JComboBox groupList = new JComboBox(groups);
    groupList.setSelectedIndex(5);
    groupList.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent r){
            JComboBox cb = (JComboBox)r.getSource();
            String campName = (String)cb.getSelectedItem();

         }
    });

    JComboBox lisList = new JComboBox(lis);
    lisList.setSelectedIndex(5);
    lisList.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent r){
            JComboBox cb = (JComboBox)r.getSource();
            String campName = (String)cb.getSelectedItem();

         }
    });

        ButtonGroup btnGrp1 = new ButtonGroup();
        JRadioButton campaignY = new JRadioButton("Yes");
        JRadioButton campaignN = new JRadioButton("No");
        JLabel campaign = new JLabel("Select all campaigns?");
        campaignY.setBounds(120, 30, 120, 50);
        campaignN.setBounds(120, 30, 120, 50);
        btnGrp1.add(campaignY);
        btnGrp1.add(campaignN);

        ButtonGroup btnGrp2 = new ButtonGroup();
        JRadioButton inboundY = new JRadioButton("Yes");
        JRadioButton inboundN = new JRadioButton("No");
        JLabel inbound = new JLabel("Select all inbound groups?");
        btnGrp2.add(inboundY);
        btnGrp2.add(inboundN);

        ButtonGroup btnGrp3 = new ButtonGroup();
        JRadioButton statusY = new JRadioButton("Yes");
        JRadioButton statusN = new JRadioButton("No");
        JLabel status = new JLabel("Select all call statuses?");
        btnGrp3.add(statusY);
        btnGrp3.add(statusN);
        statusY.setBounds(120, 30, 120, 50);
        statusN.setBounds(120, 30, 120, 50);

        ButtonGroup btnGrp4 = new ButtonGroup();
        JRadioButton userGroupY = new JRadioButton("Yes");
        JRadioButton userGroupN = new JRadioButton("No");
        JLabel userGroup = new JLabel("Select all user groups?");
        btnGrp4.add(userGroupY);
        btnGrp4.add(userGroupN);

        userGroupY.setBounds(120, 30, 120, 50);
        userGroupN.setBounds(120, 30, 120, 50);

        ButtonGroup btnGrp5 = new ButtonGroup();
        JRadioButton listsY = new JRadioButton("Yes");
        JRadioButton listsN = new JRadioButton("No");
        JLabel lists = new JLabel("Select all lists?");
        btnGrp5.add(listsY);
        btnGrp5.add(listsN);

        panel2.add(custom);
        panel2.add(radio1);
        panel2.add(radio2);
        campsList.setVisible(true);
        panel2.add(campsList);

        panel2.add(campaign);
        panel2.add(campaignY);
        panel2.add(campaignN);
        inboundsList.setVisible(true);
        panel2.add(inboundsList);
      //  campaign.setVisible(false);
      //  campaignY.setVisible(false);
      //  campaignN.setVisible(false);

        panel2.add(status);
        panel2.add(statusY);
        panel2.add(statusN);
        panel2.add(statList);
       // status.setVisible(false);
       // statusY.setVisible(false);
       // statusN.setVisible(false);

        panel2.add(inbound);
        panel2.add(inboundY);
        panel2.add(inboundN);
        panel2.add(inboundsList);
      //  inbound.setVisible(false);
     //   inboundN.setVisible(false);
     //   inboundY.setVisible(false);

        panel2.add(userGroup);
        panel2.add(userGroupY);
        panel2.add(userGroupN);
        panel2.add(groupList);
       // userGroup.setVisible(false);
      //  userGroupN.setVisible(false);
      //  userGroupY.setVisible(false);

        panel2.add(lists);
        panel2.add(listsY);
        panel2.add(listsN);
        panel2.add(lisList);
      //  lists.setVisible(false);
     //   listsN.setVisible(false);
     //   listsY.setVisible(false);
bf1o4zei

bf1o4zei1#

所以,有两个问题。
首先,当列和行属性都被指定时,列值是ignore,这可以在javadocs中找到
当构造函数或setrows和setcolumns方法将行数和列数都设置为非零值时,将忽略指定的列数。相反,列数是根据指定的行数和布局中组件的总数来确定的。
所以,在你的情况下,我把它改成 JPanel panel2 = new JPanel(new GridLayout(0, 4)); 其次,一个示例组件只能驻留在一个父级中一次。

panel2.add(custom);
    panel2.add(radio1);
    panel2.add(radio2);
    panel2.add(campsList);

    panel2.add(campaign);
    panel2.add(campaignY);
    panel2.add(campaignN);
    panel2.add(inboundsList);

    //...

    panel2.add(inbound);
    panel2.add(inboundY);
    panel2.add(inboundN);
    panel2.add(inboundsList);

你在添加 inboundsList 两次,这样做的效果是首先将其从容器中移除,然后将其重新添加到新位置。
您需要创建的的新示例 JComoboBox 使用相同的数据模型

相关问题