首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >JCheckbox未显示

JCheckbox未显示
EN

Stack Overflow用户
提问于 2014-01-14 17:29:28
回答 1查看 651关注 0票数 0

我正在尝试创建一个java,而且我在查看复选框时遇到了一些困难。我看了甲骨文教程,并包括了他们所有的代码,但我不知道我错过了什么。有什么想法吗?

代码语言:javascript
复制
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 == )
    }
}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2014-01-14 17:36:08

代码语言:javascript
复制
for(int i = 0; i < NUM_MAP_TYPE; i++) {
        map_type[i].addItemListener(this);
        this.add(map_type[i]);
}

但是最好在面板上使用LayoutManager (例如BoxLayout)。

票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/21119994

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档