我想在新的JFrame窗口方法中使用一个字符串(在我的main方法中初始化)。我有以下代码:
public static void main(String[] args){
WebElement link = driver.findElement(By.xpath("//*[@id='pagecontainer']/section/ul/li[1]/a"));
String linkLocation = link.getAttribute("href");
}
我的主要方法和我的JPanel中的JButton的以下代码
public void actionPerformed(ActionEvent e)
{
desk.browse(new URI(linkLocation));
}
如何让它发挥作用?
答案 0 :(得分:0)
这样的事情可能对你有帮助:
public String LinkLocation(){
WebElement link = driver.findElement(By.xpath("//*[@id='pagecontainer']/section/ul/li[1]/a"));
String linkLocation = link.getAttribute("href");
return linkLocation;
}
public void actionPerformed(ActionEvent e)
{
desk.browse(new URI(LinkLocation()));
}
答案 1 :(得分:0)
在String
之外定义main
:
public String linkLocation = " ";
public static void main(String[] args) {
WebElement link = driver.findElement(By.xpath("//*[@id='pagecontainer']/section/ul/li[1]/a"));
linkLocation = link.getAttribute("href");
}
您现在可以从其他地方引用linkLocation
。如果您的新方法属于同一个类,只需输入linkLocation
即可,否则请使用classname.linkLocation
。
答案 2 :(得分:0)
假设您在JButton
提取的同一个地方有linkLocation
的访问权限,请尝试JButton.setActionCommand()
:
public static void main(String[] args) {
// ...
String linkLocation = link.getAttribute("href");
jButton.setActionCommand(linkLocation);
// ..
}
现在你可以使用它,根据你的帖子,下面是按钮处理程序:
public void actionPerformed(ActionEvent e) {
desk.browse(new URI(e.getActionCommand()));
}