2013-06-24 23 views
-1

我想在一個Java應用程序中按「Enter」鍵時提交一個示例代碼以提交一個文本字段。Enter Key as Java Application Form Submission - JFrame

我使用的JFrame和下面的代碼:

package com.sh.st; 

import java.awt.Color; 
import java.awt.GridLayout; 
import java.awt.event.KeyEvent; 

import javax.swing.JButton; 
import javax.swing.JFrame; 
import javax.swing.JLabel; 
import javax.swing.JPanel; 
import javax.swing.JTextField; 




public class SearchScreen extends JFrame{ 

    JButton btsearch; 
    JLabel lbsearch; 
    JTextField txtsearch; 
    JPanel p1; 

    public SearchScreen(){ 

    //Button Creation 
    btsearch= new JButton("Search"); 


    //Label Creation 
    lbsearch= new JLabel("Type Keywords in english to be searched below:"); 


    //TextBox 
    txtsearch= new JTextField(); 


    //Pane Creation 
    p1=new JPanel(); 
    p1.setBackground(Color.gray); 

    //Pane Components 
    p1.add(lbsearch); 
    p1.add(txtsearch); 
    p1.add(btsearch); 

    btsearch.setEnabled(true); 

    //Adding JPaneel  
    getContentPane().add(p1); 

    //JFrame Setup 
    setSize(300,300); 
    setTitle("SHST"); 

    //JFrame Layout Setup 
    p1.setLayout(new GridLayout(3,3)); 


    btsearch.setMnemonic(KeyEvent.VK_ENTER); 

    } 

} 

但問題是,作爲一個虛擬的關鍵,我需要在同一時間「ALT」舉行,這是太unconfortable,而不是直觀一個用戶。

我試着在這裏看到很多頁面在堆棧和谷歌,但沒有人給我答案。我看到了一些命令,比如「addKeyListener」,但我現在無法做到我想要的。

感謝您的幫助

編輯[專題解決]

package com.sh.st; 

import java.awt.event.ActionEvent; 
import java.awt.event.ActionListener; 
import java.io.IOException; 

import javax.swing.AbstractAction; 
import javax.swing.Action; 
import javax.swing.JOptionPane; 
import javax.swing.KeyStroke; 


public class EventSearch extends SearchScreen{ 

    String query; 



    public EventSearch(){ 

     txtsearch.getInputMap().put(keyStroke, key); 
     txtsearch.getActionMap().put(key, enterAction); 


} 

    Action enterAction = new AbstractAction() { 
     @Override 
     public void actionPerformed(ActionEvent e) { 
      try { 

       HttpRequest http = new HttpRequest(CatchQuery()); 

       } catch (IOException e1) { 
       e1.printStackTrace(); //print failure 
       JOptionPane.showMessageDialog(null, "HTTP request failure."); 
      } 
     }}; 
    String key = "ENTER"; 
    KeyStroke keyStroke = KeyStroke.getKeyStroke(key); 



      public String CatchQuery(){ 
       query=txtsearch.getText(); 
       this.dispose(); 
       return query; 
      } 



} 
+0

什麼形式? html輸入表單? GUI的形式? –

+0

你在說什麼形式?我知道它是如何在擺動中完成的,但似乎你沒有使用擺動。 JSP?你必須更加精確(或至少添加一個標籤)。 – Axel

+0

@Axel是的我試圖用swing來做到這一點,我編輯了這篇文章。 Sry爲低信息。 –

回答

2

您可以使用KeyBindingsHow to use KeyBindings

下面是一段代碼

final JTextField textfield = new JTextField(); 
Action enterAction = new AbstractAction() { 
    @Override 
    public void actionPerformed(ActionEvent e) { 
     //do something 
    }}; 
String key = "ENTER"; 
KeyStroke keyStroke = KeyStroke.getKeyStroke(key); 
textfield.getInputMap().put(keyStroke, key); 
textfield.getActionMap().put(key, enterAction); 
+0

我對「enterAction」有困難,試圖創建一個變量,但這個混亂的代碼工作的其他部分,檢查edita一個 –

+0

@VictorOliveira 1)我將keyBinding添加到文本字段,例如,如果你不'如果(e.getSource()== btsearch)這總是爲false,因爲你正在添加動作,如果你想添加焦點和按鍵,添加這個鍵綁定到容器,例如JPanel或frame.getContentPane()到另一個組件 – nachokk

+0

我照你說的做了,但現在自AbstractAction來自一個抽象方法配置被拒絕CatchQuery cos它返回一個字符串 - 檢查編輯2 –

0

物權法t將ActionListener添加到文本字段中:

//TextBox 
txtsearch = new JTextField(); 
txtsearch.addActionListener(new ActionListener() { 
    @Override 
    public void actionPerformed(ActionEvent e) { 
    // code when enter is pressed in text field 
    } 
}); 
+0

這不回答,我已經使用基於虛擬鍵的actionListener ...它只是在另一個類中實現 –

+0

然後我不明白你想要做什麼。至少我是這麼做的,它在我的代碼中工作... – Axel