按时点击器JAVA代码错误

时间:2022-04-07 14:35:53

Not understand WHY SCHEDULE underline always and not working ( I tried separated into classes, same thing, just can't get where the problem is. @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@

不明白为什么SCHEDULE总是强调并且不工作(我尝试分成课程,同样的事情,只是无法解决问题所在。@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@

 import java.awt.*;
    import java.awt.event.*;
    import java.util.*;
    import javax.swing.*;


    public class assertion extends JFrame {

     JTextField curTime, xT, yT;
     Timer timer;

     public static void main(String[] args){ 

      new assertion();

     }


     public assertion() {

      JFrame window = new JFrame("ВИ Кликер");
      window.setSize(400,400);

      JPanel mainframe=new JPanel();
            window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);




            JLabel x=new JLabel();
            x.setText("X-Coordinate");
            mainframe.add(x);

            xT=new JTextField(10);
            mainframe.add(xT);
            window.getContentPane().add(mainframe);
            window.pack();
            window.setVisible(true);



            JLabel y=new JLabel();
            y.setText("Y-Coordinate");
            mainframe.add(y);

            yT=new JTextField(10);
            mainframe.add(yT);
            window.getContentPane().add(mainframe);
            window.pack();
            window.setVisible(true);



            JLabel time=new JLabel();
            time.setText("Time");
            mainframe.add(o);

            curTime = new JTextField(10);
            mainframe.add(curTime);
            window.getContentPane().add(mainframe);
            window.pack();
            window.setVisible(true);




            JButton gO =new JButton();
            gO.setText("GO");
            mainframe.add(gO);


            gO.addActionListener(new ActionListener(){
             public  void actionPerformed(ActionEvent e){
         timer();
             }
          });


     }


     public void timer(){


      Calendar calendar = Calendar.getInstance();
      calendar.set(Calendar.HOUR_OF_DAY, 23);
      calendar.set(Calendar.MINUTE, 1);
      calendar.set(Calendar.SECOND, 0);
      Date time = calendar.getTime();

      timer = new Timer();
      timer.schedule(new assertion(), time);

     }

     public void mouse() throws AWTException {

      String xx = xT.getText(), yy = yT.getText();
      int xxx = Integer.parseInt(xx), yyy = Integer.parseInt(yy);


      Robot robot = new Robot();
      robot.mouseMove(xxx, yyy);
      robot.mousePress(InputEvent.BUTTON1_MASK);
      robot.mouseRelease(InputEvent.BUTTON1_MASK);
     }


    }

2 个解决方案

#1


1  

The class Timer is ambiguous between java.util.Timer and javax.swing.Timer so timer.schedule cannot be resolved.

类java在java.util.Timer和javax.swing.Timer之间是不明确的,因此无法解析timer.schedule。

Remove the package import

删除包导入

import java.util.*;

and only import the classes you need specifically, for example

例如,只导入您需要的类

import java.util.Calendar;

javax.swing.Timer is the correct Timer to use with Swing so you will need to refactor your code to account for this. There's no need to create a new instance of assertion - just ensure that all the methods are invoked the Swing Timer ActionListener.

javax.swing.Timer是与Swing一起使用的正确Timer,因此您需要重构代码以解决此问题。没有必要创建一个新的断言实例 - 只需确保所有方法都被调用Swing Timer ActionListener。

Timer timer = new Timer(1000, new ActionListener() {

    @Override
    public void actionPerformed(ActionEvent e) {
        // perform timer actions 
    }
});

Aside: Java Naming conventions show that classes start with an uppercase letter, e.g. Assertion

旁白:Java命名约定显示类以大写字母开头,例如断言

#2


1  

Assertion does not extend TimerTask which is needed.

断言不会扩展所需的TimerTask。

Create an anonymous class like I showed you in the other question thread.

创建一个匿名类,就像我在另一个问题线程中向您展示的那样。

#1


1  

The class Timer is ambiguous between java.util.Timer and javax.swing.Timer so timer.schedule cannot be resolved.

类java在java.util.Timer和javax.swing.Timer之间是不明确的,因此无法解析timer.schedule。

Remove the package import

删除包导入

import java.util.*;

and only import the classes you need specifically, for example

例如,只导入您需要的类

import java.util.Calendar;

javax.swing.Timer is the correct Timer to use with Swing so you will need to refactor your code to account for this. There's no need to create a new instance of assertion - just ensure that all the methods are invoked the Swing Timer ActionListener.

javax.swing.Timer是与Swing一起使用的正确Timer,因此您需要重构代码以解决此问题。没有必要创建一个新的断言实例 - 只需确保所有方法都被调用Swing Timer ActionListener。

Timer timer = new Timer(1000, new ActionListener() {

    @Override
    public void actionPerformed(ActionEvent e) {
        // perform timer actions 
    }
});

Aside: Java Naming conventions show that classes start with an uppercase letter, e.g. Assertion

旁白:Java命名约定显示类以大写字母开头,例如断言

#2


1  

Assertion does not extend TimerTask which is needed.

断言不会扩展所需的TimerTask。

Create an anonymous class like I showed you in the other question thread.

创建一个匿名类,就像我在另一个问题线程中向您展示的那样。