简体   繁体   English

JCheckbox不显示

[英]JCheckbox not showing

I'm trying to create a java GUI and I'm having some trouble getting my checkboxes to show. 我正在尝试创建Java GUI,但在显示复选框时遇到了一些麻烦。 I've looked at the oracle tutorial and included all the code they have but I'm not sure what I'm missing. 我查看了oracle教程,并包括了它们所拥有的所有代码,但是我不确定自己缺少什么。 Any ideas? 有任何想法吗?

public class HPAProgram {
    public static void main(String[] args) {
        MapWindow map = new MapWindow();        
    }
}

import java.awt.event.*;
import javax.swing.*;  //notice javax
public class MapWindow extends JFrame
{
    private static final int WIDTH = 600, HEIGHT = 800;

    SettingsButtonsPanel button_panel = new SettingsButtonsPanel();

    public MapWindow()
    {
        setLocationRelativeTo(null);
        setTitle("HPA* Test");
        setSize(WIDTH, HEIGHT);
        setVisible(true);
        setDefaultCloseOperation(EXIT_ON_CLOSE);

        add(button_panel);
    }

}

import java.awt.event.*;

import javax.swing.*;  //notice javax
public class SettingsButtonsPanel extends JPanel implements ItemListener{
    private static final int WIDTH = 600, HEIGHT = 200;
    private static final int NUM_MAP_TYPE = 2;

    private JCheckBox[] map_type;

    JPanel panel = new JPanel();

    public SettingsButtonsPanel(){
        this.setBounds(0,0,WIDTH, HEIGHT);

        map_type = new JCheckBox[NUM_MAP_TYPE];

        map_type[0] = new JCheckBox("Sparse");
        map_type[0].setSelected(true);
        map_type[0].setVisible(true);
        map_type[0].setLocation(0,0);
        map_type[0].setSize(100,100);


        map_type[1] = new JCheckBox("Maze");
        map_type[1].setSelected(false);

        for(int i = 0; i < NUM_MAP_TYPE; i++)
            map_type[i].addItemListener(this);
    }

    public void itemStateChanged(ItemEvent e)
    {
        Object source = e.getItemSelectable();
        //if(source == )
    }
}
for(int i = 0; i < NUM_MAP_TYPE; i++) {
        map_type[i].addItemListener(this);
        this.add(map_type[i]);
}

But it's better to use a LayoutManager (eg BoxLayout) for the panel. 但是最好在面板上使用LayoutManager(例如BoxLayout)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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