2014-03-25 45 views
0

一个我无法弄清的基本问题,尝试了很多东西并且无法使其工作,我需要能够获取变量的值/文本 字符串输入; 这样我可以在不同类的再次使用,为了做到基于结果的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; 
     } 

    } 

} 
+1

你试过一个getter方法吗? – Sionnach733

回答

3

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

例如:

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(); 
    } 
} 
0

你的方法是有点混乱:

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

我猜你想要做这样的事情:

public String getInput() { 
    return input; 
} 

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

而且你JFrame不可见。设置这样的能见度setVisible(true)