简体   繁体   中英

Can't set Java Swing GUI visibility to true

Not exactly sure what I've done wrong here, probably just missed something. This is the GUI Class

    package me.(removed).clicker.gui;

import java.awt.Container;
import java.awt.Font;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JSlider;

public class GUI
{
  public JFrame ClownClicker;
  public JSlider slider1;
  public JSlider slider2;
  public JLabel lblNewLabelXd;
  public JSlider slider;

  public GUI()
  {
    initialize();
  }

  private void initialize()
  {
    this.ClownClicker = new JFrame();
    this.ClownClicker.setResizable(false);
    this.ClownClicker.setTitle("Clown Clicker 0.3 BETA");
    this.ClownClicker.setFont(new Font("Arial", 0, 12));
    this.ClownClicker.setBounds(100, 100, 450, 120);
    this.ClownClicker.setDefaultCloseOperation(3);

    this.slider1 = new JSlider();
    this.slider1.setMaximum(500);
    this.slider1.setMinimum(75);
    this.slider1.setValue(165);
    this.ClownClicker.getContentPane().add(this.slider1, "Center");

    this.slider2 = new JSlider();
    this.slider2.setMinimum(25);
    this.slider2.setMaximum(200);
    this.slider2.setValue(75);
    this.ClownClicker.getContentPane().add(this.slider2, "West");

    this.lblNewLabelXd = new JLabel("New label\n xd");
    this.lblNewLabelXd.setFont(new Font("Arial", 0, 11));
    this.lblNewLabelXd.setHorizontalAlignment(0);
    this.ClownClicker.getContentPane().add(this.lblNewLabelXd, "North");

    this.slider = new JSlider();
    this.slider.setMaximum(1000);
    this.slider.setMinimum(1);
    this.slider.setValue(100);
    this.ClownClicker.getContentPane().add(this.slider, "South");

    JLabel label = new JLabel();

  }
}

This is the Main class

package me.(Removed).clicker.main;

import java.awt.AWTException;
import java.awt.EventQueue;
import java.awt.Point;
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.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JSlider;
import me.ciarandev.clicker.MouseListener;
import me.ciarandev.clicker.Timer;
import me.ciarandev.clicker.gui.GUI;
import org.jnativehook.GlobalScreen;
import org.jnativehook.NativeHookException;

public class Clowns
{
  public static Robot robot;
  public static Point mousePos;
  public static boolean toggled = false;
  public static boolean activated = false;
  public static boolean skipNext = false;
  public static boolean blockHit = false;
  private static int delay = -1;
  public static long lastTime = 0L;
  public static int minCPS = 8;
  public static int maxCPS = 12;
  public static String[] toggleKey = { ",", "," };
  public static int toggleMouseButton = 3;
  public static Timer time = new Timer();

  public static void main(String[] args)
  {
    LogManager.getLogManager().reset();
    Logger.getLogger(GlobalScreen.class.getPackage().getName()).setLevel(Level.OFF);
    GUI frame = new GUI();
    EventQueue.invokeLater(new Runnable()
    {
      public void run()
      {
        try
        {
            Clowns.this.ClownClicker.setVisible(true);
        }
        catch (Exception e)
        {
          e.printStackTrace();
        }
      }
    });
    try
    {
      robot = new Robot();
      GlobalScreen.registerNativeHook();
      GlobalScreen.addNativeMouseListener(new MouseListener());
    }
    catch (NativeHookException|AWTException e)
    {
      e.printStackTrace();
    }
    try
    {
      for (;;)
      {
        Thread.sleep(1L);
        minCPS = frame.slider2.getValue();
        maxCPS = frame.slider1.getValue();
        int minusMin = frame.slider.getValue() - frame.slider2.getValue();
        int plusMax = frame.slider.getValue() + frame.slider1.getValue();
        frame.lblNewLabelXd.setText("Min: " + minCPS + ", Max: " + maxCPS + ", Middleman: " + frame.slider.getValue() + ", CPS: " + MouseListener.cps + ", delay is from " + minusMin + " to " + plusMax);
        Random random = new Random();
        Random r = new Random();
        if (delay == -1) {
          delay = random.nextInt(1000 / minCPS - 1000 / maxCPS + 1) + 1000 / maxCPS;
        }
        if ((activated) && (toggled) && (!frame.frmNiggazClickerV.isFocused()) && 
          (time.hasTimePassed(frame.slider.getValue() + (random.nextInt(maxCPS - minCPS + 1) + minCPS))) && (random.nextBoolean()) && (r.nextBoolean()))
        {
          click();
          lastTime = System.currentTimeMillis();

          time.reset();
        }
      }
    }
    catch (InterruptedException e)
    {
      e.printStackTrace();
    }
  }

  private static void click()
  {
    skipNext = true;
    robot.mousePress(16);
    robot.mouseRelease(16);
    if (blockHit)
    {
      robot.mousePress(4);
      robot.mouseRelease(4);
    }
  }

  public static void toggle()
  {
    if (toggled) {
      toggled = false;
    } else {
      toggled = true;
    }
    activated = false;
    skipNext = false;
    blockHit = false;
  }
}

This is for an autoclicker I'm currently working on, I'm not sure what the issue is, it occurs at the line Clowns.this.ClownClicker.setVisible(true);

This is the GUI class, where the ClownClicker exists.

public class GUI
{
  public JFrame ClownClicker;

It is not part of Clowns.this , so instead use the GUI instance.

final GUI frame = new GUI();

EventQueue.invokeLater(new Runnable() {
  public void run() {
    try {
        frame.ClownClicker.setVisible(true);

Instead of:

ClownClicker.setVisible(true);

Try this.

frame.ClownClicker.setVisible(true);

ClownClicker declared, is not static variable in GUI class, so u can't do Clowns.this.ClownClicker.setVisible(true); the suitable solution is frame.ClownClicker.setVisible(true);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM