java复选框三级联动怎么写?
时间: 2023-07-03 15:20:19 浏览: 93
java 三级联动的东西
5星 · 资源好评率100%
Java复选框三级联动可以通过监听器和递归实现。以下是一个简单的示例代码,供参考:
```java
import javax.swing.*;
import javax.swing.event.*;
import java.awt.*;
public class CheckboxTest extends JFrame {
private JCheckBox[] provinceCheckbox = { new JCheckBox("江苏"), new JCheckBox("浙江"), new JCheckBox("上海") };
private JCheckBox[][] cityCheckbox = {
{ new JCheckBox("南京"), new JCheckBox("苏州"), new JCheckBox("无锡") },
{ new JCheckBox("杭州"), new JCheckBox("宁波"), new JCheckBox("温州") },
{ new JCheckBox("上海市") }
};
private JCheckBox[][][] districtCheckbox = {
{
{ new JCheckBox("玄武区"), new JCheckBox("秦淮区"), new JCheckBox("建邺区") },
{ new JCheckBox("常熟市"), new JCheckBox("张家港市"), new JCheckBox("昆山市") },
{ new JCheckBox("惠山区"), new JCheckBox("锡山区"), new JCheckBox("滨湖区") }
},
{
{ new JCheckBox("西湖区"), new JCheckBox("江干区"), new JCheckBox("下城区") },
{ new JCheckBox("江北区"), new JCheckBox("海曙区"), new JCheckBox("鄞州区") },
{ new JCheckBox("鹿城区"), new JCheckBox("龙湾区"), new JCheckBox("瓯海区") }
},
{
{ new JCheckBox("黄浦区"), new JCheckBox("徐汇区"), new JCheckBox("长宁区") }
}
};
public CheckboxTest() {
JPanel panel = new JPanel(new GridLayout(0, 2));
for (JCheckBox checkBox : provinceCheckbox) {
panel.add(checkBox);
checkBox.addItemListener(new CheckboxListener(cityCheckbox[getIndex(provinceCheckbox, checkBox)]));
}
add(panel);
setSize(400, 150);
setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
private int getIndex(Object[] array, Object object) {
for (int i = 0; i < array.length; i++) {
if (array[i] == object) {
return i;
}
}
return -1;
}
private class CheckboxListener implements ItemListener {
private JCheckBox[] cityCheckbox;
public CheckboxListener(JCheckBox[] cityCheckbox) {
this.cityCheckbox = cityCheckbox;
}
@Override
public void itemStateChanged(ItemEvent e) {
if (e.getStateChange() == ItemEvent.SELECTED) {
JPanel panel = (JPanel) getContentPane().getComponent(0);
for (JCheckBox checkBox : cityCheckbox) {
panel.add(checkBox);
checkBox.addItemListener(new CheckboxListener2(districtCheckbox[getIndex(provinceCheckbox, e.getItem())][getIndex(cityCheckbox, checkBox)]));
}
revalidate();
repaint();
} else {
JPanel panel = (JPanel) getContentPane().getComponent(0);
for (JCheckBox checkBox : cityCheckbox) {
panel.remove(checkBox);
}
revalidate();
repaint();
}
}
}
private class CheckboxListener2 implements ItemListener {
private JCheckBox[] districtCheckbox;
public CheckboxListener2(JCheckBox[] districtCheckbox) {
this.districtCheckbox = districtCheckbox;
}
@Override
public void itemStateChanged(ItemEvent e) {
if (e.getStateChange() == ItemEvent.SELECTED) {
JPanel panel = (JPanel) getContentPane().getComponent(0);
for (JCheckBox checkBox : districtCheckbox) {
panel.add(checkBox);
}
revalidate();
repaint();
} else {
JPanel panel = (JPanel) getContentPane().getComponent(0);
for (JCheckBox checkBox : districtCheckbox) {
panel.remove(checkBox);
}
revalidate();
repaint();
}
}
}
public static void main(String[] args) {
new CheckboxTest();
}
}
```
在这个示例中,我们创建了三个省份复选框,每个省份下面有若干个城市复选框,每个城市下面有若干个区县复选框。当选择一个省份时,该省份下面的城市复选框会出现,当选择一个城市时,该城市下面的区县复选框会出现。整个过程是通过监听器实现的,当复选框状态改变时,监听器会相应地增加或删除与之关联的复选框。
阅读全文