Fran 99 Share Posted February 18, 2015 Hi guys, I have been working on a GUI, created it using JFrame (Windows Builder), but need some help with setting the logic once created (I am a begginer scripter learning), if someone can help I can add to Skype, but basically: This is the code: (can upload to github if better) import java.awt.BorderLayout; import java.awt.EventQueue; import javax.swing.JFrame; import javax.swing.JPanel; import javax.swing.border.EmptyBorder; import javax.swing.GroupLayout; import javax.swing.GroupLayout.Alignment; import javax.swing.JLabel; import javax.swing.JCheckBox; import javax.swing.LayoutStyle.ComponentPlacement; import javax.swing.JButton; import java.awt.Font; import java.awt.Color; public class CDGui extends JFrame { private JPanel contentPane; /** * Launch the application. */ public static void main(String[] args) { EventQueue.invokeLater(new Runnable() { public void run() { try { CDGui frame = new CDGui(); frame.setVisible(true); } catch (Exception e) { e.printStackTrace(); } } }); } /** * Create the frame. */ public CDGui() { setBackground(Color.LIGHT_GRAY); setTitle("GUI"); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setBounds(100, 100, 210, 260); contentPane = new JPanel(); contentPane.setBorder(new EmptyBorder(5, 5, 5, 5)); setContentPane(contentPane); JLabel lblLootingList = new JLabel("Looting list"); lblLootingList.setFont(new Font("Tahoma", Font.BOLD, 11)); JCheckBox chckbxFeathers = new JCheckBox("Feathers"); JButton btnRunChickendestruction = new JButton("Run ChickenDestruction"); JLabel lblOtherOptions = new JLabel("Other Options"); lblOtherOptions.setFont(new Font("Tahoma", Font.BOLD, 11)); JCheckBox chckbxBuryBones = new JCheckBox("Bury Bones"); //layout GroupLayout gl_contentPane = new GroupLayout(contentPane); gl_contentPane.setHorizontalGroup( gl_contentPane.createParallelGroup(Alignment.LEADING) .addGroup(gl_contentPane.createSequentialGroup() .addContainerGap() .addGroup(gl_contentPane.createParallelGroup(Alignment.LEADING) .addComponent(chckbxFeathers) .addComponent(lblOtherOptions) .addComponent(lblLootingList) .addComponent(chckbxBuryBones) .addComponent(btnRunChickendestruction)) .addContainerGap(67, Short.MAX_VALUE)) ); gl_contentPane.setVerticalGroup( gl_contentPane.createParallelGroup(Alignment.LEADING) .addGroup(gl_contentPane.createSequentialGroup() .addContainerGap() .addComponent(lblLootingList) .addGap(7) .addComponent(chckbxFeathers) .addGap(34) .addComponent(lblOtherOptions) .addPreferredGap(ComponentPlacement.UNRELATED) .addComponent(chckbxBuryBones) .addPreferredGap(ComponentPlacement.RELATED, 35, Short.MAX_VALUE) .addComponent(btnRunChickendestruction) .addGap(21)) ); contentPane.setLayout(gl_contentPane); } } Which translates to this GUI: I am needing help with the following: - I would like the GUI that when I click on (for example) the JButton "Run ChickenDestruction" to execute the onStart method (that starts the script) from the main class Can someone please help? I am just not familiar working with GUIs and would really like to learn. Thanks very much in advance Link to comment Share on other sites More sharing options...
Kristoffer 24 Share Posted February 19, 2015 Tip: learn how to create a gui manually first before using gui builders. To answer your question: you could create a boolean variable which you set to true when the start button is pressed (using an ActionListener). Then, in your loop make your script return if the boolean variable is false (before any other code, so your script only starts after the start button is pressed). Link to comment Share on other sites More sharing options...
Fran 99 Author Share Posted February 19, 2015 Tip: learn how to create a gui manually first before using gui builders. To answer your question: you could create a boolean variable which you set to true when the start button is pressed (using an ActionListener). Then, in your loop make your script return if the boolean variable is false (before any other code, so your script only starts after the start button is pressed). Hi Kris, many thanks for the reply. was wondering if you could please post an example ? Thanks again _________________________________________________________________________________________ EDIT: I tried adding an ActionListener to the JButton (in the GUI class) and then adding the following code in the Main class: private class submain implements ActionListener {public void actionPerformed(ActionEvent e) { onStart(); }} public void onStart(){ CDGui gui = new CDGui(); gui.setVisible(true); ///rest of onStart code I am having troubles in the GUI class when defining the ActionListener that's added to the buttoncd object so that the main class implements it: JButton buttoncd = new JButton("Run ChickenDestruction"); buttoncd.addActionListener(); //having problems defining method to call inside () add (buttoncd); Link to comment Share on other sites More sharing options...
Vlad 216 Share Posted February 19, 2015 Hi Kris, many thanks for the reply. was wondering if you could please post an example ? Thanks again _________________________________________________________________________________________ EDIT: I tried adding an ActionListener to the JButton (in the GUI class) and then adding the following code in the Main class: private class submain implements ActionListener {public void actionPerformed(ActionEvent e) { onStart(); }} public void onStart(){ CDGui gui = new CDGui(); gui.setVisible(true); ///rest of onStart code I am having troubles in the GUI class when defining the ActionListener that's added to the buttoncd object so that the main class implements it: JButton buttoncd = new JButton("Run ChickenDestruction"); buttoncd.addActionListener(); //having problems defining method to call inside () add (buttoncd); This shows a way to implement a GUI in your script that works: http://dreambot.org/forums/index.php?/topic/880-haku-s-aio-woodcutter-source/ I can't say for sure if it's the best way since I personally hate GUIs with a passion so I didn't bother making it 1337. Link to comment Share on other sites More sharing options...
qbots 239 Share Posted February 19, 2015 button.addActionListener(e->script.running = true); //Assumes you have a public boolean called running in your script. alternatively if you dont want to use a lambda: button.addActionListener(new ActionListener() {@Overridepublic void actionPerformed(ActionEvent e) {//doshit}}); Link to comment Share on other sites More sharing options...
Zawy 928 Share Posted February 19, 2015 button.addActionListener(e->script.running = true); //Assumes you have a public boolean called running in your script. alternatively if you dont want to use a lambda: button.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { //doshit } }); u can explain so gut man Link to comment Share on other sites More sharing options...
Explicit 213 Share Posted February 19, 2015 This shows a way to implement a GUI in your script that works: http://dreambot.org/forums/index.php?/topic/880-haku-s-aio-woodcutter-source/ I can't say for sure if it's the best way since I personally hate GUIs with a passion so I didn't bother making it 1337. @OP never rely on visibility checks like this. Link to comment Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.