有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java控制jPanel中复选框组件的选择

我正在用NetBeans和几个JFrame开发一个Java Swing+FX应用程序项目

我有一个面板(jPanelPattern),其中包含一些复选框(jCheckBoxPat1到jCheckBoxPat18,它们在应用程序中不断添加到面板中…)将图标/图像作为按钮组。当我点击save按钮时,我需要将选中复选框的图标/图像从数据库中记录的路径保存到目录中

“保存”按钮的操作事件包含:

for(Component c : jPanelPattern.getComponents()) { // Scan All
        if (c.isSelected()) { // Get the selected checkbox
            // Save the icon/image with FileChooser...
        } else {
            // Show message dialog for no selection...
        }
    }

Missing/Error: There is no "isSelected" and "getIcon" for the component "c" to continue select and save processes

问题:如何将扫描的组件定义为复选框

还有其他解决办法吗


共 (1) 个答案

  1. # 1 楼答案

    你只需要将其转换(并检查它是否真的是复选框)

        if(c instanceof JCheckBox){
            JCheckBox box = (JCheckBox) c;
        }
    

    Box现在是你的JCheckBox