Jump to content

How to make a class load in the main class?


Recommended Posts

Posted

Hello everyone.

 

I've been struggling with something.

 

I have 1 class called main.class. And i have an events.class for my GUI. Now i want to load the events.class in the main.class. The reason why i have 2 classes is that i want to keep my classes clean. 1 for the GUI, 1 for the bot script.

 

Thanks in advance,

 

Sebastian.

Posted

add in onStart. event.GUI(this);

or change the GUI to w/e method that contains your gui.

 

I don't really understand. My GUI is just a class. I followed a tutorial on how to make GUI. So i wanted to just test if the GUI would popup. 

 

To make it easier, this is my events.java: 

import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;

public class events extends JFrame {
	
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	public static void main (String args[]) {
		events gui = new events();
		gui.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		gui.setTitle("Events Program");
		gui.setSize(300, 100);
		gui.setVisible(true);
	}
	
	private JLabel label;
	private JButton button;
	private JLabel label2;
	private JButton button2;
	private int x = 0,y = 0;

	public events() {
		setLayout(new FlowLayout());
		button = new JButton("Click for text");
		add(button);
		
		label = new JLabel("");
		add(label);
		
		event e = new event();
		button.addActionListener(e);
		
		button2 = new JButton("Click for more text");
		add(button2);
		
		label2 = new JLabel("");
		add(label2);
		
		event2 ev = new event2();
		button2.addActionListener(ev);
		
	}
	public class event2 implements ActionListener {
		public void actionPerformed(ActionEvent ev){
			if(y==0){
			label2.setText("Label 2 text");
			y = 1;
			}
			else if(y==1){
				y = 0;
				label2.setText("");
			}
		}
}
	public class event implements ActionListener {
		public void actionPerformed(ActionEvent e) {
			if(x==0){
			label.setText("Text");
			x = 1;
			}
			else if(x == 1) {
				label.setText("");
				x = 0;
				
			}
		}
	}
}

When i add events.GUI(this); just doesn't work. 

Posted

I don't really understand. My GUI is just a class. I followed a tutorial on how to make GUI. So i wanted to just test if the GUI would popup. 

 

To make it easier, this is my events.java: 

import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;

public class events extends JFrame {
	
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	public static void main (String args[]) {
		events gui = new events();
		gui.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		gui.setTitle("Events Program");
		gui.setSize(300, 100);
		gui.setVisible(true);
	}
	
	private JLabel label;
	private JButton button;
	private JLabel label2;
	private JButton button2;
	private int x = 0,y = 0;

	public events() {
		setLayout(new FlowLayout());
		button = new JButton("Click for text");
		add(button);
		
		label = new JLabel("");
		add(label);
		
		event e = new event();
		button.addActionListener(e);
		
		button2 = new JButton("Click for more text");
		add(button2);
		
		label2 = new JLabel("");
		add(label2);
		
		event2 ev = new event2();
		button2.addActionListener(ev);
		
	}
	public class event2 implements ActionListener {
		public void actionPerformed(ActionEvent ev){
			if(y==0){
			label2.setText("Label 2 text");
			y = 1;
			}
			else if(y==1){
				y = 0;
				label2.setText("");
			}
		}
}
	public class event implements ActionListener {
		public void actionPerformed(ActionEvent e) {
			if(x==0){
			label.setText("Text");
			x = 1;
			}
			else if(x == 1) {
				label.setText("");
				x = 0;
				
			}
		}
	}
}

When i add events.GUI(this); just doesn't work. 

I'm guessing it would be events.main(this); in you'r case. seems a bit over-complicated class doe.

  • Like 1
Posted (edited)

I'm guessing it would be events.main(this); in you'r case. seems a bit over-complicated class doe.

 

Thanks bro. Testing it now.

Over-complicated? Do you have a better idea for this?

 

EDIT: It Worked! Thanks buddy. But i have one problem now. When i click on the GUI's exit X, it stops my whole OSBot client.

           Do you have any solutions to that? Sorry for the big questions tho. Very new to GUI's.

Edited by OSRS Sebastian
Posted (edited)

Thanks bro. Testing it now.

