2014-04-23 43 views
0

我正在研究一個程序,該程序有幾個基於JComboBox中的用戶選擇需要發生的功能。將ActionListener附加到JComboBox

理想情況下,用戶選擇將被拉出並用於一系列if/else語句中以更改必要的值。

這是我收到的錯誤:

method addActionListener in class JComboBox<E> cannot be applied to given types; 
     classJComboBox.addActionListener(this); 

required: ActionListener 

found: DWTools 

reason: actual argument DWTools cannot be converted to ActionListener by method invocation conversion 
    where E is a type-variable: 
    E extends Object declared in class JComboBox 

這裏是我的代碼(目前,所有我有它設置做的就是把所選的類名的JTextField用於測試目的):

import java.awt.*; 
import javax.swing.*; 
import java.awt.event.*; 
import java.text.*; 
import java.util.*; 

public class DWTools extends JFrame 
{ 
private String[] classes = { "Bard", "Cleric", "Druid", 
"Fighter", "Paladin", "Thief", "Ranger", "Wizard" }; 
public JTabbedPane mainPane; 
public JPanel basic; 
public JPanel moves; 
public JPanel spells; 
private JLabel nameJLabel; 
private JLabel classJLabel; 
private JTextField nameJTextField; 
private JComboBox classJComboBox; 

public DWTools() 
{ 
    createUserInterface(); 
} 

public void createUserInterface() 
{ 
    Container contentPane = getContentPane(); 

    JPanel topPanel = new JPanel(); 
    topPanel.setLayout(new BorderLayout()); 
    contentPane.add(topPanel); 

    createBasic(); 
    createMoves(); 
    createSpells(); 

    mainPane = new JTabbedPane(); 
    mainPane.addTab("Basic Information", basic); 
    mainPane.addTab("Character Moves", moves); 
    mainPane.addTab("Character Spells", spells); 
    topPanel.add(mainPane, BorderLayout.CENTER); 

    setTitle("Dungeon World Tools"); 
    setSize(500, 500); 
    setVisible(true); 
} 

public void createBasic() 
{ 
    basic = new JPanel(); 
    basic.setLayout(null); 

    JLabel nameJLabel = new JLabel("Name:"); 
    nameJLabel.setBounds(10, 10, 50, 25); 
    basic.add(nameJLabel); 

    JTextField nameJTextField = new JTextField(); 
    nameJTextField.setBounds(60, 10, 100, 25); 
    basic.add(nameJTextField); 

    JLabel classJLabel = new JLabel("Class:"); 
    classJLabel.setBounds(10, 60, 50, 25); 
    basic.add(classJLabel); 

    Arrays.sort(classes); 

    JComboBox classJComboBox = new JComboBox(classes); 
    classJComboBox.setBounds(60, 60, 100, 25); 
    classJComboBox.setMaximumRowCount(8); 
    basic.add(classJComboBox); 
    classJComboBox.addActionListener(this); 

public void classJComboBoxActionPerformed(ActionEvent event) 
{ 
    JComboBox box = (JComboBox)event.getSource(); 
    String chosenClass = (String)box.getSelectedItem(); 
    updateInfo(chosenClass); 
} 

protected void updateInfo(String cclass) 
{ 
    String chosen = cclass; 
    nameJTextField.setText("Chosen:" + chosen); 
} 

public void createMoves() 
{ 
    moves = new JPanel(); 
    moves.setLayout(null); 
} 

public void createSpells() 
{ 
    spells = new JPanel(); 
    spells.setLayout(null); 
} 

public static void main(String[] args) 
{ 
    DWTools application = new DWTools(); 
    application.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
} 

}

回答

0

addActionListener()需要一個ActionListener作爲參數。你通過this作爲參數。這是DWTools的一個實例。並且DWTools不實現ActionListener接口。因此編譯錯誤。

使DWTools執行ActionListener或更好,傳遞一個實現ActionListener的匿名內部類的實例。

+0

我試過了。它現在給我這個錯誤:DWTools不是抽象的,也不會在ActionListener中重寫抽象方法actionPerformed(ActionEvent)。 – user3523949

+0

這是因爲您必須實現在ActionListener中聲明的方法,否則您實際上並未實現接口。我建議你在嘗試使用Swing之前瞭解有關繼承,接口和多態的更多信息。 –

0

除了JBNizets的回答,我想強調的是該組合框的解決方案應該是專用的ActionListener。在「主類」中實現此接口可能會在需要多個操作偵聽器時很快導致混亂。所以一個解決方案可以簡單地

classJComboBox.addActionListener(new ActionListener() 
{ 
    @Override 
    public void actionPerformed(ActionEvent e) 
    { 
     // Call the already existing method 
     // for handling this event: 
     classJComboBoxActionPerformed(e); 
    } 
}); 
相關問題