Java 将以前使用的字符串导入到新方法中

Java 将以前使用的字符串导入到新方法中,java,string,selenium,jpanel,desktop,Java,String,Selenium,Jpanel,Desktop,我有一个字符串(在我的main方法中初始化),我想在新的JFrame窗口方法中使用它。我有以下代码: public static void main(String[] args){ WebElement link = driver.findElement(By.xpath("//*[@id='pagecontainer']/section/ul/li[1]/a")); String linkLocation = link.getAttribute("href"); } 对于我的main方法和我

我有一个字符串(在我的main方法中初始化),我想在新的JFrame窗口方法中使用它。我有以下代码:

public static void main(String[] args){
WebElement link = driver.findElement(By.xpath("//*[@id='pagecontainer']/section/ul/li[1]/a"));
String linkLocation = link.getAttribute("href");
}
对于我的main方法和我的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()));
}

像这样的事情可能会帮助你:

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
之外定义
字符串

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

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

假设您在获取
linkLocation
的同一位置有权访问
JButton
,您可以尝试一下
JButton.setActionCommand()

现在,您可以使用以下按钮处理程序获得它:

public void actionPerformed(ActionEvent e) {
  desk.browse(new URI(e.getActionCommand()));
}

假设您可以在获取链接位置的同一位置访问
JButton
,请尝试
JButton.setActionCommand()

现在,您可以使用以下按钮处理程序获得它:

public void actionPerformed(ActionEvent e) {
  desk.browse(new URI(e.getActionCommand()));
}

您是否可以在获取链接位置的同一位置访问
JButton
?您是否可以在获取链接位置的同一位置访问
JButton