2016-10-26 20 views
1

我想在新的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"); 
} 

我的主要方法和下面的代碼JButton的在我的JPanel

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

我如何得到這個工作?

+0

你有沒有在''linkLocation''獲取'JButton'的地方? – Arvind

回答

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())); 
} 
0

定義的mainString外:

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

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())); 
} 
相關問題