user6287736 user6287736 - 1 year ago 83
Java Question

How to let the user stop a java application

I have an application which mainly uses a bot to press keys when a condition is met. I am used to working with applets so I am wondering about how the user stops the application. I did it by using the task manager but that is not very user friendly, is it?
So how can I make I UI for a standalone java application? Or is there an other way to give the user the opportunity to stop the application?

Answer Source

First of all I need 50 reputation to comment, thats why I write an answer...

Secondly Do you have a JFrame?

If not Create a new JFrame, JPanel and JButton with:

JFrame frame = new JFrame("title");
JPanel  panel = new JPanel();
JButton button = new JButton("Close");

//You need this for the screen to show.

//add the panel to the frame and the button to the panel 
button.addActionListener(this);//the class needs to implements ActionListener


//this is the actionperformed method which will run if the button is clicked.
public void actionPerformed(ActionEvent e) {
      if(e.getSource==button){//If you have more than one button.
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download