我正在尝试使用此程序,可以弹出一个窗格,其中包含宿舍“私人房间,有线电视,互联网”等选项。当用户单击每个复选框以表示他们想要该选项时。我希望选项从文本区域消失。
到目前为止,我已经得到了程序运行时出现的列表,以及附加输出的复选框,但它现在只删除了我点击的所有内容,然后在单击不同的框时添加选项。
我不确定我还能做些什么。 你能帮忙吗?
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
public class JDormRoom extends JFrame implements ItemListener {
FlowLayout flow = new FlowLayout();
JCheckBox privateRm = new JCheckBox("Private");
JCheckBox internet = new JCheckBox("Internet");
JCheckBox cable = new JCheckBox("Cable TV");
JCheckBox microwave = new JCheckBox("Microwave");
JCheckBox refrigerator = new JCheckBox("Refrigerator");
JTextArea outArea = new JTextArea("Private Room"
+ "\nInternet" + "\nCable"
+"\nMicrowave" + "\nRefrigerator", 30,30);
String dormOption = "";
String output = "";
public JDormRoom() {
super("Check Box");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel pane = new JPanel();
pane.setLayout(flow);
pane.add(privateRm);
privateRm.addItemListener(this);
pane.add(internet);
internet.addItemListener(this);
pane.add(cable);
cable.addItemListener(this);
pane.add(microwave);
microwave.addItemListener(this);
pane.add(refrigerator);
refrigerator.addItemListener(this);
pane.add(outArea);
setContentPane(pane);
}
public static void main(String[] arguments) {
JDormRoom dFrame = new JDormRoom();
dFrame.setSize(450,300);
dFrame.setVisible(true);
}
public void itemStateChanged(ItemEvent check) {
Object source = check.getItem();
int select = check.getStateChange();
if (source == privateRm) {
//int select = check.getStateChange();
if (privateRm.isSelected())
if(select == ItemEvent.SELECTED) {
dormOption = "Private Room\n";
output += dormOption;
}
}
if (source == internet) {
//int select = check.getStateChange();
if (internet.isSelected())
if(select == ItemEvent.SELECTED) {
dormOption = "Internet\n";
output += dormOption;
}
}
if (source == cable) {
//int select = check.getStateChange();
if (cable.isSelected())
if(select == ItemEvent.SELECTED) {
dormOption = "Cable TV\n";
output += dormOption;
}
}
if (source == microwave) {
//int select = check.getStateChange();
if (microwave.isSelected())
if(select == ItemEvent.SELECTED) {
dormOption = "Microwave\n";
output += dormOption;
}
}
if (source == refrigerator) {
//int select = check.getStateChange();
if (refrigerator.isSelected())
if(select == ItemEvent.SELECTED) {
dormOption = "Refrigerator\n";
output += dormOption;
}
}
outArea.setText(output);
}
}