2016-05-05 8 views
0

条件が満たされたときに主にボットを使用してキーを押すアプリケーションがあります。私はアプレットを扱うのに慣れているので、ユーザーがアプリケーションをどのように停止するのか不思議です。私はタスクマネージャーを使ってそれをやったが、それはあまりユーザーフレンドリーではありませんでしたか? スタンドアロンJavaアプリケーションのUIを作成するにはどうしたらいいですか?または、ユーザーにアプリケーションを停止する機会を与える他の方法はありますか?あなたはGUIにボタンを追加する必要がユーザーにJavaアプリケーションを停止させる方法

+1

'でSystem.exit(0)' –

+1

はええ、あなたが 'JButton'インターフェイスを作成する必要があります。..アプリケーションを強制終了します@ cricket_007のように 'System.exit(0)'を呼び出す 'actionListener'を持っています。 –

答えて

0

あなたはJFrameのを持っていますか?

で新しいJFrameの、JPanelの、とのJButtonを作成していない場合は、次の

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

//You need this for the screen to show. 
frame.setSize(width,height); 
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
frame.setResizable(false); 
frame.setLocationRelativeTo(null); 


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

frame.setVisible(true); 

//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. 
      System.exit(0); 
     } 
} 
3

、それにアクションリスナーを設定し、addActionListenerでSystem.exit()メソッドを呼び出します。..

例:

JButton showDialogButton = new JButton("Exit the app"); 
showDialogButton.addActionListener(new ActionListener() { 
     public void actionPerformed(ActionEvent e) { 
      System.exit(0); 
     } 
}); 
関連する問題