2013-12-07 24 views
1

我在做一個java表單。Java Swing:帶有文本字段的表單

的理念是:

當用戶插入他的名字,第二個名字,他必須按「提交」按鈕。現在我想用submit按鈕來聽這個動作,但我不知道如何讓用戶在文本字段中輸入:「name」和「second name」。

我想用提交按鈕看到這些數據,但我不知道,如果可能的話。

或者我想知道當用戶點擊「提交」時,我可以看到所有這些數據。

謝謝,這是代碼。

public class appNegozio extends JFrame { 
    private JPanel contentPane; 
    private JTextField textField; 
    private JTextField textField_1; 

/** 
* Launch the application. 
*/ 
    public static void main(String[] args) { 
     EventQueue.invokeLater(new Runnable() { 
      public void run() { 
       try { 
       appNegozio frame = new appNegozio(); 
       frame.setVisible(true); 
       } catch (Exception e) { 
       e.printStackTrace(); 
       } 
      } 
     }); 
    } 

/** 
* Create the frame. 
*/ 
    public appNegozio() { 
     setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     setBounds(100, 100, 450, 300); 
     contentPane = new JPanel(); 
     contentPane.setBorder(new EmptyBorder(5, 5, 5, 5)); 
     setContentPane(contentPane); 
     SpringLayout sl_contentPane = new SpringLayout(); 
     contentPane.setLayout(sl_contentPane); 

     textField = new JTextField(); 
     sl_contentPane.putConstraint(SpringLayout.NORTH, textField, 10,  
        SpringLayout.NORTH, contentPane); 
     sl_contentPane.putConstraint(SpringLayout.WEST, textField, 62, 
        SpringLayout.WEST, contentPane); 
     contentPane.add(textField); 
     textField.setColumns(10); 

     textField_1 = new JTextField(); 
     sl_contentPane.putConstraint(SpringLayout.NORTH, textField_1, 16, 
        SpringLayout.SOUTH, textField); 
     sl_contentPane.putConstraint(SpringLayout.WEST, textField_1, 0, 
        SpringLayout.WEST, textField); 
     contentPane.add(textField_1); 
     textField_1.setColumns(10); 

     JLabel lblNome = new JLabel("Nome"); 
     sl_contentPane.putConstraint(SpringLayout.NORTH, lblNome, 10, 
        SpringLayout.NORTH, contentPane); 
     sl_contentPane.putConstraint(SpringLayout.EAST, lblNome, -7, 
        SpringLayout.WEST, textField); 
     contentPane.add(lblNome); 

     JLabel lblCognome = new JLabel("Cognome"); 
     sl_contentPane.putConstraint(SpringLayout.NORTH, lblCognome, 0, 
        SpringLayout.NORTH, textField_1); 
     sl_contentPane.putConstraint(SpringLayout.EAST, lblCognome, -6, 
        SpringLayout.WEST, textField_1); 
     contentPane.add(lblCognome); 

    JButton btnSubmit = new JButton("Submit"); 
    sl_contentPane.putConstraint(SpringLayout.NORTH, btnSubmit, 24, 
        SpringLayout.SOUTH, textField_1); 
    sl_contentPane.putConstraint(SpringLayout.WEST, btnSubmit, 10, 
        SpringLayout.WEST, contentPane); 
    contentPane.add(btnSubmit); 
    } 
} 

回答

2

一個簡單的方法是使用一個final局部變量每個JTextField。 您可以從ActionListener內訪問它們添加到您的JButton

final JTextField textField = new JTextField(); 

btnSubmit.addActionListener(new ActionListener() { 

    public void actionPerformed(final ActionEvent e) { 
     System.out.println(textField.getText()); 
    } 
}); 
+0

太感謝你了 –

2

不要延長一個JFrame,您不添加任何新的功能框架。

請閱讀Swing教程How to Use Text Fields中的部分,該示例演示了一種更好的方法來構造程序,以便您可以訪問ActionListener中文本字段中的數據。

Swing教程提供了所有Swing組件的示例。

+0

好,謝謝,我讀 –

+0

@GiovanniFar,你不覺得你應該先閱讀教程(decideing要使用哪個回答之前),以瞭解這個建議。將變量作爲最終的程序來編譯,這不是一個好主意。您應該從一開始就正確地設計代碼。 – camickr

0

使用JTextField object.getText()並將它本地存儲在按鈕的Event函數中的String中。使用本地String作爲參考。

String name=jtextfield.getText(); 
相關問題