【问题标题】:Needing to return value from user input需要从用户输入返回值
【发布时间】:2014-03-25 16:08:16
【问题描述】:

一个我无法弄清楚的基本问题,尝试了很多东西但无法让它工作,我需要能够获取变量的值/文本 字符串输入; 这样我就可以在不同的类中再次使用它,以便根据结果执行 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;
        }

    }

}

【问题讨论】:

  • 你试过getter方法了吗?

标签: java swing


【解决方案1】:

您可以将窗口显示为模态 JDialog,而不是 JFrame,并将获得的 String 放入可以通过 getter 方法访问的私有字段中。然后调用代码就可以很方便的获取到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();
   }
}

【讨论】:

    【解决方案2】:

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

    我已经在相同的上下文中发布了答案。

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

    【讨论】:

    • 如有任何混淆请告诉我。
    【解决方案3】:

    你的方法有点混乱:

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

    我猜你想做这样的事情:

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

    此外,您的JFrame 尚不可见。像这样设置可见性setVisible(true)

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-12-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-05-30
      • 1970-01-01
      • 2021-11-10
      相关资源
      最近更新 更多