java - 需要从用户输入返回值

标签 java swing

一个我无法弄清楚的基本问题,尝试了很多方法但无法使其工作,我需要能够获取变量的值/文本 字符串输入; 这样我就可以在不同的类中再次使用它,以便根据结果执行 if 语句

import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTextField;

        public class pInterface extends JFrame {
    String input;
    private JTextField item1;

    public pInterface() {
        super("PAnnalyser");
        setLayout(new FlowLayout());

        item1 = new JTextField("enter text here", 10);
        add(item1);

        myhandler handler = new myhandler();
        item1.addActionListener(handler);
        System.out.println();

    }

    public class myhandler implements ActionListener {

        // class that is going to handle the events
        public void actionPerformed(ActionEvent event) {

            // set the variable equal to empty
            if (event.getSource() == item1)// find value in box number 1
                input = String.format("%s", event.getActionCommand());

        }

        public String userValue(String input) {
            return input;
        }

    }

}

最佳答案

您可以将窗口显示为模态 JDialog,而不是 JFrame,并将获得的 String 放入可通过 getter 方法访问的私有(private)字段中。然后调用代码可以轻松获取 String 并使用它。请注意,不需要单独的字符串字段(您将其称为“输入”),因为我们可以轻松简单地直接从 JTextField 中提取字符串(在我们的“getter”方法中)。

例如:

import java.awt.Dialog.ModalityType;
import java.awt.Window;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.FocusAdapter;
import java.awt.event.FocusEvent;

import javax.swing.*;
import javax.swing.text.JTextComponent;

public class TestPInterface {
   @SuppressWarnings("serial")
   private static void createAndShowGui() {
      final JFrame frame = new JFrame("TestPInterface");

      // JDialog to hold our JPanel
      final JDialog pInterestDialog = new JDialog(frame, "PInterest",
            ModalityType.APPLICATION_MODAL);
      final MyPInterface myPInterface = new MyPInterface();
      // add JPanel to dialog
      pInterestDialog.add(myPInterface);
      pInterestDialog.pack();
      pInterestDialog.setLocationByPlatform(true);

      final JTextField textField = new JTextField(10);
      textField.setEditable(false);
      textField.setFocusable(false);      

      JPanel mainPanel = new JPanel();
      mainPanel.add(textField);
      mainPanel.add(new JButton(new AbstractAction("Get Input") {

         @Override
         public void actionPerformed(ActionEvent e) {
            // show dialog
            pInterestDialog.setVisible(true);
            // dialog has returned, and so now extract Text
            textField.setText(myPInterface.getText());
         }
      }));

      frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      frame.getContentPane().add(mainPanel);
      frame.pack();
      frame.setLocationRelativeTo(null);
      frame.setVisible(true);
   }

   public static void main(String[] args) {
      SwingUtilities.invokeLater(new Runnable() {
         public void run() {
            createAndShowGui();
         }
      });
   }
}

// by making the class a JPanel, you can put it anywhere you want
// in a JFrame, a JDialog, a JOptionPane, another JPanel
@SuppressWarnings("serial")
class MyPInterface extends JPanel {

   // no need for a String field since we can 
   // get our Strings directly from the JTextField
   private JTextField textField = new JTextField(10);

   public MyPInterface() {
      textField.addFocusListener(new FocusAdapter() {
         @Override
         public void focusGained(FocusEvent e) {
            JTextComponent textComp = (JTextComponent) e.getSource();
            textComp.selectAll();
         }
      });

      add(new JLabel("Enter Text Here:"));
      add(textField);

      textField.addActionListener(new ActionListener() {

         @Override
         public void actionPerformed(ActionEvent e) {
            Window win = (Window) SwingUtilities.getWindowAncestor(MyPInterface.this);
            win.dispose();
         }
      });
   }

   public String getText() {
      return textField.getText();
   }
}

关于java - 需要从用户输入返回值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22640157/

相关文章:

java - 想要暂停代码直到用户使用按钮

java - 如何将一个字符串分成 2-4 个长 block ?

java - 将数组内容分配给另一个数组

java - 如何创建一个可滚动的 Java Box?

java - 如何让 JButton 发送电子邮件? (Java Swing )

java - TextField.setText() 返回一个奇怪的错误

java - 打印到控制台更新视觉效果? - java

JavaFx - 将 MapProperty 键集绑定(bind)到 SetProperty

java - 有哪些轻量级 map 工具包?

java - Geckodriver 突然开始失败