I am trying to create a autoclicker but i encountered a random problem.
I have make the "Exit" button to spawn on the bottom right by using .getContentPane().getSize()
but on my "Basic" Jframe that value some times changes (i have added a system print when it opens.
The first value is java.awt.Dimension[width=284,height=462]
and the second is java.awt.Dimension[width=294,height=472] (this is the correct one)
Can someone explain why this happens because i got my mind blown.
I am using NetBeans.
Here is not my full code:
Main
/*
* To change this license header, choose License Headers in Project
Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package test;
import java.awt.AWTException;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JPanel;
/**
*
* @author Xaralabos
*/
public class Test {
/**
* @param args the command line arguments
*/
static void placeAtThe (JComponent a, String where, Dimension framesize){
if((where == "bottom")||(where == "Bottom")||(where == "bot")||(where == "Bot")){
a.setLocation(a.getX(),(framesize.height - a.getHeight()));
}
else if ((where == "right")||(where == "Right")){
a.setLocation(framesize.width - a.getWidth(), a.getY());
}
}
// Center Align - Width (needs width of frame and of component)
static int CW (int framew, int compw){
int c = (((framew) / 2) - (compw / 2));
return c;
}
// CREATE NEW FRAME FOR Basic Clicker
static class BasicClickerFrame implements ActionListener{
private JFrame Homef;
private boolean StopCopies;
public BasicClickerFrame(JFrame Homef){
this.Homef = Homef;
StopCopies = false;
}
public void actionPerformed(ActionEvent e) {
Homef.dispose();
if (StopCopies == false){
StopCopies = true;
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
double width = screenSize.getWidth();
double height = screenSize.getHeight();
int basicw = 300;
int basich = 500;
// FRAME , SIZE - SPAWN LOCATION
JFrame Basic = new JFrame("Basic Auto Clicker");
Basic.setResizable(false);
Basic.setSize(basicw, basich);
Basic.setLocation(((int) (width / 4)) - (Basic.getWidth() / 2), ((int) height / 10));
Basic.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// COLORING THE BACKGROUND
JPanel contentPane = new JPanel();
contentPane.setBackground(Color.WHITE);
contentPane.setLayout(null);
// add more code here
Basic.setContentPane(contentPane);
// EXIT BUTTON (CLOSES WINDOW, NOT PROGRAM)
JButton Exit = new JButton("Exit");
Exit.setSize(Exit.getPreferredSize());
// add more code here
contentPane.add(Exit);
// button for getting keybind for starting
JButton Getme = new JButton("Click Me");
Getme.setSize(Getme.getPreferredSize());
// add more code here
contentPane.add(Getme);
// SPAWN EVERYTHING
Basic.setVisible(true);
// ADD LOCATIONS HERE
placeAtThe(Exit, "right", Basic.getContentPane().getSize());
placeAtThe(Exit, "bot", Basic.getContentPane().getSize());
Getme.setLocation(CW(Basic.getContentPane().getSize().width, Getme.getWidth()), 50);
System.out.println(Basic.getContentPane().getSize());
}
}
}
public static void main(String[] args) throws IOException, AWTException {
int frame1w = 600;
int frame1h = 400;
// MAIN WINDOW - SIZE - SPAWN LOCATION
JFrame Homef = new JFrame("Auto Clicker");
Homef.setResizable(false); // stops resizing
Homef.setSize(frame1w, frame1h);
Homef.setLocationRelativeTo(null);
Homef.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// COLORING BACKGROUND
JPanel contentPane = new JPanel();
contentPane.setBackground(Color.WHITE);
contentPane.setLayout(null);
// Add more code here
Homef.setContentPane(contentPane);
// EXIT BUTTON (CLOSES PROGRAM)
JButton Exit = new JButton("Exit");
Exit.setSize(Exit.getPreferredSize());
// add code here
contentPane.add(Exit);
// BUTTON FOR CREATING A NEW FRAME FOR Basic AUTO CLICKER
JButton BasicClicker = new JButton("Basic Clicker");
BasicClicker.setSize(BasicClicker.getPreferredSize());
BasicClicker.addActionListener(new BasicClickerFrame(Homef));
// ADD MORE STUFF HERE
contentPane.add(BasicClicker);
Homef.setVisible(true);
// ADD LOCATIONS HERE
placeAtThe(Exit, "right", Homef.getContentPane().getSize());
placeAtThe(Exit, "bot", Homef.getContentPane().getSize());
BasicClicker.setLocation(CW(Homef.getContentPane().getSize().width, BasicClicker.getWidth()), BasicClicker.getY());
}
}
NOTE i tried removing the first frame and just open the basic one right away.
When i did, the problem is fixed(i think,i runned it like 30 times without the problem appearing) but yeah... i dont know why....HELP!