简体   繁体   English

如何从JMenuItem访问JTextPane内容?

[英]How can I get access to the JTextPane content from a JMenuItem?

I'm working on a text editor using Java (Swing). 我正在使用Java(Swing)开发文本编辑器。 So far I have made the body. 到目前为止,我已经做了身体。 I'm having problem with this feature: 我对此功能有疑问:

  • New (JMenuItem) (empties the content of the JTextArea). 新建(JMenuItem)(清空JTextArea的内容)。

When the user clicks on the button, the JTextArea content should be replaced with an empty string. 当用户单击按钮时,应将JTextArea内容替换为空字符串。 This is my code (I'm ommiting code that's not relevant to the problem, such as menu creation, menu items addition, only adding the classes.) 这是我的代码(我省略了与该问题无关的代码,例如菜单创建,菜单项添加,仅添加类。)

This is the TextArea class: 这是TextArea类:

class MyTextArea extends JTextArea implements ActionListener {
    JTextArea myTextArea;
    public MyTextArea() {
        init();

    }

    public void init(){
        setLineWrap(true);
    }

    @Override
    public void actionPerformed(ActionEvent e) {

    }
}

(Empty, as you can see.) (如您所见,为空。)

This is the MenuBar class: 这是MenuBar类:

class MyMenuBar extends JMenuBar implements ActionListener {
    private JMenu mArchivo;
    private JMenuItem mNuevo;

    public MyMenuBar(){
        init();
        add(mArchivo);
    }

    private void init() {
        mArchivo = settingUpMenus("Archivo", "Archivo", 'A');
        mNuevo = settingUpMenuItems("Nuevo", "Nuevo", 'N');
        mArchivo.add(mNuevo);
    }

    private JMenu settingUpMenus(String mTitle, String mDescription,         
char mMnemonic) {
        JMenu mMenu;
        mMenu = new JMenu(mTitle);
        mMenu.setMnemonic(mMnemonic);

mMenu.getAccessibleContext().setAccessibleDescription(mDescription);
        mMenu.setActionCommand(mTitle);
        mMenu.addActionListener(this::actionPerformed);
        return mMenu;
    }

    private JMenuItem settingUpMenuItems(String mTitle, String 
mDescription, char mMnemonic) {
        JMenuItem mMenuItem;
        mMenuItem = new JMenuItem(mTitle);
        mMenuItem.setMnemonic(mMnemonic);



  mMenuItem.getAccessibleContext().
setAccessibleDescription(mDescription);
        mMenuItem.setActionCommand(mTitle);
        mMenuItem.addActionListener(this::actionPerformed);
        return mMenuItem;
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        switch(e.getActionCommand()) {
            case "Nuevo":
                onNew();
                break;
        }
    }

    private void onNew() {

    }
}

And this is the class constructor where I add the JTextArea and the JMenu with it's items and all. 这是类构造函数,我在其中添加JTextArea和JMenu及其所有项。

public Editor() {
    JScrollPane myScrollPane = new JScrollPane(new MyTextArea(), 
    JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED, 
    JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
    systemLook();
    setTitle("Text editor");
    setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    setSize(new Dimension(800, 700));
    setVisible(true);
    setJMenuBar(new MyMenuBar());
    add(myScrollPane);
}

However, I have tried many ways for my new button to get access to the current instance of JTextArea and to modify it, such as getting the parent classes with the ActionEvent object in the actionPerformed method inside the JMenu class. 但是,我已经尝试了许多新按钮来访问JTextArea的当前实例并对其进行修改,例如在JMenu类内的actionPerformed方法中使用ActionEvent对象获取父类。 But none of the intents I have done can access to the JTextArea. 但是我做的所有意图都无法访问JTextArea。 Any ideas? 有任何想法吗? Should I implement it another way? 我应该以其他方式实现它吗?

Just pass it as a parameter in the constructor of the menu bar like 只需将其作为参数传递给菜单栏的构造函数,例如

...
private JTextArea myTextArea;

public MyMenuBar(MyTextArea myTextArea){
    init();
    add(mArchivo);
    this.myTextArea = myTextArea;
}
...

and in the main would look like 并且主要看起来像

MyTextArea myTextArea = new MyTextArea();
JScrollPane myScrollPane = new JScrollPane(myTextArea, 
        JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED, 
        JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
systemLook();
setTitle("Text editor");
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
setSize(new Dimension(800, 700));
setVisible(true);
setJMenuBar(new MyMenuBar(myTextArea));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM