gpt4 book ai didi

Java - 从主方法中的另一个方法访问变量

转载 作者:行者123 更新时间:2023-11-30 08:01:03 25 4
gpt4 key购买 nike

如果有人可以提供帮助,这是完整的代码,我基本上是在尝试制作一个电子邮件可用性检查器。我想要的只是将变量行也放入我的 Jlist 中,这样它将输出所有电子邮件并在该电子邮件已被检查时从 JList 中删除。

   class main{
public static void main(String[] args) throws InterruptedException {
gui.gui();

}
}

|

 import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;

class tuna {
public static void tuna() throws InterruptedException, FileNotFoundException {
WebDriver driver = new ChromeDriver();
try (BufferedReader br = new BufferedReader(new FileReader("emails.txt"))) {
String line;


while ((line = br.readLine()) != null) {
driver.get("https://signup.live.com/signup?wa=wsignin1.0&rpsnv=12&ct=1465840004&rver=6.7.6643.0&wp=&wreply=https%3a%2f%2fwww.msn.com%2fen-us%2fhomepage%2fSecure%2fPassport%3fru%3dhttp%253a%252f%252fwww.msn.com%252f%253focid%253dmailsignout%2526pfr%253d1&id=1184&pcexp=True&bk=1465840005&uiflavor=web&uaid=dc151162984741948b8371ce3e0c865e&mkt=EN-US&lc=1033&lic=1");
WebElement Email = driver.findElement(By.id("MemberName"));
WebElement Refresher = driver.findElement(By.id("FirstName"));
WebElement TakenErrorMsg = driver.findElement(By.id("MemberNameError"));
Email.sendKeys(line);
Refresher.click();
Thread.sleep(650);
if (TakenErrorMsg.isDisplayed()) {
System.out.println(line + " is taken!");

} else {
System.out.println(line + " is not taken!");
}
}
} catch (IOException e) {
e.printStackTrace();
}

}
}

|

import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.FileNotFoundException;


public class gui extends JFrame {
public static void gui() {


JLabel label1 = new JLabel("Hotmail availability checker");
label1.setVisible(true);
label1.setSize(250, 250);
label1.setLocation(165, -100);

JLabel label2 = new JLabel("Emails unchecked");
label2.setVisible(true);
label2.setSize(250, 240);
label2.setLocation(40, -82);




JButton b1 = new JButton("Start");
b1.setSize(200,50);
b1.setLocation(140, 300);
b1.setVisible(true);


JScrollPane scrollPane = new JScrollPane();
final DefaultListModel emailsChecked = new DefaultListModel();
emailsChecked.addElement("AAA");

final JList list = new JList(emailsChecked);
list.setVisible(true);
list.setVisible(true);
list.setLocation(20,50);
list.setSize(150,200);
scrollPane.setViewportView(list);



JFrame frames = new JFrame();
frames.setTitle("Hotmail availability checker");
frames.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frames.setLayout(null);
frames.setVisible(true);
frames.setSize(500, 400);
frames.setLocationRelativeTo(null);
frames.add(b1);
frames.add(label1);
frames.add(list);
frames.add(label2);
frames.add(scrollPane);


b1.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
try {
tuna.tuna();
} catch (InterruptedException e1) {
e1.printStackTrace();
} catch (FileNotFoundException e1) {
e1.printStackTrace();
}


}
});
}
}

最佳答案

如果 line 是在另一个方法中声明的局部变量,那么您不能从您的 main 方法访问它。

方法中的局部变量只能从声明在局部变量范围内的类访问(在有限的情况下);例如使用匿名内部类。这显然不适用于 main 方法。

我不知道建议的解决方案是什么,但如果一个方法需要查看另一个方法的局部变量,那么您的应用程序的设计显然有问题。

关于Java - 从主方法中的另一个方法访问变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38035093/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com