我将如何显示我的UI

时间:2017-06-05 20:07:13

标签: java swing user-interface

我试图通过从我的clicker类调用我的主类的方法来显示我的UI。 主要课程

package me.removed.clicker;

import me.removed.clicker.Clicker;

public class Main
{
      public static void main(String[] args)
      {
        Clicker.getClicker().openUI();
      }
    }

Clicker类

package me.removed.clicker;

import java.awt.EventQueue;
import java.awt.Robot;
import java.util.Random;
import java.util.logging.Level;
import java.util.logging.LogManager;
import java.util.logging.Logger;
import javax.sound.sampled.AudioInputStream;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.Clip;
import javax.sound.sampled.FloatControl;
import javax.sound.sampled.FloatControl.Type;
import javax.swing.UIManager;
import javax.swing.UIManager.LookAndFeelInfo;
import me.removed.clicker.handler.KeyboardHandler;
import me.removed.clicker.handler.MouseHandler;
import me.removed.clicker.util.Time;
import org.jnativehook.GlobalScreen;

public class Clicker
{
  public static Clicker instance;
  private boolean LEFT_held;
  private boolean LEFT_skip;
  private Time LEFT_pressTime;
  private boolean toggled;
  private Random random;
  private ClickerFrame clickerFrame;
  private MouseHandler clickerHandler;
  private KeyboardHandler keyHandler;
  private Robot robot;
  private Thread thread;

  public boolean isToggled()
  {
    return this.toggled;
  }

  public void setToggled(boolean toggled)
  {
    this.toggled = toggled;
  }

  public Clicker()
  {
    this.LEFT_held = false;
    this.LEFT_skip = false;
    this.toggled = false;
    this.random = new Random();
    try
    {
      GlobalScreen.registerNativeHook();
      this.clickerHandler = new MouseHandler();
      this.keyHandler = new KeyboardHandler();
      GlobalScreen.addNativeMouseListener(this.clickerHandler);
      GlobalScreen.addNativeKeyListener(this.keyHandler);
      this.robot = new Robot();
      LogManager.getLogManager().reset();
      Logger logger = Logger.getLogger(GlobalScreen.class.getPackage().getName());
      logger.setLevel(Level.OFF);
    }
    catch (Exception e)
    {
      e.printStackTrace();
    }
    this.thread = new Thread(new Runnable()
    {
      public void run()
      {
        try
        {
          for (;;)
          {
            Thread.sleep(1L);
            if ((Clicker.this.isToggled()) && 


              (Clicker.this.isLeftHeld()))
            {
              long CPSDelay = Clicker.this.generateCPSDelay();
              if (Clicker.this.getLeftPressTime() != null)
              {
                if (Clicker.this.getLeftPressTime().hasPassed())
                {
                  Clicker.this.setLeftPressTime(new Time(CPSDelay));
                  Clicker.this.setLeftSkip(true);
                  Clicker.this.robot.mousePress(16);
                  Clicker.this.robot.mouseRelease(16);

                  Thread runThread = new Thread(new Runnable()
                  {
                    public void run()
                    {
                      try
                      {
                        AudioInputStream audioInputStream = 
                          AudioSystem.getAudioInputStream(Clicker.class.getClassLoader().getResource("left_click.wav"));

                        Clip clip = AudioSystem.getClip();

                        clip.open(audioInputStream);

                        float volume = -9.0F - Clicker.this.random.nextFloat() * 11.0F;


                        FloatControl gainControl = 
                          (FloatControl)clip.getControl(FloatControl.Type.MASTER_GAIN);
                        gainControl.setValue(volume);

                        clip.start();
                      }
                      catch (Exception localException) {}
                    }
                  });
                  runThread.start();
                }
              }
              else
              {
                Clicker.this.setLeftPressTime(new Time(CPSDelay));
                Clicker.this.setLeftSkip(true);
              }
            }
          }
        }
        catch (Exception e)
        {
          e.printStackTrace();
        }
      }
    });
    this.thread.start();
  }

  public boolean isLeftHeld()
  {
    return this.LEFT_held;
  }

  public void setLeftHeld(boolean LEFT_held)
  {
    this.LEFT_held = LEFT_held;
  }

  public void destroy()
  {
    try
    {

    }
    catch (Throwable e)
    {
      e.printStackTrace();
    }
    System.exit(0);
  }

