2014-03-25 76 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)