Over-complicated? Do you have a better idea for this?

 

EDIT: It Worked! Thanks buddy. But i have one problem now. When i click on the GUI's exit X, it stops my whole OSBot client.

           Do you have any solutions to that? Sorry for the big questions tho. Very new to GUI's.

you have this: 

gui.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

you could just remove it and everything would work fine.

or you could change EXIT_ON_CLOSE to HIDE_ON_CLOSE

Edited by Viliuks
Posted

Can you help me with one little thing tho? if it's not too much ofcourse:

 

How do stop the loop from my script till the GUI is not visible anymore? Because, when the GUI is open, the script will automaticly start chopping tree's.

gui.setVisible(true);
        try {
            while (gui.isVisible()) {
                sleep(100);
            }
        }catch (InterruptedException e){
            e.printStackTrace();
        }
  • Like 1
Posted (edited)

Can you help me with one little thing tho? if it's not too much ofcourse:

 

How do stop the loop from my script till the GUI is not visible anymore? Because, when the GUI is open, the script will automaticly start chopping tree's.

private boolean GUI_Initialized = false;

public void onStart() {

        // start GUI

        java.awt.EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                GUI = new /* GUI FILE NAME */();
                GUI_Initialized = true;
                GUI.setVisible(true);
            }
        });
        while (true) {
            if (GUI_Initialized && !GUI.isVisible()) {
                break;
            }
            sleepFor(100, 200);
        }

        // end GUI

/*
 * Any other onStart stuff
 */

}

This should solve your issue. Good luck! smile.png

 

Read the thread more thoroughly. Try this in your GUI class.

public class ShinyScript_GUI extends JFrame {

        // this keeps your class cleaner
	public JPanel mainPanel;

        // these are your labels, fields and anything else your GUI is using
	public JLabel labelTarget;
	public JTextField textFieldTarget;

        // this is the main executor for your GUI
	public ShinyScript_GUI() {
		super("ShinyScript v0.00");
		setSize(500, 500);
		setLocationRelativeTo(null);
		setDefaultCloseOperation(DISPOSE_ON_CLOSE);
		addToMainPanel();
		add(mainPanel);
	}

        // this helps keep your GUI clean as well
	public void addToMainPanel() {
		// main panel, this is pretty much 100% required
		mainPanel = new JPanel();
		mainPanel.setBounds(0, 0, 469, 496);
		mainPanel.setLayout(null);

		// buttons, fields, anything in your gui pretty much
		labelTarget = new JLabel("Target:");
		labelTarget.setBounds(21, 11, 50, 21);
		mainPanel.add(labelTarget);
		labelTarget.setFont(new Font("Arial", Font.BOLD, 12));
	}

Edited by Shiny
  • Like 1
Posted

Both solutions didn't work. But i think that problem is on my side. I'm a little confused tho. I have 2 classes. 1 class for the bot script. and 1 class for the GUI. Do i need to insert this into the onStart() { of my main.java (Bot script) or? 

 

Do you guys think it's better to just use 1 class for everything?

Posted

Both solutions didn't work. But i think that problem is on my side. I'm a little confused tho. I have 2 classes. 1 class for the bot script. and 1 class for the GUI. Do i need to insert this into the onStart() { of my main.java (Bot script) or? 

 

Do you guys think it's better to just use 1 class for everything?

 

I edited previous post with more info. If that doesn't work then I suggest doing more reading into java.

 

  • Like 1
Posted (edited)

Thanks for the quick reply! I guess it's on my side that i don't understand a living shit out of GUI's haha. I'm gonna watch more tutorials how to make a GUI. 

Do you have any idea where to start with? I heard WindowBuilder in Eclipse is a good one?

 

Yes window builder is a good tool for GUI's. I suggest going through the newboston's tutorials for java. You will learn enough of a foundation to be able to branch out on your own. Good luck!

 

Edit: If my boredom continues today, I might make a tutorial on how to make an AIO woodcutter from scratch. We'll see. It might be a bit controversial, but there's like 100 AIO woodcutters and it's the easiest tutorial to make lol.

 

Edited by Shiny
  • Like 1

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...