  public void openUI()
  {
    EventQueue.invokeLater(new Runnable()
    {
      public void run()
      {
        try
        {
          UIManager.LookAndFeelInfo[] installedLookAndFeels;
          int length = (installedLookAndFeels = UIManager.getInstalledLookAndFeels()).length;
          for (int i = 0; i < length; i++)
          {
            UIManager.LookAndFeelInfo info = installedLookAndFeels[i];
            if ("Windows".equals(info.getName()))
            {
              UIManager.setLookAndFeel(info.getClassName());
              break;
            }
          }
          Clicker.this.clickerFrame = new ClickerFrame();
          Clicker.this.clickerFrame.setVisible(true);
        }
        catch (Exception e)
        {
          e.printStackTrace();
        }
      }
    });
  }

  public boolean getLeftSkip()
  {
    return this.LEFT_skip;
  }

  public void setLeftSkip(boolean LEFT_skip)
  {
    this.LEFT_skip = LEFT_skip;
  }

  public Time getLeftPressTime()
  {
    return this.LEFT_pressTime;
  }

  public void setLeftPressTime(Time LEFT_pressTime)
  {
    this.LEFT_pressTime = LEFT_pressTime;
  }

  public ClickerFrame getClickerFrame()
  {
    return this.clickerFrame;
  }

  public long generateCPSDelay()
  {
    int minCPS = 12;
    int maxCPS = 15;
    double minDiff = 83.0D;
    double maxDiff = 66.0D;
    double randomDiff = this.random.nextDouble() * 17.0D;
    return (long) (66.0D + randomDiff);
  }

  public static void setupClicker()
  {
    (Clicker.instance = new Clicker()).openUI();
  }

  public static Clicker getClicker()
  {
    return instance;
  }
}

你可以在我的主课程中看到我试图调用OpenUI但是即使我这样做没有任何反应,过程运行但没有UI打开,我尝试删除Void也没有用,如其他帖子所示。

控制台错误:

Jun 06, 2017 3:43:54 AM org.jnativehook.DefaultLibraryLocator getLibraries
INFO: Library extracted successfully: C:\Users\WarDev\AppData\Local\Temp\JNativeHook-2.0.3.dll (0x7432773EB4D09DC286D43FCC77DDB0E1E3BCE2B4).

Jun 06, 2017 3:43:54 AM java.lang.ClassLoader$NativeLibrary load
INFO: hook_get_auto_repeat_rate [42]: SPI_GETKEYBOARDSPEED: 31.

Jun 06, 2017 3:43:54 AM java.lang.ClassLoader$NativeLibrary load
INFO: hook_get_auto_repeat_delay [56]: SPI_GETKEYBOARDDELAY: 1.

Jun 06, 2017 3:43:54 AM java.lang.ClassLoader$NativeLibrary load
INFO: hook_get_pointer_acceleration_multiplier [70]: SPI_GETMOUSE[2]: 1.

Jun 06, 2017 3:43:54 AM java.lang.ClassLoader$NativeLibrary load
INFO: hook_get_pointer_acceleration_threshold [84]: SPI_GETMOUSE[0]: 6.

Jun 06, 2017 3:43:54 AM java.lang.ClassLoader$NativeLibrary load
INFO: hook_get_pointer_acceleration_threshold [86]: SPI_GETMOUSE[1]: 10.

Jun 06, 2017 3:43:55 AM java.lang.ClassLoader$NativeLibrary load
INFO: hook_get_pointer_sensitivity [101]: SPI_GETMOUSESPEED: 10.

Jun 06, 2017 3:43:55 AM java.lang.ClassLoader$NativeLibrary load
INFO: hook_get_multi_click_time [115]: GetDoubleClickTime: 500.

java.lang.NullPointerException
    at javax.swing.ImageIcon.<init>(Unknown Source)
    at me.dewgs.clicker.ClickerFrame.<init>(ClickerFrame.java:442)
    at me.dewgs.clicker.Clicker$2.run(Clicker.java:175)
    at java.awt.event.InvocationEvent.dispatch(Unknown Source)
    at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
    at java.awt.EventQueue.access$500(Unknown Source)
    at java.awt.EventQueue$3.run(Unknown Source)
    at java.awt.EventQueue$3.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source)
    at java.awt.EventQueue.dispatchEvent(Unknown Source)
    at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
    at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
    at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
    at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
    at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
    at java.awt.EventDispatchThread.run(Unknown Source)

1 个答案:

答案 0 :(得分:0)

public static Clicker instance;

在我看来,实例是null。

public static void setupClicker()
  {
    (Clicker.instance = new Clicker()).openUI();
  }

您在哪里调用setupClicker()来初始化变量?

当您使用getClicker()方法显示用户界面时,为什么要调用setupClicker()