Jump to content

TEXTFIELD GUI


alkku15

Recommended Posts

hi im noob and just learned how to make a GUI with eclipse window builder and move it into main.java (so theres no need for another .java file) 

current main.java code


@ScriptManifest(author = "gay", info = "gay", name = "gay", version = 69, logo = "")
public class Main extends Script {
	private JPanel ScriptName;
	public String chat;
	
	public void JFrame() {
		
		JFrame ScriptName;
		ScriptName = new JFrame();
		ScriptName.setResizable(false);
		ScriptName.setTitle("AIO FIGHTER"); //your own title
		ScriptName.setBounds(100, 100, 250, 250);
		ScriptName.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		ScriptName.getContentPane();
		ScriptName.setLayout(null);
		 
		//START BUTTON
		JButton btnNewButton = new JButton("START!");
		btnNewButton.setBounds(10, 177, 214, 23);
		ScriptName.add(btnNewButton);
		ScriptName.getContentPane().add(btnNewButton);
		ScriptName.setVisible(true);
		//TEXTFIELD
		JTextField txtnameOfNpc = new JTextField();
		txtnameOfNpc.setHorizontalAlignment(SwingConstants.CENTER);
		txtnameOfNpc.setText("(name of npc to kill)");
		txtnameOfNpc.setBounds(10, 11, 214, 20);
		ScriptName.add(txtnameOfNpc);
		txtnameOfNpc.setColumns(10);

		ScriptName.getContentPane().add(btnNewButton);
		ScriptName.setVisible(true); //makes the button appearing when the script start
		txtnameOfNpc.addActionListener(e -> chat = (String) txtnameOfNpc.getSelectedText().toString());
		
	}
	
	@Override
	public void onStart() {
		//log("Let's get started!");
		JFrame();
	}

	@Override
	public int onLoop() throws InterruptedException {
		
		return (random(50, 150));
	}

	@Override
	public void onExit() {
		//log("Thanks for running my Tea Thiever!");
		ScriptName.setVisible(false); 
	}

	@Override
	public void onPaint(Graphics2D g) {
		///hhhhhhhh nothing
	}

}

so now my problem is that i cant get the string "chat" to work for some reason, i tried some ActionListener things from the post below but couldnt get them to work either

and i dont know whats wrong with this one txtnameOfNpc.addActionListener(e -> chat = (String) txtnameOfNpc.getSelectedText().toString()); ?

help pls! ty!

Link to comment
Share on other sites

22 minutes ago, ProjectPact said:

Are you sure you are supposed to be using "getSelectedText()"? What happens if you just use getText()?

doesnt work, tried another post too 

and all my code here  (GUI.JAVA))))))))

public class gui {

	public void run(main Main) {
		
              JFrame jFrame = new JFrame("joujouou");
              jFrame.setSize(300, 500);
              jFrame.setResizable(false);

              JPanel settingsPanel = new JPanel();
              TitledBorder leftBorder = BorderFactory.createTitledBorder("Settings");
              leftBorder.setTitleJustification(TitledBorder.LEFT);
              settingsPanel.setBorder(leftBorder);
              settingsPanel.setLayout(null);
              settingsPanel.setBounds(5, 200, 280, 180);
              jFrame.add(settingsPanel);

              JPanel startPanel = new JPanel();
              startPanel.setLayout(null);
              startPanel.setBounds(5, 350, 70, 20);
              jFrame.add(startPanel);

              	JTextField txtnameOfNpc = new JTextField();
      			txtnameOfNpc.setHorizontalAlignment(SwingConstants.CENTER);
      			txtnameOfNpc.setText("(name of npc to kill)");
      			txtnameOfNpc.setBounds(10, 11, 214, 20);
      			settingsPanel.add(txtnameOfNpc);
      			txtnameOfNpc.setColumns(10);
      			txtnameOfNpc.addActionListener(e -> Main.hide = (String) txtnameOfNpc.getText().toString());
              
      
                JButton startButton = new JButton("Start");
                startButton.addActionListener(e -> {
                synchronized (Main.lock) {
                Main.lock.notify();
                }
                jFrame.setVisible(false);
                });
                startButton.setBounds(5, 390, 70, 20);
                startPanel.add(startButton);

                jFrame.setVisible(true);
	}
	
}

