我有两个ItemListener
s,它们具有为每个声明一个变量的内部方法。我想比较上层类中的两个变量。
这是我的代码:
public class MyFrame extends JFrame {
public MyFrame () {
initialize();
}
public void initialize() {
this.setSize(600,200);
this.setTitle("Frame");
this.setLocationRelativeTo(null);
String [] brothers = {"John", "Francis"};
String [] sisters = {"Sarah","Diana"};
JPanel centralPnl = new JPanel();
this.getContentPane().add(centralPnl, BorderLayout.CENTER);
final JComboBox broBox = new JComboBox(brothers);
centralPnl.add(broBox);
final JComboBox sisBox = new JComboBox(sisters);
centralPnl.add(sisBox);
broBox.addItemListener(new ItemListener() {
@Override
public void itemStateChanged(ItemEvent e) {
if (e.getStateChange() == ItemEvent.SELECTED) {
String selectedValue = broBox.getSelectedItem().toString();
//connection to DB to get data; DBConnection is a class that connects to DB
//and a have getAge method which returns age of brothers in array format
String[] age = new DBConnection().getAge(selectedValue);
String johnAge = String[0];
}
}
});
sisBox.addItemListener(new ItemListener() {
@Override
public void itemStateChanged(ItemEvent e) {
if (e.getStateChange() == ItemEvent.SELECTED) {
String selectedValue = broBox.getSelectedItem().toString();
//connection to DB to get data; DBConnection is a class that connects to DB
//and a have getAge method which returns age of sisters in array format
String[] age = new DBConnection().getAge(selectedValue);
String sarahAge = String[0]
}
}
});
}
我想比较johnAge
和sarahAge
。我应该怎么办?