【问题标题】:JCheckbox not showingJCheckbox 不显示
【发布时间】:2014-02-02 21:26:17
【问题描述】:

我正在尝试创建一个 Java GUI,但在显示我的复选框时遇到了一些问题。我查看了 oracle 教程并包含了他们拥有的所有代码,但我不确定我缺少什么。有什么想法吗?

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 == )
    }
}

【问题讨论】:

    标签: java swing user-interface checkbox panel


    【解决方案1】:
    for(int i = 0; i < NUM_MAP_TYPE; i++) {
            map_type[i].addItemListener(this);
            this.add(map_type[i]);
    }
    

    但最好为面板使用 LayoutManager(例如 BoxLayout)。

    【讨论】:

      猜你喜欢
      • 2014-08-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-03-11
      相关资源
      最近更新 更多