Hi all,
please help me connecting exit menu to OnExit event handler
Here is what I have coded so far!
Also a coding question: What is the best method between these two:
1. Creating instance of JFrame and call its methods like set Size
2. create class that extends JFrame and override the methods with mine
Thanks a lot
package learnswing;
import java.awt.event.ActionEvent;
import java.awt.event.KeyEvent;
import javax.swing.*;
public class MainJava{
public static void main(String[] args){
Frame test = new Frame();
test.MakeFrame();
}
}
class Frame{
public void MakeFrame(){
JFrame frame = new JFrame();
//make menu
this.MakeMenu(frame);
frame.setSize(300, 200);
frame.setTitle("Iam JFrame");
frame.setVisible(true);
}
private void MakeMenu(JFrame fr){
//declarations
JMenuBar menubar;
JMenu file, help;
JMenuItem openfile, exit;
JMenuItem about;
//create menu
menubar = new JMenuBar();
//file menu
file = new JMenu("File");
//add components
openfile = new JMenuItem("Open...Ctrl+O");
openfile.setMnemonic(KeyEvent.VK_O);
file.add(openfile);
exit = new JMenuItem("Exit...Ctrl+Q");
exit.setMnemonic(KeyEvent.VK_Q);
//tie to action
//exit.addActionListener(this.OnExit());
file.addSeparator();
file.add(exit);
//ad them to menubar
menubar.add(file);
//help menu
help = new JMenu("Help");
//add components
about = new JMenuItem("Help...Ctrl+H");
about.setMnemonic(KeyEvent.VK_H);
help.add(about);
//ad them to menubar
menubar.add(help);
//set menubar
fr.setJMenuBar(menubar);
}
private void OnExit(ActionEvent evt){
System.exit(0);
}
}