gpt4 book ai didi

Java JMenu 无法正常工作

转载 作者:行者123 更新时间:2023-11-29 07:05:23 25 4
gpt4 key购买 nike

我一直在研究一个 html 文本编辑程序,我想知道是否有人可以帮助我解决这个小问题。我有两个 jmenus,但其中一个行为不端。我有两个问题。 1 菜单中的菜单项虽然不是子菜单,但有一个子菜单箭头The problem

  1. 是我向菜单添加了一个 Action 监听器,但是当我单击该 Action 时,该 Action 并没有发生。这是代码。

    import java.awt.GridLayout;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.awt.event.KeyEvent;
    import java.io.File;
    import java.io.FileReader;
    import java.io.FileWriter;
    import java.io.IOException;

    import javax.swing.JButton;
    import javax.swing.JFileChooser;
    import javax.swing.JFrame;
    import javax.swing.JMenu;
    import javax.swing.JMenuBar;
    import javax.swing.JMenuItem;
    import javax.swing.JOptionPane;
    import javax.swing.JTextArea;


    public class Window {

    static boolean saved = false;
    static boolean opened = false;
    static File saveDirectory = null;
    static File openDirectory = null;

    static final JTextArea editor = new JTextArea(10,50);

    public static void openWindowEditor(){
    JFrame f = new JFrame("Easy HTML Text editor");

    JMenuBar menuBar = new JMenuBar();

    JMenu fileMenu = new JMenu("File");

    JMenu viewMenu = new JMenu("View");

    JMenuItem fileMenuSave = new JMenuItem("Save");
    JMenuItem fileMenuOpen = new JMenuItem("Open");
    JMenuItem fileMenuSaveAs = new JMenuItem("Save as...");

    JMenuItem viewMenuEasyInsert = new JMenu("Easy insert");

    fileMenu.setMnemonic(KeyEvent.VK_A);

    viewMenu.setMnemonic(KeyEvent.VK_A);

    menuBar.add(fileMenu);
    menuBar.add(viewMenu);

    fileMenu.add(fileMenuSave);
    fileMenu.add(fileMenuOpen);
    fileMenu.add(fileMenuSaveAs);

    viewMenu.add(viewMenuEasyInsert);

    fileMenuSave.addActionListener(new ActionListener() {
    public void actionPerformed(ActionEvent arg0) {
    save(editor.getText());
    }
    });

    fileMenuOpen.addActionListener(new ActionListener() {
    public void actionPerformed(ActionEvent arg0) {
    open();
    }
    });

    fileMenuSaveAs.addActionListener(new ActionListener() {
    public void actionPerformed(ActionEvent e) {
    saveAs();
    }
    });

    viewMenuEasyInsert.addActionListener(new ActionListener() {
    public void actionPerformed(ActionEvent arg0) {
    openEasyInsertWindow();
    }
    });

    f.setJMenuBar(menuBar);
    f.add(editor);

    f.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
    f.setLayout(new GridLayout(1,1));
    f.pack();
    f.setVisible(true);
    }

    public static void open(){
    JFileChooser chooser = new JFileChooser();
    int returnVal = chooser.showDialog(null, "open");

    if(returnVal == JFileChooser.APPROVE_OPTION){
    File file = chooser.getSelectedFile();

    try{
    editor.read(new FileReader(file), null);
    }catch(IOException e){
    JOptionPane.showMessageDialog(null, e);
    }
    }
    }

    public static void save(String text){
    JFileChooser chooser = new JFileChooser();
    File file = null;

    int returnVal = chooser.showDialog(null, "Save");
    if(returnVal == JFileChooser.APPROVE_OPTION){
    file = (chooser.getSelectedFile());

    String result = text.replace("\n", System.getProperty("line.separator"));

    saveDirectory = chooser.getSelectedFile();
    try{
    FileWriter fw = new FileWriter(file);
    fw.write(result);
    fw.close();
    saved = true;
    }catch(IOException e){
    JOptionPane.showMessageDialog(null, e);
    }
    }
    }

    public static void saveAs(){
    JFileChooser chooser = new JFileChooser();
    File dir;

    int returnVal = chooser.showDialog(null, "Save as...");
    if(returnVal == JFileChooser.APPROVE_OPTION){
    dir = chooser.getSelectedFile();
    try{
    FileWriter fw = new FileWriter(dir);
    fw.write(editor.getText());
    fw.close();
    }catch(IOException e){
    JOptionPane.showMessageDialog(null, e);
    }
    }
    }

    public static void openEasyInsertWindow(){
    JFrame f = new JFrame("easy insert");

    JButton insertDivButton = new JButton("Insert <div> element");
    JButton openInsertSettings = new JButton("Open the insert settings");

    f.setLayout(new GridLayout(2,1));
    f.add(insertDivButton);
    f.add(openInsertSettings);

    f.pack();
    f.setVisible(true);

    JOptionPane.showMessageDialog(editor, "Easy insert");
    }
    }

最佳答案

因为您将其定义为 JMenu 而不是 JMenuItem

JMenuItem viewMenuEasyInsert = new JMenu("Easy insert");

应该是

JMenuItem viewMenuEasyInsert = new JMenuItem("Easy insert");

关于Java JMenu 无法正常工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20395769/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com