这个场景的最佳解决方案是什么:我在Netbeans中实现了基于SOAP的Web服务,客户端应该点击多个复选框,然后将这些复选框发送到服务器并存储。假设我的webservice有这些复选框,可以选择全部或部分:
种族: 1.Caucasian 2.South-Est Asian 3.南亚 4.African 5.其他
在同一个网络服务的另一部分,我实现了与
相关的复选框性别: 1.Male 2.Female
如下所示,可以选择其中一个或两个,但对于种族部分而言,解决方案看起来非常复杂,我还有其他部分甚至更多的复选框!
客户端代码:
private void salvaActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
disease=malattia.getText();
etastr=eta.getText();
age=java.lang.Integer.parseInt(etastr);
description=descrizione.getText();
//Here i'm initiating the array using sexint as the dimension
sexarra=new String[sexint];
if(sexint==1)
sexarra[0]=sexone;
else if(sexint==0)
JOptionPane.showMessageDialog(null, "Bisogna specificare almeno un valore del campo sesso", "Errore", JOptionPane.ERROR_MESSAGE);
else{
sexarra[0]=sexone;
sexarra[1]=sextwo;}
// I define the parameters and afterwards send them to server
Vector parametri = new Vector();
parametri.addElement(new Parameter("malattia", String.class, disease, null));
parametri.addElement(new Parameter("age", int.class, age, null));
parametri.addElement(new Parameter("descrizione", String.class, description, null));
parametri.addElement(new Parameter("sexarra",String[].class, sexarra, null));
//Code related to calculating sexint which is used above as the dimension to array
private void femaleActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
if(female.isSelected()){
if(sexint==0){
sexint++;
sexone=female.getText();
}
else if(sexint==1){
sexint++;
sextwo=female.getText();
}
else
sexint--;
}
}
private void maleActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
if(male.isSelected()){
if(sexint==0){
sexint++;
sexone=male.getText();
}
else if(sexint==1){
sexint++;
sextwo=male.getText();
}
else
sexint--;
}
}
与服务器端相关的代码:
public String aggiungi_malattia(String malattia, int eta, String descrizione, String[] sexarra) {
String ris = "no";
String q = null, w = null;
String errore = connetti();
if(sexarra.length == 2){
q = "INSERT INTO malattia (nome, eta, descrizione, sesso) "
+ "VALUES ('" + malattia + "','" + eta + "','" + descrizione + "','" + sexarra[0] + "')";
w="INSERT INTO malattia (nome, eta, descrizione, sesso) "
+ "VALUES ('" + malattia + "','" + eta + "','" + descrizione + "','" + sexarra[1] + "')";
}
{
q = "INSERT INTO malattia (nome, eta, descrizione, sesso) "
+ "VALUES ('" + malattia + "','" + eta + "','" + descrizione + "','" + sexarra[0] + "')";
谢谢大家的时间和精力!
答案 0 :(得分:0)
创建一个列表或数组,以维护类级别所选项目(字符串)的列表。
List<String> selectedEthnicities = new ArrayList();
您应该从将多个复选框转换为复选框数组开始,例如:
JCheckBox[] ethnicities = new JCheckBox[5];
ethnicities[0] = new JCheckBox("USA");
ethnicities[1] = new JCheckBox("Brazil");
ethnicities[2] = new JCheckBox("Mexico");
ethnicities[3] = new JCheckBox("Canada");
然后您可以在循环框中添加ItemListener
:
for(int i=0; i< ethnicities .length; i++) {
ethnicities [i].addItemListener(new EthnicityItemListener(selectedEthnicities ));
}
请注意,您已将selectedEthnicities
数组列表传递给项侦听器。
现在,EthnicityItemListener
项侦听器的实现可能如下所示:
private class EthnicityItemListener implements ItemListener {
ArrayList selectedList;
private EthnicityItemListener(ArrayList<String> selectedEthnicities) {
this.selectedList = selectedEthnicities;
}
@Override
public void itemStateChanged(ItemEvent e) {
//add or remove selected chk box text to the list based on
//selection state.
if(e.getStateChange() == ItemEvent.SELECTED){
this.selectedList.add(((JCheckBox)e.getSource()).getText());
}else {
this.selectedList.remove(((JCheckBox)e.getSource()).getText());
}
System.out.println(selectedList);
}
}
可以循环类变量selectedEthnicities
来创建SQL语句。
这样就可以减少很多不必要的代码复杂性。