简体   繁体   English

如何访问相同的文本区域变量?

[英]How can I access the same text area variable?

The code below works as such:下面的代码是这样工作的:

  • User clicks the run button用户单击运行按钮
  • Program reads the file from designated location程序从指定位置读取文件
  • Program removes content from <script> </script> , including tags themselves程序从<script> </script>中删除内容,包括标签本身
  • Program returns edited text into JTextArea called textArea程序将编辑后的文本返回到名为textArea JTextArea

I've tried making it a global variable since it's in two different classes.我尝试将其设为全局变量,因为它位于两个不同的类中。 The run down is that once the user clicks the "run button", the text area initialised in the GUI class will update.问题在于,一旦用户单击“运行按钮”,GUI class 中初始化的文本区域就会更新。

public class GUI{
        static JTextArea textArea;
    public GUI() {
        JFrame frame = new JFrame();
        textArea = new JTextArea(5,30);
        JButton runButton = new JButton("Remove JS");
        JButton importButton = new JButton("Import File");
        JPanel panel = new JPanel();
        
        runButton.addActionListener(new runApp());
        runButton.setBounds(100, 100, 100, 80);
        importButton.addActionListener(new importFile());
        importButton.setBounds(100, 100, 80, 60);
        
        panel.setBorder(BorderFactory.createEmptyBorder(300, 300 , 150, 150));
        panel.setLayout(new GridLayout(0, 1));
        panel.add(textArea);
        panel.add(runButton);
        panel.add(importButton);
        
        frame.add(panel, BorderLayout.CENTER);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setTitle("JavaScript Extractor");
        frame.pack();
        frame.setVisible(true);
    }

    public static void main(String[] args) {
        new GUI();

    }
}

class runApp implements ActionListener{
    public void actionPerformed(ActionEvent arg0) {
        
        RemoveScript run = new RemoveScript();
        

        try {
            File fileObject = new File("C:\\Users\\coker\\Documents\\readJS.txt");
            Scanner reader = new Scanner(fileObject);
            while(reader.hasNextLine()) {
                String output = reader.nextLine();
                textArea.setText(run.removeScript(output));
            }
            reader.close();
        }catch(FileNotFoundException e) {
            System.out.println("An error has occured.");
            e.printStackTrace();
        }
    }
}

3 options: 3个选项:

  • Make your listener class an inner class of GUI , then it will have access to all fields of it's outer class (no need for static in that case) Make your listener class an inner class of GUI , then it will have access to all fields of it's outer class (no need for static in that case)
  • Keep the 2 classes completely separate, and pass a reference to the text field to the listener (eg via constructor parameter).保持 2 个类完全分开,并将对文本字段的引用传递给侦听器(例如,通过构造函数参数)。
  • access the static field via GUI.textArea通过GUI.textArea访问 static 字段

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

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