我在java swing中编写了一个框架。在其中我有一个复选框。我想,在点击复选框后,其他项目将改变它的可见性。我试图按照下面的代码进行操作但是没有按照我的意愿工作。
public InFrm() {
setTitle("In");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 450, 300);
getContentPane().setLayout(new GridLayout(1, 1, 0, 0));
seeMe=false;
JSplitPane splitPane = new JSplitPane();
splitPane.setResizeWeight(0.7);
splitPane.setOrientation(JSplitPane.VERTICAL_SPLIT);
getContentPane().add(splitPane);
JPanel panel = new JPanel();
splitPane.setLeftComponent(panel);
panel.setLayout(null);
JPanel panel_1 = new JPanel();
splitPane.setRightComponent(panel_1);
panel_1.setLayout(null);
JLabel lblKind= new JLabel("Kind");
lblKind.setBounds(10, 8, 33, 14);
lblKind.setVisible(seeMe);
panel_1.add(lblKind);
JComboBox ChoiceOd = new JComboBox();
ChoiceOd.setBounds(53, 5, 28, 20);
ChoiceOd.setVisible(seeMe);
panel_1.add(ChoiceOd);
// more items using seeMe
JCheckBox chckbxOd = new JCheckBox("Od");
chckbxOd.addItemListener(new ItemListener() {
public void itemStateChanged(ItemEvent e) {
seeOd();
}
});
chckbxOd.setBounds(6, 150, 97, 23);
panel.add(chckbxOd);
}
protected void seeOd() {
if(seeMe){
seeMe=false;
}
else
{
seeMe=true;
}
}
答案 0 :(得分:1)
在see方法中,您只设置标志,但当然不会设置组件的可见性。将可见性直接设置到组件,而不是工作
答案 1 :(得分:0)
你的听众应该是这样的:
public void itemStateChanged(ItemEvent ev) {
Object item = ev.getItem();
if (ev.getStateChange() == ItemEvent.DESELECTED) {
//hide the component associated with this item
} else {
//show the component associated with this item
}
}