- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我已经完成了一个带有两个具有自动完成功能的 JTextField 的应用程序应用程序工作正常,但是当鼠标指针位于第一个文本字段上时单击选项卡按钮时,鼠标焦点不会进入第二个文本字段,而是在第二次获得焦点。即为了传递焦点,我们按下 Tab 按钮两次
谁能告诉我这个问题的原因和解决方案
我的代码如下
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.event.*;
import java.text.Collator;
import java.util.*;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JComboBox;
import javax.swing.JTextField;
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;
import javax.swing.text.AttributeSet;
import javax.swing.text.BadLocationException;
import javax.swing.text.PlainDocument;
public class AutoCompleteExample extends javax.swing.JFrame {
public AutoCompleteExample() {
initComponents();
List<Country> countries = new ArrayList<Country>();
Locale[] locales = Locale.getAvailableLocales();
for (Locale locale : locales) {
String iso = locale.getISO3Country();
String code = locale.getCountry();
String name = locale.getDisplayCountry();
if (!"".equals(iso) && !"".equals(code) && !"".equals(name)) {
countries.add(new Country(iso, code, name));
}
}
Collections.sort(countries, new CountryComparator());
java.util.Set<String> item = new java.util.TreeSet<String>();
for(Country cn:countries){
item.add(""+cn);
}
ArrayList<String> items = new ArrayList<String>(item);
setupAutoComplete(country,code, items);
country.setColumns(30);
}
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
jDesktopPane1 = new javax.swing.JDesktopPane();
jLabel1 = new javax.swing.JLabel();
country = new javax.swing.JTextField();
jLabel2 = new javax.swing.JLabel();
code = new javax.swing.JTextField();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
jDesktopPane1.setBackground(javax.swing.UIManager.getDefaults().getColor("Button.background"));
jLabel1.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
jLabel1.setText("Country Name");
jLabel1.setBounds(0, 10, 80, 20);
jDesktopPane1.add(jLabel1, javax.swing.JLayeredPane.DEFAULT_LAYER);
country.addFocusListener(new java.awt.event.FocusAdapter() {
public void focusLost(java.awt.event.FocusEvent evt) {
countryFocusLost(evt);
}
});
country.addKeyListener(new java.awt.event.KeyAdapter() {
public void keyReleased(java.awt.event.KeyEvent evt) {
countryKeyReleased(evt);
}
});
country.setBounds(90, 10, 140, 20);
jDesktopPane1.add(country, javax.swing.JLayeredPane.DEFAULT_LAYER);
jLabel2.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
jLabel2.setText("Code");
jLabel2.setBounds(235, 10, 50, 20);
jDesktopPane1.add(jLabel2, javax.swing.JLayeredPane.DEFAULT_LAYER);
code.setBounds(300, 10, 160, 20);
jDesktopPane1.add(code, javax.swing.JLayeredPane.DEFAULT_LAYER);
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jDesktopPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 473, Short.MAX_VALUE)
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jDesktopPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 43, Short.MAX_VALUE)
);
pack();
}// </editor-fold>
private void countryFocusLost(java.awt.event.FocusEvent evt) {
// TODO add your handling code here:
}
private void countryKeyReleased(java.awt.event.KeyEvent evt) {
}
public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new AutoCompleteExample().setVisible(true);
}
});
}
// Variables declaration - do not modify
public static javax.swing.JTextField code;
public static javax.swing.JTextField country;
private javax.swing.JDesktopPane jDesktopPane1;
private javax.swing.JLabel jLabel1;
private javax.swing.JLabel jLabel2;
// End of variables declaration
private static boolean isAdjusting(JComboBox cbInput) {
if (cbInput.getClientProperty("is_adjusting") instanceof Boolean) {
return (Boolean) cbInput.getClientProperty("is_adjusting");
}
return false;
}
private static void setAdjusting(JComboBox cbInput, boolean adjusting) {
cbInput.putClientProperty("is_adjusting", adjusting);
}
public static void setupAutoComplete(final JTextField txtInput,final JTextField txtInput2, final ArrayList<String> items) {
final DefaultComboBoxModel model = new DefaultComboBoxModel();
final JComboBox cbInput = new JComboBox(model) {
public Dimension getPreferredSize() {
return new Dimension(super.getPreferredSize().width, 0);
}
};
setAdjusting(cbInput, false);
for (Object item : items) {
model.addElement(item);
}
cbInput.setSelectedItem(null);
cbInput.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
if (!isAdjusting(cbInput)) {
if (cbInput.getSelectedItem() != null) {
txtInput.setText(cbInput.getSelectedItem().toString());
txtInput2.setText(datas.get(cbInput.getSelectedItem().toString())+"");
}
}
}
});
txtInput.addKeyListener(new KeyAdapter() {
@Override
public void keyPressed(KeyEvent e) {
setAdjusting(cbInput, true);
if (e.getKeyCode() == KeyEvent.VK_SPACE) {
if (cbInput.isPopupVisible()) {
e.setKeyCode(KeyEvent.VK_ENTER);
}
}
if (e.getKeyCode() == KeyEvent.VK_ENTER || e.getKeyCode() == KeyEvent.VK_UP || e.getKeyCode() == KeyEvent.VK_DOWN) {
e.setSource(cbInput);
cbInput.dispatchEvent(e);
if (e.getKeyCode() == KeyEvent.VK_ENTER) {
txtInput.setText(cbInput.getSelectedItem().toString());
// System.out.println("SEEEEE:"+datas.get(cbInput.getSelectedItem().toString()));
txtInput2.setText(datas.get(cbInput.getSelectedItem().toString())+"");
cbInput.setPopupVisible(false);
}
}
if (e.getKeyCode() == KeyEvent.VK_ESCAPE) {
cbInput.setPopupVisible(false);
}
setAdjusting(cbInput, false);
}
@Override
public void keyReleased(KeyEvent e)
{
setAdjusting(cbInput, true);
if (e.getKeyCode() == KeyEvent.VK_SPACE) {
if (cbInput.isPopupVisible()) {
e.setKeyCode(KeyEvent.VK_ENTER);
}
}
if (e.getKeyCode() == KeyEvent.VK_ENTER || e.getKeyCode() == KeyEvent.VK_UP || e.getKeyCode() == KeyEvent.VK_DOWN) {
e.setSource(cbInput);
cbInput.dispatchEvent(e);
if (e.getKeyCode() == KeyEvent.VK_ENTER) {
txtInput.setText(cbInput.getSelectedItem().toString());
// System.out.println("SEEEEE:"+datas.get(cbInput.getSelectedItem().toString()));
txtInput2.setText(datas.get(cbInput.getSelectedItem().toString())+"");
cbInput.setPopupVisible(false);
}
}
if (e.getKeyCode() == KeyEvent.VK_ESCAPE) {
cbInput.setPopupVisible(false);
}
setAdjusting(cbInput, false);
}
});
txtInput.addFocusListener(new FocusListener() {
public void focusGained(FocusEvent e) {
}
@Override
public void focusLost(FocusEvent e) {
try{ setAdjusting(cbInput, true);
e.setSource(cbInput);
cbInput.dispatchEvent(e);
txtInput.setText(cbInput.getSelectedItem().toString());
// System.out.println("SEEEEE:"+datas.get(cbInput.getSelectedItem().toString()));
txtInput2.setText(datas.get(cbInput.getSelectedItem().toString())+"");
cbInput.setPopupVisible(false);
setAdjusting(cbInput, false);
}catch(Exception s){country.setText("");code.setText("");
}
}
});
txtInput.getDocument().addDocumentListener(new DocumentListener() {
public void insertUpdate(DocumentEvent e) {
updateList();
}
public void removeUpdate(DocumentEvent e) {
updateList();
}
public void changedUpdate(DocumentEvent e) {
updateList();
}
private void updateList() {
setAdjusting(cbInput, true);
model.removeAllElements();
String input = txtInput.getText();
if (!input.isEmpty()) {
for (Object item : items) {
if ((item+"").toLowerCase().startsWith(input.toLowerCase())) {
model.addElement(item);
}
}
}
cbInput.setPopupVisible(model.getSize() > 0);
setAdjusting(cbInput, false);
}
});
txtInput.setLayout(new BorderLayout());
txtInput.add(cbInput, BorderLayout.SOUTH);
}
public static HashMap datas=new HashMap();;
}
////////////////////////////////////////////////////class JTextFieldLimits for limiting texfield strength/////////////////////////////////////////
class JTextFieldLimits extends PlainDocument {
private int limit;
JTextFieldLimits(int limit) {
super();
this.limit = limit;
}
@Override
public void insertString( int offset, String str, AttributeSet attr ) throws BadLocationException {
if (str == null) return;
if ((getLength() + str.length()) <= limit) {
super.insertString(offset, str, attr);
}
}
}
////////////////////////////////////////////////////class Country for getting names/////////////////////////////////////////
class Country {
private String iso;
private String code;
public String name;
Country(String iso, String code, String name) {
this.iso = iso;
this.code = code;
this.name = name;
AutoCompleteExample.datas.put(name, code);
}
@Override
public String toString() {
return name;
}
public String getName() {
return code;
}
}
class CountryComparator implements Comparator<Country> {
private Comparator comparator;
CountryComparator() {
comparator = Collator.getInstance();
}
@Override
public int compare(Country o1, Country o2) {
return comparator.compare(o1.name, o2.name);
}
}
提前致谢。
最佳答案
为什么要重新发明轮子,而是使用
关于java - JTextField 焦点未集中在第一次单击 Tab 按钮上,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13931081/
我在看 Tabs UI 的选项。默认行为是它们水平堆叠。有没有办法改变它? 我想堆叠Tabs垂直。它将节省大量的 UI“不动产”,尤其是在移动应用程序 UI 中。 最佳答案 尝试这个: Tabs ta
问题:在 Zsh 中使用 TAB 向后导航,类似于在 Firefox 中 Shift-TAB Shift-TAB 应该执行的操作示例 我在终端中运行以下代码 ls 我明白了 A B C D E F
有很多人想知道如何完成制表符。这不是这些问题之一。问题是如何将 tab 键分配给 tab 补全? alt-tab/esc-tab 很痛苦。在面板禁用模式选项卡完成工作,这让我暂时...但我仍然希望面板
所以我有一个带有 3 个屏幕的 TabNavigator。 import React from 'react'; import {TabNavigator,createBottomTabNavigat
我想触发一个事件,例如当我在文本框中按 Tab 键时显示警报消息。 $("input").blur(function (e) { if (e.which == 9) alert(
我将这段代码设置为创建一个选项卡式菜单,它显示为一个选项卡式菜单,但当我加载网站时,选项卡在单击时不会改变。在“检查元素”中它说 $( "#tabs").tabs();不是函数。我不知道下一步要解决这
表单中有多个输入字段的示例,因为有一些字段在此之前会自动填充,如果我按 Tab 键,那么它应该跳过该自动填充的输入字段并应转到空白输入字段 我尝试过,但是当我开始在空白输入中写入时会发生什么,它也会自
我想写一个小的 chrome 扩展程序,它应该从网页 A(当前网页)获取信息,将选项卡更新到网页 B,然后将代码注入(inject)网页 B。不幸的是,以下代码正在将网页更新到 B 但注入(injec
如果当前事件选项卡中的表单很脏,我试图阻止 mat-tab 的选项卡更改。 但是我找不到拦截选项卡更改事件的方法。 // Tab 0 Content // Tab
我已从 MacOS Mojave 上的默认终端切换到 iterm2 .我有一个关于从当前选项卡打开新选项卡的问题。 确实,我希望与上一个当前选项卡处于同一路径。 为此,我执行了经典程序,即转到 ite
我在我的网站的两页上有此代码,但在一页上该功能不起作用。 Firebug 向我显示“$(...).tabs 不是函数”。我不明白为什么,谁能告诉我什么是错的? 这是有效的: http://www.in
我需要可靠的方法来为 Windows 和 XP 创建 %tab% (包含一个制表符)。 SET TAB=" " 应该适用于 Windows 7(未测试)但不适用于 Win XP(已测试)。 这个 fo
我正在尝试使用 RMarkdown 制作静态网页。我想定义一个 UI,它有第一层选项卡,然后是第一层下面的选项卡。我已经在 RMarkdown: Tabbed and Untabbed heading
我正在尝试使用 RMarkdown 制作静态网页。我想定义一个 UI,它有第一层选项卡,然后是第一层下面的选项卡。我已经在 RMarkdown: Tabbed and Untabbed heading
我在使用 chrome.tabs.create 方法打开多个选项卡时遇到问题。我正在尝试使用 chrome.tabs.create 循环打开大约 9 个选项卡,但打开的选项卡数量仅限于 4 个。看起来
我正在使用 Material ui 的标签,并且能够对标签的指示器进行更改。但是,我正在尝试使用样式将每个选项卡的指示器宽度减小到某个固定宽度。但似乎指标以一些计算值定位在左侧,并且给它一个宽度不会使
我在 OS X 终端中使用 emacs 24.3,并且遇到了一些奇怪的事情。 在 markdown-mode.el 中,tab 键通过 (define-key map (kbd "") 'markdo
在 Chrome 开发者工具上 Uncaught (in promise) Error: There is no clipping info for given tab at E._handleRes
在vim中,我想将:tabnew缩短为:tn,将:tabp缩短为:th,将:tabn缩短为:tl在我的.vimrc中。知道我将如何重新映射这样的命令吗? 最佳答案 使用cabbrev: ca tn t
我读过几个主题,讨论 IE 中的地址栏在使用 TAB 时基本上是第一个获得焦点的主题(MSDN 自己的文档讨论了这一点)。 然而,我也见过一些情况,但情况并不总是如此...... 我有一个母版页,内容
我是一名优秀的程序员,十分优秀!