Captain's Universe Home
Captain's Universe Home
Cosmic Ray Muon DetectorTeleGarden Pages
Time on MarsBryophyllum Plants
Jupiter Radio AstronomyAncient Pages
Salzburg Tourist GuideEarth Magnetometer
  H O M E     AJAX & MORE     LINUX & MORE     RTAI     XENOMAI     ADEOS IPIPE      
    JAVA & BROWSERS     *NIX     ELECTRONICS     REVIEWS     ARTEMIA     FAIRY SHRIMP      



Java Application Template GUI Tutorial Part 3/5 - How to make an executable JAR file

In PART 1 and PART 2 we have created the classes for displaying the splash screen and invoking the main-class. Now it's time to look at the actual program, the class Main.

In this class, we open a JFrame, add a menu-bar and a tool-bar. Furthermore we handle the menubar events and also the toolbar events. Finally we add the App1-class to the JFrame, which contains the rest of the application code.



Main.java:
// JAVA Application Template (C) 2005 www.captain.at
// class Main: creates the main window with the menu-bar and tool-bar
// adds App1 to the bottom part of the layout in the main-window

import javax.swing.*;
import java.awt.*;
import java.awt.event.*; 

public class Main {
 
		private JTabbedPane tabbedPane;
		private JFrame dlframe; 
		App1 app1;
		
		public Main() {
      
			dlframe = new JFrame("JAVA Application Template (C) 2005 www.captain.at");
      
			dlframe.addWindowListener(new WindowAdapter() {
				public void windowClosing(WindowEvent e) {
					System.exit(0);
				}
			});

			buildMenu();

			app1 = new App1();
			
			JToolBar toolBar = new JToolBar();
			addButtons(toolBar);
			
			dlframe.getContentPane().add(toolBar, BorderLayout.NORTH);
			dlframe.getContentPane().add( app1, BorderLayout.CENTER );

			dlframe.pack();
			dlframe.setSize(765, 690);
        	dlframe.setBackground(Color.white);
			dlframe.setVisible(true);

		}

		private void buildMenu() {
			JMenuBar menubar = new JMenuBar();
			JMenu filemenu = new JMenu("File");
			JMenuItem openitem = new JMenuItem("Open");
			JMenuItem saveitem = new JMenuItem("Save");
			JMenuItem exititem = new JMenuItem("Exit");
           
 			openitem.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					app1.loadFile();
				}
			});

 			saveitem.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					app1.saveFile();
				}
			});
 
			exititem.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					System.exit(0);
				}
			}); 

 
			filemenu.add(openitem);
			filemenu.add(saveitem);
			filemenu.add(exititem);
			menubar.add(filemenu);
			dlframe.setJMenuBar(menubar);
			
		}
		
		protected void addButtons(JToolBar toolBar) {
			JButton button = null;
			button = new JButton("Button1");
			button.setToolTipText("This is Button1");
			button.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					System.out.println("Button1 pressed");
				}
			});
			toolBar.add(button);
		}
		
		public static void main(String[] args) {
			
			Main dl = new Main();

		}
        
}                              




PART 1 PART 2 PART 3 PART 4 PART 5


Last-Modified: Sat, 04 Feb 2006 16:03:10 GMT

Google
 
Web www.captain.at
go to top
© 1996-2010 . All rights reserved.
No reproduction, distribution, publishing or transmission of the copyrighted materials at this site is permitted. Policy
go to top