我有一个带有几个元素的GUI,我只是在其中选中并指定我指定的最后一个元素(这是文本区域),如果我按Tab键,我希望它创建一个事件。
它完全没问题,因为我使用输入按钮编写了我的代码,但没有选项卡按钮,我找不到原因。
我的GUI窗口:
这是我的代码:
textArea_beschreibung.addKeyListener(new KeyAdapter() {
@Override
public void keyPressed(KeyEvent e) {
if (e.getKeyCode() == KeyEvent.VK_TAB){
String dauervar = input_dauer.getText (); //auslesen von benutzername
String beschreibungvar = textArea_beschreibung.getText (); //auslesen von benutzername
String projektvar = (String)comboBox_projekt.getSelectedItem();
String aktivitvar = (String)comboBox_aktivitaet.getSelectedItem();
String datumvar = input_datum.getText ();
SimpleDateFormat df1 = new SimpleDateFormat("dd/MM/yyyy");
Date date = null;
try {
date = df1.parse(datumvar);
} catch (ParseException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
long epoch = date.getTime();
String numberAsStringUnixTImestamp = String.valueOf(epoch / 1000);
String [ ] datenarray = {"a","b","c","d","e","f"};
datenarray[0] = numberAsStringUnixTImestamp;
datenarray[1] = dauervar;
datenarray[2] = beschreibungvar;
datenarray[3] = projektvar;
datenarray[4] = aktivitvar;
datenarray[5] = usernamevar;
String JsonArray = "{ \"" + "datum" + "\":\"" + datenarray[0] + "\", \"" + "dauer" + "\":\"" + datenarray[1] + "\", \"" + "beschreibung" + "\":\"" + datenarray[2] + "\", \"" + "projektname" + "\":\"" + datenarray[3] + "\", \"" + "kategorie" + "\":\"" + datenarray[4] + "\", \"" + "username" + "\":\"" + datenarray[5] + "\" }";
input_datum.setText(reportDate);
if(check_datum.isSelected()){
CustomDateFinal[0] = datumvar;
input_datum.setText(CustomDateFinal[0]);
}
input_dauer.setText(USERDefTimeJSON);
textArea_beschreibung.setText("");
comboBox_projekt.requestFocus();
try {
SimplePingPong.httpRequestVoid(JsonArray,"unused","werte");
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
}
});
答案 0 :(得分:1)
在我指定的最后一个元素(即文本区域)上,如果我按Tab键,我希望它创建一个事件。
Tab键由文本区域处理。默认操作是在文本区域中插入制表符。
要恢复默认标签逻辑,您可以使用以下任何一种方法:
/*
This is my understanding of how tabbing works. The focus manager
recognizes the following default KeyStrokes for tabbing:
forwards: TAB or Ctrl-TAB
backwards: Shift-TAB or Ctrl-Shift-TAB
In the case of JTextArea, TAB and Shift-TAB have been removed from
the defaults which means the KeyStroke is passed to the text area.
The TAB KeyStroke inserts a tab into the Document. Shift-TAB seems
to be ignored.
This example shows different approaches for tabbing out of a JTextArea
*/
import java.awt.*;
import java.util.*;
import java.awt.event.*;
import javax.swing.*;
public class TextAreaTab extends JFrame
{
public TextAreaTab()
{
Container contentPane = getContentPane();
contentPane.setLayout(new BoxLayout(contentPane, BoxLayout.Y_AXIS));
contentPane.add( nullTraversalKeys() );
contentPane.add( writeYourOwnAction() );
contentPane.add( useKeyListener() );
contentPane.add( addTraversalKeys() );
}
// Reset the text area to use the default tab keys.
// This is probably the best solution.
private JComponent nullTraversalKeys()
{
JTextArea textArea = new JTextArea(3, 30);
textArea.setText("Null Traversal Keys\n2\n3\n4\n5\n6\n7\n8\n9");
JScrollPane scrollPane = new JScrollPane( textArea );
textArea.setFocusTraversalKeys(KeyboardFocusManager.FORWARD_TRAVERSAL_KEYS, null);
textArea.setFocusTraversalKeys(KeyboardFocusManager.BACKWARD_TRAVERSAL_KEYS, null);
return scrollPane;
}
// Replace the Tab Actions. A little more complicated but this shows
// how to create a custom Action so you have full control.
private JComponent writeYourOwnAction()
{
JTextArea textArea = new JTextArea(3, 30);
textArea.setText("Write Your Own Tab Actions\n2\n3\n4\n5\n6\n7\n8\n9");
JScrollPane scrollPane = new JScrollPane( textArea );
InputMap im = textArea.getInputMap();
KeyStroke tab = KeyStroke.getKeyStroke("TAB");
textArea.getActionMap().put(im.get(tab), new TabAction(true));
KeyStroke shiftTab = KeyStroke.getKeyStroke("shift TAB");
im.put(shiftTab, shiftTab);
textArea.getActionMap().put(im.get(shiftTab), new TabAction(false));
return scrollPane;
}
// Use a KeyListener
// (not recommended, since Swing provides newer and better API's)
private JComponent useKeyListener()
{
JTextArea textArea = new JTextArea(3, 30);
textArea.setText("Use Key Listener\n2\n3\n4\n5\n6\n7\n8\n9");
JScrollPane scrollPane = new JScrollPane( textArea );
textArea.addKeyListener(new KeyAdapter()
{
public void keyPressed(KeyEvent e)
{
if (e.getKeyCode() == KeyEvent.VK_TAB)
{
e.consume();
KeyboardFocusManager.
getCurrentKeyboardFocusManager().focusNextComponent();
}
if (e.getKeyCode() == KeyEvent.VK_TAB
&& e.isShiftDown())
{
e.consume();
KeyboardFocusManager.
getCurrentKeyboardFocusManager().focusPreviousComponent();
}
}
});
return scrollPane;
}
// Add Tab and Shift-Tab KeyStrokes back as focus traversal keys.
// Seems more complicated then just using null, but at least
// it shows how to add a KeyStroke as a focus traversal key.
private JComponent addTraversalKeys()
{
JTextArea textArea = new JTextArea(3, 30);
textArea.setText("Add Traversal Keys\n2\n3\n4\n5\n6\n7\n8\n9");
JScrollPane scrollPane = new JScrollPane( textArea );
Set set = new HashSet( textArea.getFocusTraversalKeys(
KeyboardFocusManager.FORWARD_TRAVERSAL_KEYS ) );
set.add( KeyStroke.getKeyStroke( "TAB" ) );
textArea.setFocusTraversalKeys(
KeyboardFocusManager.FORWARD_TRAVERSAL_KEYS, set );
set = new HashSet( textArea.getFocusTraversalKeys(
KeyboardFocusManager.BACKWARD_TRAVERSAL_KEYS ) );
set.add( KeyStroke.getKeyStroke( "shift TAB" ) );
textArea.setFocusTraversalKeys(
KeyboardFocusManager.BACKWARD_TRAVERSAL_KEYS, set );
return scrollPane;
}
class TabAction extends AbstractAction
{
private boolean forward;
public TabAction(boolean forward)
{
this.forward = forward;
}
public void actionPerformed(ActionEvent e)
{
if (forward)
tabForward();
else
tabBackward();
}
private void tabForward()
{
KeyboardFocusManager manager = KeyboardFocusManager.getCurrentKeyboardFocusManager();
manager.focusNextComponent();
}
private void tabBackward()
{
KeyboardFocusManager manager = KeyboardFocusManager.getCurrentKeyboardFocusManager();
manager.focusPreviousComponent();
}
}
public static void main(String[] args)
{
try
{
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
}
catch (Exception e) { }
TextAreaTab frame = new TextAreaTab();
frame.setDefaultCloseOperation( EXIT_ON_CLOSE );
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
}
我确实在按Tab键时实际进行了所有处理。 Tab键的功能应该只是将焦点移动到下一个组件。
如果用户想要处理表单上的所有数据,那么您应该拥有某种"流程数据"用户可以单击的按钮。也许这个按钮就在文本区域之后。