复选框对齐3行

时间:2014-09-13 14:18:01

标签: java swing layout jcheckbox

您好我正在编写一个JFrame,它有很多动态生成的复选框我想确保这些复选框有4个不同的列,最后的提交按钮请帮助

这是代码

final JFrame frame=new JFrame("Button Frame");
            //JCheckBox[] checkBoxes = new JCheckBox[6];
            final JCheckBox[] checkBoxes= //= {new JCheckBox("bool"), new JCheckBox("list"), new JCheckBox("3"), new JCheckBox("4"), new JCheckBox("5"), new JCheckBox("6")};
            {
                new JCheckBox("alist"),new JCheckBox("arithmetic"),new JCheckBox("ASCIInumbers"),new JCheckBox("bag"),    
                new JCheckBox("basicSize"),new JCheckBox(" basis_emit"),new JCheckBox("bitstring"),new JCheckBox("bit"),        
                new JCheckBox("blast"),new JCheckBox("bool"),new JCheckBox("canonical"),new JCheckBox("Coder"),       
                new JCheckBox("combin"),new JCheckBox("complex"),new JCheckBox("ConseqConv"),new JCheckBox("container"),
                new JCheckBox("Decode"),new JCheckBox("DeepSyntax"),new JCheckBox("defCNF"),new JCheckBox("divides"),
                new JCheckBox("Encode"),new JCheckBox("EncodeVar"),new JCheckBox("extended_emit"),new JCheckBox("extreal"),new JCheckBox("fcp"),       
                new JCheckBox("finite_map"),new JCheckBox("fixedPoint"),new JCheckBox("float"),new JCheckBox("fmaptree"),   
                new JCheckBox("frac"),new JCheckBox("gcdset"),new JCheckBox("gcd"),new JCheckBox("HolSmt"),new JCheckBox("hrat"),    
                new JCheckBox("hreal"),new JCheckBox("ieee"),new JCheckBox("ind_type"), new JCheckBox("inftree"),
                new JCheckBox("int_arith"),new JCheckBox("integer_word"),new JCheckBox("integerRing"),new JCheckBox("integer"),     
                new JCheckBox("integral"),new JCheckBox("intExtension"),new JCheckBox("intreal"),new JCheckBox("lbtree"),new JCheckBox("lebesgue"),   
                new JCheckBox("lim"),new JCheckBox("list"),new JCheckBox("llist"),new JCheckBox("logroot"),   
                new JCheckBox("marker"),new JCheckBox("measure"),new JCheckBox("nets"),new JCheckBox("normalForms"),   
                new JCheckBox("numeral_bit"),new JCheckBox("numeral"),new JCheckBox("numpair"),new JCheckBox("numposrep"),    
                new JCheckBox("numRing"),new JCheckBox("num"),new JCheckBox("Omega_Automata"),new JCheckBox("Omega"),new JCheckBox("one"),
                new JCheckBox("operator"),new JCheckBox("option"),new JCheckBox("pair"),     
                new JCheckBox("Past_Temporal_Logic"),new JCheckBox("path"),new JCheckBox("patricia_casts"),new JCheckBox("patricia"),  
                new JCheckBox("poly"),new JCheckBox("poset"),new JCheckBox("powser"),        
                new JCheckBox("pred_set"),new JCheckBox("prelim"),new JCheckBox("prim_rec"),new JCheckBox("primeFactor"),new JCheckBox("probability"),    
                new JCheckBox("quantHeuristics"),new JCheckBox("quote"),      
                new JCheckBox("quotient_list"),new JCheckBox("quotient_option"),new JCheckBox("quotient_pair"),new JCheckBox("quotient_pred_set"),new JCheckBox("quotient_sum"),     
                new JCheckBox("quotient"),new JCheckBox("ratRing"),new JCheckBox("rat"),new JCheckBox("real_sigma"),
                new JCheckBox("realax"),new JCheckBox("real"),new JCheckBox("relation"),new JCheckBox("res_quan"),     
                new JCheckBox("rich_list"),new JCheckBox("ringNorm"),new JCheckBox("ring"),new JCheckBox("sat"),new JCheckBox("semi_ring"),     
                new JCheckBox("seq"),new JCheckBox("set_relation"),new JCheckBox("sorting"),    
                new JCheckBox("state_option"),new JCheckBox("state_transformer"),new JCheckBox("string_num"),new JCheckBox("string"), 
                new JCheckBox("sum_num"),new JCheckBox("sum"),new JCheckBox("Temporal_Logic"),   
                new JCheckBox("topology"),new JCheckBox("transc"),new JCheckBox("update"),new JCheckBox("util_prob"),new JCheckBox("while"),new JCheckBox("words")};

            int numb=checkBoxes.length;
            //List<Checkbox> checkboxes = new ArrayList<Checkbox>();
            int align = BoxLayout.PAGE_AXIS;

            frame.setLayout(new FlowLayout(align));
           // String labels[] = {"A", "B", "C", "D", "E", "F"};
            for (int i = 0; i < numb; i++) {
                //checkBoxes[i].setText(labels[i]);
                frame.add(checkBoxes[i]);
               // Checkbox checkbox = new Checkbooks(labels[i]);
                //checkboxes.add(checkbox); //for further use you add it to the list
                //frame.add(checkbox);
            }
            JButton button = new JButton("Submit");
            frame.add(button);

        }


            frame.setSize(600,600);
            frame.setVisible(true);
            button.addActionListener(new ActionListener() {
                int numb=checkBoxes.length;

                @Override
                public void actionPerformed(ActionEvent arg0) {
                    for(int a=0;a<numb;a++)
                    {
                        if (checkBoxes[a].isSelected()) {

                            //execute("app load [\""+checkBoxes[a].getText()+"Theory\"]; open "+checkBoxes[a].getText()+"Theory;");
                            JOptionPane.showMessageDialog(frame, checkBoxes[a].getText());
                        }
                    }

这是我当前视图的图像 enter image description here                         }

2 个答案:

答案 0 :(得分:5)

不要使用FlowLayout。相反,您应该使用GridLayout

阅读How to Use GridLayout上Swing教程中的部分,了解更多信息和示例。

如果您不希望按钮作为Grid的一部分,则需要使用不同的布局管理器嵌套面板。

答案 1 :(得分:2)

只需使用JPanel即可。将其布局设置为BorderLayout

选择另一个JPanel,将其布局设置为GridLayout0行和4列。现在只需添加JCheckBox es即可。

另一个,JPanel添加JButton

现在将JPanel包含JCheckBox的{​​{1}}添加到第一个面板BorderLayout.CENTER,将第三个JPanel添加到BorderLayout.PAGE_END。就是这样。

这里尝试这个代码示例:

import java.awt.*;
import javax.swing.*;

public class AlignCheckBoxExample {

    private static final String[] data = {
        "alist", "arithmetic", "ASCIInumbers", "bag",
        "basicSize", "basis_emit", "bitstring", "bit",
        "blast", "bool", "canonical", "coder", "combin",
        "complex", "ConseqConv", "container", "Decode",
        "DeepSyntax", "defCNF", "divides", "Encode",
        "EncodeVar", "extended_emit", "extreal", "fcp",
        "finite_map", "fixedPoint", "float", "fmaptree",
        "frac", "gcdset", "gcd", "HolSmt", "hrat",
        "hreal", "ieee", "ind_type", "inftree",
        "int_arith", "integer_word", "integerRing", "integer",
        "integral", "intExtension", "intreal", "lbtree", "lebesgue",
        "lim", "list", "llist", "logroot",
        "marker", "measure", "nets", "normalForms",
        "numeral_bit", "numeral", "numpair", "numposrep"
    };

    private JCheckBox[] checkBox;
    private JButton submitButton;

    private void displayGUI() {
        JFrame frame = new JFrame("Align JComboBox Example");
        frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);

        JPanel contentPane = new JPanel();
        contentPane.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
        contentPane.setLayout(new BorderLayout(5, 5));

        checkBox = new JCheckBox[data.length];
        JPanel centerPanel = new JPanel();
        centerPanel.setLayout(new GridLayout(0, 4, 5, 5));
        for (int i = 0; i < data.length; i++) {
            checkBox[i] = new JCheckBox(data[i]);
            centerPanel.add(checkBox[i]);
        }
        contentPane.add(centerPanel, BorderLayout.CENTER);

        JPanel footerPanel = new JPanel();
        submitButton = new JButton("Submit");
        footerPanel.add(submitButton);
        contentPane.add(footerPanel, BorderLayout.PAGE_END);

        frame.setContentPane(contentPane);
        frame.pack();
        frame.setLocationByPlatform(true);
        frame.setVisible(true);
    }

    public static void main(String[] args) {
        Runnable runnable = new Runnable() {
            @Override
            public void run() {
                new AlignCheckBoxExample().displayGUI();
            }
        };
        EventQueue.invokeLater(runnable);
    }
}

这是输出:

JCHECKBOX EXAMPLE