Java 当组合框位于子菜单中时,如何在组合框上添加操作侦听器

Java 当组合框位于子菜单中时,如何在组合框上添加操作侦听器,java,swing,combobox,Java,Swing,Combobox,实际上,我正在制作一个普通的文本编辑器,其中包含一个带有子菜单字体的菜单更改。因此,我想在字体菜单中添加字体名称的组合框,当我选择组合框的值时,文本窗格的字体将更改。以下是我的代码: import java.awt.Font; import java.awt.GraphicsEnvironment; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.JCombo

实际上,我正在制作一个普通的文本编辑器,其中包含一个带有子菜单字体的菜单更改。因此,我想在字体菜单中添加字体名称的组合框,当我选择组合框的值时,文本窗格的字体将更改。以下是我的代码:

import java.awt.Font;
import java.awt.GraphicsEnvironment;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JScrollPane;
import javax.swing.JTextPane;

public class TextEditor implements ActionListener{
  JMenuBar menubar;
  JMenu fileMenu;
  JMenu changeMenu;

  JMenuItem newItem;
  JMenuItem editorframe;

  JMenu font;
  JMenu style;
  JMenu size;
  JMenu color;



  JTextPane textPane;
  JScrollPane scrollPane;

  JScrollPane contentPane;
  /** Contain the list of the font*/
  JComboBox<String> box;
  /** This array contain font family which are installed on system */
  String fontArray[];
  JFrame frame;

  public TextEditor() {
    JFrame.setDefaultLookAndFeelDecorated(true);

    frame = new JFrame();
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    //Retrieve all installed Font from system and store them into array
    GraphicsEnvironment gg = GraphicsEnvironment.getLocalGraphicsEnvironment();
    fontArray = gg.getAvailableFontFamilyNames();

    menubar = createMenuBar();
    frame.setJMenuBar(menubar);

    contentPane = createContentPane();
    frame.setContentPane(contentPane);

    frame.setVisible(true);
    frame.setSize(500,500);
  }
  /**
   * This method create a Menu Bar and Menu and MenuItem in t
   * @return menubar MenuBar of frame
   */
  public JMenuBar createMenuBar() {
    JMenuBar menubar = new JMenuBar();
    fileMenu = new JMenu("File");


    newItem = new JMenuItem("New");
    editorframe = new JMenuItem("EditorFrame");

    changeMenu = new JMenu("Change");
    //sub menus are used to change the attribute of text of the Text Pane.
    font = new JMenu("Font");
    style = new JMenu("Style");
    size = new JMenu("Size");
    color = new JMenu("Color");
    //adding all subMenu into changeMenu
    changeMenu.add(font);
    changeMenu.add(style);
    changeMenu.add(size);
    changeMenu.add(color);

    box = new JComboBox<String>(fontArray);

    box.addActionListener(this);
    font.add(box);

    fileMenu.add(newItem);
    fileMenu.add(editorframe);

    newItem.addActionListener(this);
    menubar.add(fileMenu);
    menubar.add(changeMenu);

    return menubar;
  }
  /**
   * This method create contentPane for frame and create text pane and add it into contentPane
   * @return scrollPane content pane of frame
   */
  public JScrollPane createContentPane() {
    textPane =  new JTextPane();
    scrollPane = new JScrollPane(textPane);
    scrollPane.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);

    return scrollPane;
  }
  @Override
  public void actionPerformed(ActionEvent e) {
    if(e.getSource() == box) {
      String fontName = box.getSelectedItem().toString();
      textPane.setFont(new Font(fontName, Font.PLAIN, 20));
    }
  }
  public static void main(String args[]) {
    new TextEditor();
  }
}

但问题是,当我在组合框上添加action Listener时,它不起作用。

你能检查actionPerformed是否被调用吗?是的,我检查了,但当我单击组合框时actionPerformed从未调用可能重复的。她的@Catalina岛问题是actionListener在sub中组合框时不工作menu@GaneshPatel:对。我提到了两个变通办法。