Java多个CheckBoxes

时间:2014-05-14 23:46:55

标签: java swing jframe jcheckbox

我遇到多个CheckBox的问题。当我使用我的代码创建它们时,它只显示最后一个框“bolonska zmes”,其他框仅在鼠标悬停时显示。我认为这可能是图层的一些问题,但我不知道该怎么做。谢谢你的帮助。

  public class OknoPizzaVlastna extends JFrame
    {
         private String nazvy[] = { "cesnak", "feferony", "hrasok", "cibula",
         "kecup", "tatarskaOmacka", "vajce",
         "kapia", "fazula", "kukurica", "ananas", "brokolica",
         "Niva", "Mozarella", "olivy", "inovec udeny", "articoky",
         "klobasa", "sampiony", "salama", "slanina", "hranolky", "tuniak",
         "sunka", "kuracie maso", "syr", "Morska zmes", "bolonska zmes"};

         private JCheckBox boxes[];

        public OknoPizzaVlastna()
        {
            boxes = new JCheckBox[nazvy.length];

            for (int i = 0; i < nazvy.length; i++) 
            {            
                createrCheckBox(i);          
            }

            setTitle("Vlastna Pizza");
            setSize(480,320);
            setVisible(true);
            setResizable(true);

            getContentPane().setLayout(null);
            setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        }

        public void createrCheckBox(int i)
        {
            boxes[i] = new JCheckBox();
            //proper locations will be solved later
            boxes[i].setLocation(62+i*30,54+i*20); 
            boxes[i].setSize(100,50);
            boxes[i].setText(nazvy[i]);
            boxes[i].setSelected(false);
            boxes[i].setVisible(true);
            getContentPane().add(boxes[i]);  
        } 
    }

1 个答案:

答案 0 :(得分:2)

如果问题是,&#39;如何布局此GUI?&#39;答案可能是:

要组织强大的GUI组件,请使用布局管理器或combinations of them,以及布局填充和放大器。 white space的边框。

在这种情况下,我们使用单个列GridLayout,每个复选框都有一个EmptyBorder,以便在我们继续向下菜单时连续缩进它们。

Pizza Menu

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

public class OknoPizzaVlastna extends JFrame {

    private String nazvy[] = {
        "cesnak", "feferony", "hrasok", "cibula",
        "kecup", "tatarskaOmacka", "vajce",
        "kapia", "fazula", "kukurica", "ananas", "brokolica",
        "Niva", "Mozarella", "olivy", "inovec udeny", "articoky",
        "klobasa", "sampiony", "salama", "slanina", "hranolky", "tuniak",
        "sunka", "kuracie maso", "syr", "Morska zmes", "bolonska zmes"
    };

    JPanel ui= new JPanel(new GridLayout(0,1,4,4));

    private JCheckBox boxes[];

    public OknoPizzaVlastna() {
        ui.setBorder(new EmptyBorder(10,10,10,10));
        setContentPane(ui);
        boxes = new JCheckBox[nazvy.length];

        for (int i = 0; i < nazvy.length; i++) {
            createrCheckBox(i);
        }

        setTitle("Vlastna Pizza");
        pack();
        setVisible(true);
        setResizable(true);
        setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
    }

    public void createrCheckBox(int i) {
        boxes[i] = new JCheckBox(nazvy[i]);
        boxes[i].setBorder(new EmptyBorder(0,i*30,0,0));
        ui.add(boxes[i]);
    }

    public static void main(String[] args) {
        Runnable r = new Runnable() {
            public void run() {
                JFrame frame = new OknoPizzaVlastna();
            }
        };
        SwingUtilities.invokeLater(r);
    }
}