我有以下代码,但我一直收到错误。
void是变量itemStateChanged的无效类型 这是错误消息,但是当我取消无效时,我会收到语法错误。
任何人都可以修复代码。
final JComboBox combobox = new JComboBox(procedurelist);
combobox.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent event) {
procedure = (String) combobox.getSelectedItem();
private void itemStateChanged(ItemEvent e) {
if(e.getStateChange() == ItemEvent.SELECTED) {
if("Focus".equals(e.getItem())) {
textField_9.requestFocus();
}
}
}
}
});
combobox.setBounds(68, 216, 86, 20);
contentPane.add(combobox);
答案 0 :(得分:1)
public void actionPerformed(ActionEvent event) { //Method declaration
procedure = (String) combobox.getSelectedItem();
private void itemStateChanged(ItemEvent e) { //Another Method declaration
if(e.getStateChange() == ItemEvent.SELECTED) {
if("Focus".equals(e.getItem())) {
textField_9.requestFocus();
}
}
}
}
您在另一种方法中声明方法。 Java语法不允许这样做。如果您尝试监控ComboBox
中项目的状态更改,请使用ItemListener
final JComboBox combobox = new JComboBox(procedurelist);
combobox.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent event) {
procedure = (String) combobox.getSelectedItem();
}
});
combobox.addItemListener(new ItemListener(){
public void itemStateChanged(ItemEvent e) {
if(e.getStateChange() == ItemEvent.SELECTED) {
if("Focus".equals(e.getItem())) {
textField_9.requestFocus();
}
}
}
});
答案 1 :(得分:1)
你在一个方法中声明了一个方法,这是不可能做到的。
根据您的目的,您实际上并不需要它......
final JComboBox combobox = new JComboBox(procedurelist);
combobox.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent event) {
procedure = (String) combobox.getSelectedItem();
if("Focus".equals(procedure)) {
// Set text??
textField_9.requestFocusInWindow();
// Select all..
}
}
});
// Bad idea, learn to use layout managers
//combobox.setBounds(68, 216, 86, 20);
contentPane.add(combobox);