在Java中访问另一个类中的字符串 - GUI

时间:2017-06-16 09:29:07

标签: java swing

Java的另一个问题......我知道它是基本的,但我不是专业人士。

所以我有Main.java

public class Main {
public static void main(String[] args) {
    System.setProperty("webdriver.gecko.driver", "C:/geckodriver.exe");
    WebDriver driver = new FirefoxDriver();
    driver.get("https://my.emerchantpay.com/");
    eMerchantPay emp = PageFactory.initElements(driver, eMerchantPay.class);
    SwingUtilities.invokeLater(new Runnable() {
       public void run() {
            JFrame frame = new MainFrame("Please enter your credentials");
            frame.setSize(500, 400);
            //frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            frame.setVisible(true);
       }
    });

    emp.uid(username);
    emp.pwd(password);
    emp.LoginButton.click();

该字符串位于DetailsPanel.java

public class DetailsPanel  extends JPanel{

private static final long serialVersionUID = 1234567891;

private EventListenerList listenerList = new EventListenerList();

public DetailsPanel() {
    Dimension size = getPreferredSize();
    size.width = 250;
    setPreferredSize(size);

    setBorder(BorderFactory.createTitledBorder("Personal Details"));

    JLabel nameLabel = new JLabel("Name: ");
    JLabel passwordLabel = new JLabel("Password: ");

    final JTextField nameField = new JTextField(10);
    final JPasswordField passwordField = new JPasswordField(10);

    final JButton addBtn = new JButton("Submit");

    addBtn.addActionListener(new ActionListener() {

        @Override
        public void actionPerformed(java.awt.event.ActionEvent e) {
            String name = nameField.getText();
            String password = passwordField.getText();

            String text = name + ": " + password + "\n";
            JFrame frame = (JFrame) SwingUtilities.getWindowAncestor(addBtn);
            frame.dispose();
            System.out.println (text);
        }
    });

    setLayout(new GridBagLayout());

    GridBagConstraints gc = new GridBagConstraints();

    /// First column ///
    gc.anchor = GridBagConstraints.LINE_END;
    gc.weightx = 0.5;
    gc.weighty = 0.5;

    gc.gridx = 0;
    gc.gridy = 0;
    add(nameLabel, gc);

    gc.gridx = 0;
    gc.gridy = 1;
    add(passwordLabel, gc);

    /// Second column ///
    gc.anchor = GridBagConstraints.LINE_START;

    gc.gridx = 1;
    gc.gridy = 0;
    add(nameField, gc);

    gc.gridx = 1;
    gc.gridy = 1;
    add(passwordField, gc);

    /// Final row ///
    gc.weighty = 10;

    gc.anchor = GridBagConstraints.FIRST_LINE_START;

    gc.gridx = 1;
    gc.gridy = 2;
    add(addBtn, gc);
}

public void fireDetailEvent(DetailEvent event) {
    Object[] listeners = listenerList.getListenerList();

    for (int i=0; i < listeners.length; i += 2) {
        if (listeners[i] == DetailListener.class) {
            ((DetailListener)listeners[i+1]).detailEventOccured(event);
        }
    }
}

public void addDetailsListener(DetailListener listener) {
    listenerList.add(DetailListener.class, listener);
}

public void removeDetailListener(DetailListener listener) {
    listenerList.remove(DetailListener.class, listener);

}

所以字符串就在这里

String name = nameField.getText();

String password = passwordField.getText();

如何从Main.java访问这些内容?我必须将名称值分配给emp.uid(username);

2 个答案:

答案 0 :(得分:1)

您的字段受包保护,因此它们在同一个包中可见。

但是,您需要DetailsPanel的实例才能访问它们。

因此,在Main班级使用DetailsPanel的地方,您可以使用以下内容:

DetailsPanel details = new DeatilsPanel();
...
String name = details.nameField.getText();
char[] password = details.passwordField.getPassword();

(出于安全原因,JPasswordField确实使用getText()方法在[{1}}弃用的情况下返回密码。)

答案 1 :(得分:0)

您可以在DetailsPanel.java文件中创建两个数据成员,并可以将它们命名为name和password。 在Main.java中,您可以创建DetailsPanel的对象,并使用该对象可以访问名称和密码的值

Main.java

DetailsPanel obj=new DetailsPanel();
emp.uid(obj.name);
emp.pwd(obj.password);

DetailsPanel.java

class DetailsPanel{
 String name;
 String password;
} 

希望它能帮助你掌握基础知识。