0

好的,所以我是 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方法是首先运行的方法,它只是创建一个按钮,现在只是关闭当前帧并运行并运行该constructor方法,该方法只会增加每次按下按钮的点击次数。

4

0 回答 0