我一直在研究html文本编辑程序,我想知道是否有人可以帮我解决这个小问题。我有两个jmenus,但其中一个是行为不端。我有两个问题。 1菜单中的菜单项有一个子菜单箭头,尽管它不是子菜单
是我已经在菜单中添加了一个动作监听器,但是当我点击动作时,会发生doesent。这是代码。
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");
}
}
答案 0 :(得分:6)
因为您已将其定义为JMenu
而非JMenuItem
JMenuItem viewMenuEasyInsert = new JMenu("Easy insert");
应该是
JMenuItem viewMenuEasyInsert = new JMenuItem("Easy insert");