Jump to content

zeroter5

Recommended Posts

Hello Osbot community, I am currently working on a script where you enter a mob name in an J-input dialog window, and select an item to eat from a dropdown(JComboBox) menu and the bot will kill the any nearby mobs with the name you entered and when your character drops below a certain hp mark your character will eat the food. At the current moment I have the script set up such that each of these 2 actions is accomplished in separate jframes/windows. Can anyone point me in the right direction on combining the windows into one? Any help would be much appreciated!

i've included pictures of part of my code in these imgur links below

VbbFzsG.png

(how the 2 windows are displayed when program is run)

Dyhxhh5.png

the createGUI method call creates the jcombobox in this image while Joptionpane is the input box

Link to comment
Share on other sites

Heyo,

Firstly, instead of doing this, perhaps you could just make the script heal up with any items in the inventory with an eat option? That way, you wouldn't even need this extra setup step!

If you still want to do it, I would suggest creating your own frame. JOptionPane is fine for small things where you need one or two settings to be saved, but when you start working with more complicated configurations, having multiple JOptionPanes is a bit messy. (You could perhaps extend panel and attach this to your option pane, but  I personally prefer creating a whole new frame). You could do this by creating a new class which extends JFrame, and perhaps create and position text fields within this frame in the constructor of the class. There will be plenty of swing tutorials online as it's a pretty standard thing, and you can even get window builders to help you generate the code (often as plugins to IDEs). I would recommend using a layout other than absolute though, since 4k and other higher resolution screens are becoming more widely used so re sizeable windows are very preferable (swing issues :/).

26 minutes ago, Chris said:

add a JTextField to the gui

mobName = JTextField#getText

You cannot do this directly to the JOptionPane!

Edit: it looks like you've already created your own frame, do as Chris said! (apologies, I misread your post)

Edit2: You can also (I believe) get rid of that potentially infinite sleep after the mobName initialisation, since I think JOptionPane does that for you. Just be sure to run some string analysis on that variable to make sure it's valid.

Edited by Apaec
Link to comment
Share on other sites

Thanks for the suggestions guys, i tried adding in the label and textfield into a new panel and then adding that new panel to the gui as a whole. So i have the panel that contains the dropdown box/label and then the textfield one. 

Here's what I end up with A3jltsF.png

private void createGUI(){

 

final int guiWidth = 350, guiHieght = 150;

 

Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();

 

// Calculating x and y coordinates

        final int gX = (int) (screenSize.getWidth() / 2) - (guiWidth / 2);

        final int gY = (int) (screenSize.getHeight() / 2) - (guiHieght / 2);

 

        // create a new JFrame with the title "GUI"

        gui = new JFrame("GUI");

       

        // set the x coordinate, y coordinate, width and height of the GUI

        gui.setBounds(gX, gY, guiWidth, guiHieght);

gui.setResizable(true);

// Create a sub container JPanel

JPanel panel = new JPanel();

JPanel panel1 = new JPanel();

gui.add(panel);

gui.add(panel1);

 

JLabel label = new JLabel("Select a food type:"); // create a label

JLabel label1 = new JLabel("Enter Mob Name");

label.setForeground(Color.white);

label1.setForeground(Color.white);

panel.add(label); // add it to the JPanel 

panel1.add(label1);

       

 

// create a select box for food options

        JComboBox<String> foodSelector = new JComboBox<>(new String[]{"Shrimps", "Trout", "Salmon","Lobster", null});

        JTextField mobName = new JTextField();

        // add an action listener, to listen for user's selections, assign to a variable called selectedFood on selection.

        foodSelector.addActionListener(e -> selectedFood = foodSelector.getSelectedItem().toString());

 

        // add the select/text-field box to the JPanel panels

        panel.add(foodSelector);

        panel1.add(mobName);

        

        JButton startButton = new JButton("Start");

// add an action listener to the button

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

});

panel.add(startButton); // Add the button to the panel

 

 

gui.setVisible(true); //makes GUI visible

}

Here is my code for the GUI, I think my problem may just be the panels are on top of each other but im not sure?

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...