我在代码重复、结构等方面遇到了问题,所以我开始按“类”划分事物。
public void setCorrectStreetType() {
String text = comboVia.getSelectedItem().toString().toUpperCase();
if (text.equals("CARRER")) {
comboVia.setSelectedIndex(0);
} else if (text.equals("PASSEIG")) {
comboVia.setSelectedIndex(1);
} else if (text.equals("PLAÇA")) {
comboVia.setSelectedIndex(2);
} else if (text.equals("RAMBLA")) {
comboVia.setSelectedIndex(3);
} else if (text.equals("RONDA")) {
comboVia.setSelectedIndex(4);
} else if (text.equals("AVINgudA")) {
comboVia.setSelectedIndex(5);
} else if (text.equals("VIA")) {
comboVia.setSelectedIndex(6);
} else if (text.equals("TRAVESSERA")) {
comboVia.setSelectedIndex(7);
} else if (text.equals("PASSATGE")) {
comboVia.setSelectedIndex(8);
} else if (text.equals("BULEVAR")) {
comboVia.setSelectedIndex(9);
} else if (text.equals("OTROS") || text.equals("null") || text.equals("")) {
comboVia.setSelectedIndex(10);
}
}
理论上,这部分代码从 JComboBox
获取数据并将其与我之后编写的文本进行比较,如果满足条件,它基本上会将组合框设置为某个索引。
问题是,代码没有意义。当用户请求时,诸如“Carrer”或“Plaça”之类的文本应该直接来自数据库。因此,我无法真正掌握通过此代码管道获取“街道类型”的变量的想法。
// variable vias = the text coming from the db
if (vias.equals(?)){
setCorrectStreetType();
}
street.setText(direccion + "");
street.setforeground(new java.awt.Color(255,255,255));
TL;DR 我需要通过比较两个文本并在组合框中设置索引的类来传递变量。