我有一個問題。在我的應用程序中,我有一個按鈕,當我點擊它時,文本被保存在一個字符串變量中,當變量不在裏面時,該按鈕的值爲NULL,我怎樣才能保存變量的值並使用它後來。謝謝。在Java中存儲字符串變量以便稍後使用
private void initialize() {
frame = new JFrame();
frame.setBounds(100, 100, 413, 445);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().setLayout(null);
JButton btnNewButton = new JButton("Word for Search");
btnNewButton.setBounds(223, 62, 118, 23);
frame.getContentPane().add(btnNewButton);
textField = new JTextField();
textField.setBounds(35, 63, 131, 20);
frame.getContentPane().add(textField);
textField.setColumns(10);
btnNewButton.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
search = textField.getText();
System.out.println("String for car = " + search);
WebDriver driver = new FirefoxDriver();
driver.get("https://en.wikipedia.org/wiki/" + search);
String tstr1 = driver.findElement(By.xpath("//*[@id='content']")).getText();
System.out.println("String for car = " + tstr1);
driver.close();
}
});
}
}
所有我想要的是當我從public void actionPerformed(ActionEvent e)
的String tstr1
出口保持所保存的數據。
你能提供一個運行的例子,減少到必要的部分,重現你的問題,並指出確切的問題是在哪裏? – SomeJavaGuy
我剛更新了這個問題。 –
你可以簡單地創建一個類實例變量並將字符串保存到它 – SomeJavaGuy