Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/364.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 为什么JTextField操作侦听器不工作_Java_Swing_User Interface_Actionlistener_Textlabel - Fatal编程技术网

Java 为什么JTextField操作侦听器不工作

Java 为什么JTextField操作侦听器不工作,java,swing,user-interface,actionlistener,textlabel,Java,Swing,User Interface,Actionlistener,Textlabel,好的,我是Java编程新手,我想制作一个简单的Java GUI应用程序,首先输入秒数,然后在该秒数内尽可能多地单击按钮(如单击速度测试概念)。它还远未完成,现在我正在实现,以便可以为文本字段添加占位符来向用户解释,但焦点操作侦听器不起作用 import javax.swing.*; import java.awt.*; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt

好的,我是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", Font.BOLD, 40));
        app.newButton.addActionListener(app);

        app.textInput.setForeground(Color.GRAY);

        app.initPanel.setBorder(BorderFactory.createEmptyBorder(20, 40, 10, 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
方法是将首先运行的方法,它只创建一个按钮,现在只关闭当前帧并运行
构造函数
方法,该方法只增加每次按下按钮的点击次数。

代码的整个结构是错误的。您不应该使用静态方法,也不应该使用类似“app.newButton…”这样的代码。如果您的代码结构正确,那么该变量将定义为类中的实例变量,然后您只需使用“newButton…”引用它们。。。“。因此,我建议您阅读文本字段中添加ActionLIstener的工作代码。下载工作演示代码,并根据您的要求一次修改一个组件。好的,谢谢您的帮助!!!;)
addFocusListener
不会出现在问题的java代码中。也许这就是它不起作用的原因?当然,它在中更简单,但在中仍然可行