如何将存储在一个类中的JTextField中的String返回给另一个类?

时间:2016-02-25 16:27:30

标签: java string swing jtextfield

在类UserInterface中我在单击浏览器按钮后在JTextField中设置String(filePath)。现在我想关闭我的窗口并将存储在JTextField中的String返回给调用UserInterface的MainClass

这是MainClass

public class MainClass {

public void mainClass(String IDEFilePath) throws IOException
{
    UserInterface userInterface = new UserInterface();
    String filePath = userInterface.createInterface();

    System.out.println(filePath);
 }
 }

这是UserInterface类

package com.mycompany.prototype2;

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

public class UserInterface {

JButton browseButton;
JTextField pathField;
JFrame frame;
String IdeFilePath;
JPanel panel, topPanel, bottomPanel;

UserInterface()
{
    // Creating instance of JFrame
    frame = new JFrame("Convert IDEJUnit to WebDriver");

    // Setting the width and height of frame
    frame.setSize(350, 200);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    panel = new JPanel(new BorderLayout());

    topPanel = new JPanel(new BorderLayout());
    topPanel.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
    bottomPanel = new JPanel(new BorderLayout());
    bottomPanel.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));

    panel.add(topPanel, BorderLayout.NORTH);
    panel.add(bottomPanel, BorderLayout.SOUTH);
}



public String createInterface() {    

    //Top Panel
    JLabel IDEFilePathLabel = new JLabel("IDE File Path", JLabel.CENTER);
    pathField = new JTextField(10);


    browseButton = new JButton("Browse");
    browseButton.addActionListener(new ActionListener() { 
        @Override
        public void actionPerformed(ActionEvent e)
        {
            String defaultDirectoryPath = "C:\\Users\\Parul\\Desktop";
            JFileChooser chooser = new JFileChooser(defaultDirectoryPath);
            int returnVal = chooser.showOpenDialog(frame);
            chooser.setDialogTitle("Select Location");
            if (returnVal == JFileChooser.APPROVE_OPTION) 
            {
                java.io.File file = chooser.getSelectedFile();
                pathField.setText(chooser.getSelectedFile().toString());
                IdeFilePath = pathField.getText();
            } 
        }
    });



    topPanel.add(IDEFilePathLabel, BorderLayout.WEST);
    topPanel.add(pathField, BorderLayout.CENTER);
    topPanel.add(browseButton, BorderLayout.EAST);


    //Bottom Panel
    JButton okButton = new JButton("OK");
    bottomPanel.add(okButton, BorderLayout.CENTER);

    okButton.addActionListener(new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent e) {
            frame.dispose();
        }
    });

    frame.add(panel);
    frame.setVisible(true);

    return IdeFilePath ;
}


}

1 个答案:

答案 0 :(得分:0)

将以下代码添加到MainClass中,并确保将filePath设为静态,

public static void setString(String text)
{
    filePath = text;
}

并从UserInterface调用此方法,如下所示:

MainClass.setText(pathField.getText());

行之前:

frame.dispose();