如何在子菜单

时间:2017-07-18 09:19:12

标签: java swing combobox

实际上,我正在制作一个普通的文本编辑器,其中包含一个带有子菜单“font”的菜单“Change”。所以我想在“font”菜单中添加字体名称的comboBox,当我选择comboBox的值时,Text窗格的字体会被更改。这是我的代码:

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();
  }
}

但问题是当我在box(ComboBox)上添加动作侦听器时,它不起作用。

0 个答案:

没有答案
相关问题