【问题标题】:How to return a String stored in JTextField in one class to another class?如何将存储在一个类中的 JTextField 中的字符串返回到另一个类?
【发布时间】:2016-02-25 16:27:29
【问题描述】:

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

这是主类

public class MainClass {

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

    System.out.println(filePath);
 }
 }

这是用户界面类

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 ;
}


}

【问题讨论】:

  • 使用JDialog 而不是JFrame - 这会让一切变得更容易。
  • 除了@resueman 的建议,见The Use of Multiple JFrames, Good/Bad Practice?
  • 系统out print的当前输出是什么,是否为null?
  • 我相信它是因为您处理了 JFrame,调试并查看在调用 dispose 方法之前和之后 String 变量发生了什么,干杯

标签: java string swing jtextfield


【解决方案1】:

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

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

并像这样从 UserInterface 调用此方法:

MainClass.setText(pathField.getText());

行前:

frame.dispose();

【讨论】:

  • static 不是一个应该依赖的解决方案,当 OP 想要使用该类的多个实例时会发生什么?
  • 真的,你会如何避免使用静态?
  • 按照建议,使用模态JDialog
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-10-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-11-14
相关资源
最近更新 更多