swing 文本框判断是否输入完成

java swing 文本框判断是否输入完成

inputQRTextArea.addKeyListener(new KeyListener() {
		    private long lastTimeMillSencond;
		    @Override
		    public void keyTyped(KeyEvent e) {
		    }
		    @Override
		    public void keyReleased(KeyEvent e) {
		    }
		    @Override
		    public void keyPressed(KeyEvent e) {
                if (EventHWUtil.isJustShiftDown(e)) {//双击Shift
                    if (lastTimeMillSencond == 0) {
		                lastTimeMillSencond = System.currentTimeMillis();
		            } else {
		                long currentTime = System.currentTimeMillis();
		                if (MenuUtil2.isDoubleClick(currentTime - lastTimeMillSencond )) {
		                    genQRbutton.doClick(); 
		                    lastTimeMillSencond = 0;
		                } else {
		                    lastTimeMillSencond = System.currentTimeMillis();
		                }
		            }
                } else {//输入完成之后生成二维码
                    if (timer == null) {
                        timer = new Timer();
                    }
                    if (null != task) {
//                        System.out.println(222);
                        task.cancel();
                        task = null;
                    }

                    if (task == null) {
//                        System.out.println(111);
                        task = new TextBoxTask(QRCodePanel.this);
                        timer.schedule(task, INPUT_WAIT_SECOND * 1000);
                    }
                }
            }
		});

要执行的任务:

import com.swing.component.QRCodePanel;

/**
 * Created by 黄威 on 16/11/2016.<br >
 */
public class TextBoxTask extends java.util.TimerTask {
    private QRCodePanel qrCodePanel;

    public TextBoxTask(QRCodePanel qrCodePanel) {
        super();
        this.qrCodePanel = qrCodePanel;
    }

    @Override
    public void run() {
        //输入完成之后生成二维码
        System.out.println("$$$$$");
        this.qrCodePanel.generateQRAction(false);
    }
}

 参考我的另外一篇博客:http://hw1287789687.iteye.com/blog/2323017

 

 

好的,我可以为您提供一些代码示例,供您参考和学习: ```java import javax.swing.*; import java.awt.*; import java.awt.event.*; public class ScoreJudgement extends JFrame implements ActionListener { private JLabel scoreLabel, resultLabel; private JTextField scoreField, resultField; private JButton judgeButton, resetButton; public ScoreJudgement() { setTitle("成绩判断"); setSize(400, 200); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); JPanel panel = new JPanel(new GridLayout(3, 2)); scoreLabel = new JLabel("分数:"); panel.add(scoreLabel); scoreField = new JTextField(); panel.add(scoreField); resultLabel = new JLabel("结果:"); panel.add(resultLabel); resultField = new JTextField(); resultField.setEditable(false); panel.add(resultField); judgeButton = new JButton("判断"); judgeButton.addActionListener(this); panel.add(judgeButton); resetButton = new JButton("重置"); resetButton.addActionListener(this); panel.add(resetButton); setContentPane(panel); setVisible(true); } @Override public void actionPerformed(ActionEvent e) { if (e.getSource() == judgeButton) { try { int score = Integer.parseInt(scoreField.getText()); if (score >= 60) { resultField.setText("及格"); } else { resultField.setText("不及格"); } } catch (NumberFormatException ex) { JOptionPane.showMessageDialog(this, "请输入有效的数字!", "错误", JOptionPane.ERROR_MESSAGE); } } else if (e.getSource() == resetButton) { scoreField.setText(""); resultField.setText(""); scoreField.requestFocus(); } } public static void main(String[] args) { new ScoreJudgement(); } } ``` 这是一个简单的 Swing 程序,包括一个窗口、几个标签、几个文本框和几个按钮。其,`ScoreJudgement` 是程序的主类,继承自 `JFrame` 类,并实现了 `ActionListener` 接口。 在程序的构造函数,我们创建了一个 `JPanel` 对象,并将其设置为窗口的内容面板。然后,我们依次创建了标签、文本框和按钮,并将它们添加到面板。 在 `actionPerformed` 方法,我们为判断按钮和重置按钮分别添加了事件处理逻辑。当用户单击判断按钮时,我们首先尝试将分数文本框的文本转换为整数,如果转换失败,则弹出一个错误对话框;如果转换成功,则判断分数是否大于或等于 60 分,并将判断结果显示在结果文本框。当用户单击重置按钮时,我们清空两个文本框内容,并将焦点设回分数文本框。 最后,在 `main` 方法,我们创建了一个 `ScoreJudgement` 对象,以启动程序。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值