(MAIN.JAVAAAAAAAAAAAA)

@ScriptManifest(author = "hh", info = "hh", name = "hh", version = 0, logo = "")
public class main extends Script {
	Object lock = new Object();
	private gui gui = new gui();
	public String hide = "";
	
	@Override
	public void onStart() {
		gui.run(this);
	}

	@Override
	public int onLoop() throws InterruptedException {
		return (random(50, 100));
	}

	@Override
	public void onExit() {
	}

	@Override
	public void onPaint(Graphics2D g) {
		g.drawString(hide, 50, 50); <------------ TRYING TO PRINT IT OUT ON SCREEN (if this would work i would start writing aio killer ofc)
	}

}

 

Link to comment
Share on other sites

Adding an ActionListener to a text field isn't going to work (you would need to use a DocumentListener, but that would be ugly as hell and unnecessary)

The better way to approach it would be to get the text from the text field when the user clicks start.

startButton.addActionListener(e -> chat = txtnameOfNpc.getText());

Your GUI code all kinds of fucked up though

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

34 minutes ago, Explv said:

Adding an ActionListener to a text field isn't going to work (you would need to use a DocumentListener, but that would be ugly as hell and unnecessary)

The better way to approach it would be to get the text from the text field when the user clicks start.


startButton.addActionListener(e -> chat = txtnameOfNpc.getText());

Your GUI code all kinds of fucked up though

ty so much you always helping us noobs out! much love

Link to comment
Share on other sites

15 hours ago, alkku15 said:

hi im noob and just learned how to make a GUI with eclipse window builder and move it into main.java (so theres no need for another .java file) 

current main.java code



@ScriptManifest(author = "gay", info = "gay", name = "gay", version = 69, logo = "")
public class Main extends Script {
	private JPanel ScriptName;
	public String chat;
	
	public void JFrame() {
		
		JFrame ScriptName;
		ScriptName = new JFrame();
		ScriptName.setResizable(false);
		ScriptName.setTitle("AIO FIGHTER"); //your own title
		ScriptName.setBounds(100, 100, 250, 250);
		ScriptName.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		ScriptName.getContentPane();
		ScriptName.setLayout(null);
		 
		//START BUTTON
		JButton btnNewButton = new JButton("START!");
		btnNewButton.setBounds(10, 177, 214, 23);
		ScriptName.add(btnNewButton);
		ScriptName.getContentPane().add(btnNewButton);
		ScriptName.setVisible(true);
		//TEXTFIELD
		JTextField txtnameOfNpc = new JTextField();
		txtnameOfNpc.setHorizontalAlignment(SwingConstants.CENTER);
		txtnameOfNpc.setText("(name of npc to kill)");
		txtnameOfNpc.setBounds(10, 11, 214, 20);
		ScriptName.add(txtnameOfNpc);
		txtnameOfNpc.setColumns(10);

		ScriptName.getContentPane().add(btnNewButton);
		ScriptName.setVisible(true); //makes the button appearing when the script start
		txtnameOfNpc.addActionListener(e -> chat = (String) txtnameOfNpc.getSelectedText().toString());
		
	}
	
	@Override
	public void onStart() {
		//log("Let's get started!");
		JFrame();
	}

	@Override
	public int onLoop() throws InterruptedException {
		
		return (random(50, 150));
	}

	@Override
	public void onExit() {
		//log("Thanks for running my Tea Thiever!");
		ScriptName.setVisible(false); 
	}

	@Override
	public void onPaint(Graphics2D g) {
		///hhhhhhhh nothing
	}

}

 

Careful, the variable you reference in onExit is uninitialized and will throw a nullpointerexception.

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