我正在为学生注册编写一个GUI Java程序,它将从数据库中检索可用的类,为用户提供选择类的选项,然后将其存储在DB中。
我尝试做的事情到目前为止取得了部分成功,是这样的 - 我创建了一个包含可用专业的组合框(从DB获取),检索该专业的可用类并显示复选框对于这些课程。
此代码存在两个问题。
ArrayList<JCheckBox> checkBoxes=new ArrayList<JCheckBox>();
//combox action listener below
public void actionPerformed(ActionEvent e)
{
//get all available classes for the selected major
avail_class = new String[count_class];
//get all available class ids
avail_classid = new String[count_class];
JCheckBox checkbox;
int xdim = 75;
for (int i = 0; i < count_class; i++)
{
checkbox = new JCheckBox(avail_classid[i] + "-" + avail_class[i]);
checkbox.setBackground(new Color(0, 255, 255));
checkbox.setBounds(183, xdim, 289, 23);
contentPane.add(checkbox);
checkBoxes.add(checkbox);
checkbox.setEnabled(true);
xdim = xdim + 50;
}
}
修改
对于我的第二个问题,我调用了repaint()并且它有效。对于第一个,我做了以下事情:
if(flag < 0)
//flag will be raised whenever there is a change in the selected major. For ex, from web dev to data analytics
for(int i = 0; i < checkBoxes.size(); i++)
{
checkBoxes.get(i).setVisible(false);
System.out.println("old Checkboxes invisible!" + i);
}
答案 0 :(得分:1)
您需要将repaint和revalidate函数调用到包含复选框的容器,以使用新的复选框重新绘制它。