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

时间:2016-06-26 03:59:41

标签: java

如果有人可以提供帮助,这是完整的代码,我基本上是在尝试制作电子邮件可用性检查程序。我想要的只是将变量行放入我的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();
                }


            }
        });
    }
}

1 个答案:

答案 0 :(得分:3)

如果line是在另一种方法中声明的局部变量,则无法通过main方法访问它。

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

我不知道建议作为解决方案,但如果一个方法需要查看另一个方法的局部变量,那么应用程序的设计显然有些错误。