将以前使用的字符串导入新方法
Importing previously used strings into a new method
我有一个字符串(在我的主要方法中初始化),我想在新的 JFrame window 方法中使用。我有以下代码:
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));
}
如何让它工作?
这样的事情可能对你有帮助:
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()));
}
在 main
之外定义您的 String
:
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
。如果您的新方法在同一个 class 中,只需键入 linkLocation
即可,否则使用 classname.linkLocation
.
假设您可以在与 linkLocation
相同的位置访问 JButton
,您可以尝试一下 JButton.setActionCommand()
:
public static void main(String[] args) {
// ...
String linkLocation = link.getAttribute("href");
jButton.setActionCommand(linkLocation);
// ..
}
现在您可以使用它了,根据您的 post 下面是按钮处理程序:
public void actionPerformed(ActionEvent e) {
desk.browse(new URI(e.getActionCommand()));
}
我有一个字符串(在我的主要方法中初始化),我想在新的 JFrame window 方法中使用。我有以下代码:
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));
}
如何让它工作?
这样的事情可能对你有帮助:
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()));
}
在 main
之外定义您的 String
:
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
。如果您的新方法在同一个 class 中,只需键入 linkLocation
即可,否则使用 classname.linkLocation
.
假设您可以在与 linkLocation
相同的位置访问 JButton
,您可以尝试一下 JButton.setActionCommand()
:
public static void main(String[] args) {
// ...
String linkLocation = link.getAttribute("href");
jButton.setActionCommand(linkLocation);
// ..
}
现在您可以使用它了,根据您的 post 下面是按钮处理程序:
public void actionPerformed(ActionEvent e) {
desk.browse(new URI(e.getActionCommand()));
}