需要从用户输入返回值

时间:2014-03-25 16:08:16

标签: 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;
        }

    }

}

3 个答案:

答案 0 :(得分:3)

您可以将窗口显示为模态JDialog,而不是JFrame,并将获取的String放入可以通过getter方法访问的私有字段中。然后调用代码可以轻松获取String并使用它。请注意,不需要单独的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();
   }
}

答案 1 :(得分:2)

这样做的好方法是使用Callback mechanism

我已在相同的背景下发布了答案。

请在JFrame in separate class, what about the ActionListener?找到。

答案 2 :(得分:0)

你的方法有点令人困惑:

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

我想你想做这样的事情:

public String getInput() {
    return input;
}

public void setInput(String input) {
    this.input = input;
}

此外,您的JFrame尚未显示。像setVisible(true)

一样设置可见性