简体   繁体   English

从另一个类更改JTextArea值

[英]Change JTextArea value from another class

How to allow another class to modify JTextArea from another class? 如何允许另一个类从另一个类修改JTextArea?

For example, let's say I have 2 classes: 例如,假设我有2个类:

Panel.java Panel.java

public class Panel extends JPanel
{
    private JTextArea instructions;

    public Panel()
    {
        instructions = new JTextArea(15,15);
        add(instructions);
    }
}

MenuBar.java MenuBar.java

public class MenuBar extends JMenuBar
{
    private JMenuItem openMenuItem,;

    public MenuBar()
    {
        JMenu fileMenu = new JMenu("File");
        openMenuItem = new JMenuItem("Open");
        fileMenu.add(openMenuItem);
        add(fileMenu);

        MenuListener listener = new MenuListener();
        openMenuItem.addActionListener(listener);
    }

    private class MenuListener implements ActionListener
    {
        public void actionPerformed(ActionEvent event)
        {
            if( event.getSource() == openMenuItem )
            {
                // change value of JTextArea from Panel.java
            }
        }
    }
}

How can I modify instructions JTextArea (Panel.java) from MenuBar.class? 如何从MenuBar.class修改指令JTextArea(Panel.java)?

First of all, the code snippet of MenuBar doesn't compile because of a comma in following line: 首先,由于以下行中的逗号,MenuBar的代码片段无法编译:

private JMenuItem openMenuItem,;

Now you have a class attribute instructions which is a JTextArea object in the class Panel. 现在您有了一个类属性指令,它是类Panel中的JTextArea对象。 This is private so only the class Panel can reach it, which is good. 这是私人的,所以只有班级小组才能到达,这很好。 If you want to get or set the value of that property by another class, you can add a getter/setter for it. 如果要通过其他类获取或设置该属性的值,可以为其添加getter / setter。

In Panel add following code: 在Panel中添加以下代码:

public void setInstructions(JTextArea instructions) {
        this.instructions = instructions;
    }

    public JTextArea getInstructions() {
        return instructions;
    }

setInstructions is the method where you are looking for, you can set the instruction TextArea with the value you pass by as parameter. setInstructions是您要查找的方法,您可以将指令TextArea设置为您传递的值作为参数。

please modify the private class in MenuBarwith following code to show what I mean. 请使用以下代码修改MenuBar中的私有类,以显示我的意思。

private class MenuListener implements ActionListener
    {
        public void actionPerformed(ActionEvent event)
        {
            if( event.getSource() == openMenuItem )
            {
                Panel panel = new Panel(); //I don't know where you get your panel data from so I create a new one
                panel.setInstructions(new JTextArea("I'm a text area and I have instructions")); //set the JTextArea value

                System.out.println(panel.getInstructions().getText()); //test print to show how you can get the JTextArea text
            }
        }
    }

I also added a test print to validate the setter works. 我还添加了一个测试打印来验证setter的工作原理。

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

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