我正在努力建立一个“愚蠢的人”。电话,因此我需要建立一个字母数字键盘。因此,交易适用于每个按钮,如果他们在一定的时间限制(2秒)内反复按下他们输入的字符会发生变化。例如,按1输入A,但如果在2秒内再次按下,输入将更改为B,如果再次按下2秒,则输入为C,依此类推。
已经有一个与此完全相关的问题(Swing alphanumeric keypad)。我整天都在读它并试图理解,但我不能......
我想出了类似的东西(我知道它可能真的很傻但是我的大脑真的让我不能让它变得不那么傻),下面的代码就是这个动作听众的1按钮;
long buttn_last_pressed = System.currentTimeMillis();
line.setText("A");
if ((System.currentTimeMillis() - buttn_last_pressed) <= 2000) {
line1.setText("B"); }
问题在于输入总是出现在B.我真的卡住了,我无法理解这一点,因为你可以从愚蠢的代码中看到我来了任何帮助将非常感激, 非常感谢你提前! :)
答案 0 :(得分:1)
所以,如果我们看一下这个片段......
long buttn_last_pressed = System.currentTimeMillis();
line.setText("A");
if ((System.currentTimeMillis() - buttn_last_pressed) <= 2000) {
除非line.setText("A");
确实非常慢,否则不可能花费2秒钟来达到if
声明......
你的意思是更像......
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
public class JavaApplication787 {
public static void main(String[] args) {
new JavaApplication787();
}
public JavaApplication787() {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
ex.printStackTrace();
}
JFrame frame = new JFrame("Testing");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(new TestPane());
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}
public class TestPane extends JPanel {
private JTextField field;
public TestPane() {
setLayout(new GridBagLayout());
JButton btn = new JButton("1 ABC");
btn.addActionListener(new ActionListener() {
private long lastClick = -1;
private int count;
private String[] value = {"1", "A", "B", "C"};
@Override
public void actionPerformed(ActionEvent e) {
long now = System.currentTimeMillis();
if (now - lastClick < 2000) {
count++;
if (count >= value.length) {
count = 0;
}
} else {
count = 0;
}
field.setText(value[count]);
lastClick = System.currentTimeMillis();
}
});
field = new JTextField(4);
GridBagConstraints gbc = new GridBagConstraints();
gbc.gridwidth = GridBagConstraints.REMAINDER;
add(btn, gbc);
add(field, gbc);
}
}
}
好的,所以这一切都是检查最后一次点击的时间之间的差异,如果它小于2秒,它会更新count
,指示要使用的当前值,否则,它将count
重置为0
,默认为正常密钥......
btn.addActionListener(new ActionListener() {
private long lastClick = -1;
private int count;
private String[] value = {"1", "A", "B", "C"};
@Override
public void actionPerformed(ActionEvent e) {
long now = System.currentTimeMillis();
if (now - lastClick < 2000) {
count++;
if (count >= value.length) {
count = 0;
}
} else {
count = 0;
}
field.setText(value[count]);
lastClick = System.currentTimeMillis();
}
});