微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

为什么JTextField操作侦听器不起作用

如何解决为什么JTextField操作侦听器不起作用

好吧,所以我是Java编程的新手,我想制作一个简单的JAVA GUI应用程序,您首先输入一个秒数,然后在该秒数内尽可能多地单击一个按钮(例如单击速度测试概念)。距离完成还很远,现在我正在实施,以便可以为文本字段添加一个占位符以向用户解释,但是焦点动作侦听器无法正常工作。


import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.FocusEvent;
import java.awt.event.FocusListener;
import java.time.LocalTime;
import java.util.*;

public class Main implements ActionListener,FocusListener {

    private JPanel panel;
    private JPanel initPanel;
    private JFrame frame;
    private JButton button;
    private JButton newButton;
    private JLabel label;
    private JLabel newLabel;
    private JTextField textInput;
    private LocalTime time;
    private ImageIcon img;
    private int count = 0;
    private Scanner input = new Scanner(system.in);

    public static void constructor() {

        Main app = new Main();

        app.panel = new JPanel();
        app.frame = new JFrame();
        app.label = new JLabel("Number of clicks: 0");
        app.img = new ImageIcon("E:\\Descarcari Chrome\\mouse.png");
        app.time = LocalTime.Now();

        app.panel.setBorder(BorderFactory.createEmptyBorder(20,40,10,40));
        app.panel.setLayout(new GridLayout(0,1));

        app.button = new JButton("Click me!");
        app.button.setFocusPainted(false);
        app.button.setFont(new Font("Arial",Font.BOLD,40));
        app.button.addActionListener(app);

        app.panel.add(app.button);
        app.panel.add(app.label);

        app.frame.add(app.panel,BorderLayout.CENTER);
        app.frame.setDefaultCloSEOperation(JFrame.EXIT_ON_CLOSE);
        app.frame.setTitle("Click Speed Test");
        app.frame.setIconImage(app.img.getimage());
        app.frame.pack();
        app.frame.setVisible(true);
        app.frame.setResizable(false);
    }

    public static void chooseTime() {
        Main app = new Main();

        app.initPanel = new JPanel();
        app.newButton = new JButton("Submit!");
        app.textInput = new JTextField("Number of seconds for the test");
        app.frame = new JFrame();


        app.newButton.setFocusPainted(false);
        app.newButton.setFont(new Font("Arial",40));
        app.newButton.addActionListener(app);

        app.textInput.setForeground(Color.GRAY);

        app.initPanel.setBorder(BorderFactory.createEmptyBorder(20,40));
        app.initPanel.setLayout(new GridLayout(0,1));
        app.initPanel.add(app.newButton);
        app.initPanel.add(app.textInput);
        app.textInput.addActionListener(app);

        app.frame.add(app.initPanel,BorderLayout.CENTER);
        app.frame.setDefaultCloSEOperation(JFrame.EXIT_ON_CLOSE);
        app.frame.setTitle("Click Speed Test");
        app.frame.pack();
        app.frame.setVisible(true);
        app.frame.setResizable(false);

    }

    public static void main(String[] args) {
        chooseTime();
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        if (e.getSource() == button) {
            count++;
            label.setText("Number of clicks: " + count);
            button.setText("");
        } else if (textInput.getText().isEmpty() == false){
            frame.getContentPane().removeAll();
            frame.repaint();
            frame.setVisible(false);
            constructor();
        }else {
            JLabel wrong = new JLabel("Input cannot be null");
            initPanel.add(wrong);
            chooseTime();
        }
    }

    @Override
    public void focusGained(FocusEvent e) {
        System.out.println("FOCUS");
        if(textInput.getText().equals("Number of seconds for the test")) {
            textInput.setText("");
            textInput.setForeground(Color.BLACK);
        }
    }

    @Override
    public void focusLost(FocusEvent e) {
        System.out.println("LOST FOCUS");
        if(textInput.getText().isEmpty()) {
            textInput.setForeground(Color.GRAY);
            textInput.setText("Number of seconds for the test");
        }
    }
}

让我解释一下我在做什么: chooseTime方法是将首先运行的方法,它仅创建一个按钮,该按钮现在仅关闭当前帧并运行并运行constructor方法,该方法增加每次按钮按下的点击次数。 / p>

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。