Jump to content

Explv's Dank GUI Tutorial


Explv

Recommended Posts

  • 3 weeks later...

Why do you dispose of it in onExit()? Wouldn't that get rid of it when the script stops and not when you press the start button?

 

Yes. When the user stops the script the JFrame is disposed of if it exists.

 

The JFrame is also hidden when the user starts the script:

startButton.addActionListener(e -> { // This code is called when the user clicks the button
    started  = true; // Set the boolean variable started to true
    gui.setVisible(false); // Hide the GUI
});

See the line 

gui.setVisible(false); // Hide the GUI
  • Like 1
Link to comment
Share on other sites

  • 2 months later...

Why declare a JFrame within the script class? Separate it.

public class GUI extends JFrame {
    public GUI() {
        setTitle("GUI);
        ...
        pack();
        setVisible(true);
    }
}

Instantiate within the script class

public class ShitScript extends Script {
    private GUI gui;
...
    @Override
    public void onStart() {
        gui = new GUI();
    }

    @Override
    public void onExit() {
        if(gui != null) gui.dispose();
    }
...
}

This way, you can manage your GUI without having to trawl through a ton of unrelated code in the script class.

  • Like 2
Link to comment
Share on other sites

Why declare a JFrame within the script class? Separate it.

public class GUI extends JFrame {
    public GUI() {
        setTitle("GUI);
        ...
        pack();
        setVisible(true);
    }
}

Instantiate within the script class

public class ShitScript extends Script {
    private GUI gui;
...
    @Override
    public void onStart() {
        gui = new GUI();
    }

    @Override
    public void onExit() {
        if(gui != null) gui.dispose();
    }
...
}

This way, you can manage your GUI without having to trawl through a ton of unrelated code in the script class.

 

Why are you reading a GUI tutorial noob :kappa:

Edited by Explv
Link to comment
Share on other sites

// Declare two constants for width and height of the GUI
final int GUI_WIDTH = 350, GUI_HEIGHT = 75;
        
// Get the size of the screen
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
        
// Calculating x and y coordinates
final int gX = (int) (screenSize.getWidth() / 2) - (GUI_WIDTH / 2);
final int gY = (int) (screenSize.getHeight() / 2) - (GUI_HEIGHT / 2);
Or to set a size to exactly your liking

gui#setSize(int width, int heigth) //size of the gui;
gui#setLocationRelativeTo(null); //centers the gui
Nice guide, will help the noobs. good job! biggrin.png Edited by Psvxe
  • Like 1
Link to comment
Share on other sites

// Declare two constants for width and height of the GUI
final int GUI_WIDTH = 350, GUI_HEIGHT = 75;
        
// Get the size of the screen
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
        
// Calculating x and y coordinates
final int gX = (int) (screenSize.getWidth() / 2) - (GUI_WIDTH / 2);
final int gY = (int) (screenSize.getHeight() / 2) - (GUI_HEIGHT / 2);
Or to set a size to exactly your liking

gui#setSize(int width, int heigth) //size of the gui;
gui#setLocationRelativeTo(null); //centers the gui
Nice guide, will help the noobs. good job! biggrin.png

 

 

Nice, I wrote this a while ago :P  definitely more succinct to use setLocationRelativeTo(null)  

Edited by Explv
  • Like 1
Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

  • Recently Browsing   0 members

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