目前有一个名为menuAddGame的JMenu,它假设打开一个JFrame。但是,我不太确定这是否可行。我目前在JMenu中有一个名为menuGame的JMenuItems的actionListeners。
public void actionPerformed(ActionEvent e)
{
JMenuItem menuItem = (JMenuItem)e.getSource();
System.out.println(menuItem.getText());
JOptionPane.showMessageDialog(null, "Du valgte spillet: " + menuItem.getText());
tempGamemode = menuItem.getText();
showGameMode.setText(menuItem.getText());
dd.setGameMode(menuItem.getText());
dd.setGameMode(tempScore);
question.setText(dd.getQuestion());
}
};
dd.addGameMenu(menuGame, al);
}
addGameMenu(JMenu,ActionListener)包含:
public void addGameMenu(JMenu control, ActionListener al) {
String fileName = "gamemodes.txt";
try {
FileReader inputFile = new FileReader(fileName);
BufferedReader bufferReader = new BufferedReader(inputFile);
String line;
while ((line = bufferReader.readLine()) != null) {
String split[] = line.split("#");
for (int i = 0; i < split.length; i++) {
JMenuItem menuItem = new JMenuItem(split[i]);
menuItem.addActionListener(al);
control.add(menuItem);
}
}
bufferReader.close();
} catch (Exception e) {
System.out.println("Fejl ved linje:" + e.getMessage());
}
}
但是,如何向名为menuAddGame的JMenu添加额外的actionlistener?
提前致谢。