[英]How can I access the same text area variable?
The code below works as such:下面的代码是这样工作的:
<script> </script>
, including tags themselves<script> </script>
中删除内容,包括标签本身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个选项:
GUI
, then it will have access to all fields of it's outer class (no need for static in that case) GUI
, then it will have access to all fields of it's outer class (no need for static in that case)GUI.textArea
GUI.textArea
访问 static 字段
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.