⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 mainclass.java

📁 一个关于SWING学习的源代码. 大家过来看一看
💻 JAVA
字号:
package com.pepsan.framework;

import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuItem;
import javax.swing.JMenuBar;
import javax.swing.JLabel;
import javax.swing.ImageIcon;
import javax.swing.JPopupMenu;
import javax.swing.JPanel;
import javax.swing.SwingConstants;
import java.awt.event.*;
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.util.*;
import java.io.*;

/*
 *	MainClass is obviously top class of the SwingStarter.
 *  This is intended to illustrate a basic application implemented in
 *	in Java. This version requires Swing.
 *
 *	The intention is to provide a jump start rather than a comlete example. A 
 *  few key features include a splash screen at startup, a tips system,
 *  QuickTime content playing, examples of menus, popup menus, Swing tables, 
 *  and so forth.
 *
 *	The default file to contain the splash screen is "MainSplash.jpg". 
 *
 *	Copyright (c) 1999 Pepsan & Associates, Inc.
 */

public class MainClass {
	// The default image for the splash window
	static private String		splashName = "MainSplash.jpg";
	// The default image for the "main" window
	static private String		backFile = "swinging.gif";
	static private JTips		tips;
	static private String		tipsPropertiesFile = "Tips.properties";
	static private Properties	tipsProperties;
	// This icon is shown upper left in the tips window
	static private String		iconFile = "tipsIcon.gif";
	// The text shown at the top of the tips window
	static private String		heading = "My advice to you is to...";
	static private JFrame		myFrame;
	static private JSplash		splash;

	// MainClass accepts either zero or one argument. If an argument is
	// present it had better be a valid path to an alternatve image file!
	public static void main(String argv[]) {
		
		if (argv.length > 1) {
			System.err.println( "Too many arguments - all are ignored." );
			System.err.println( "Arguments to main are: ");
			for (int i=0; i < argv.length; i++) {
				System.err.println(argv[i]);
			}
		} else {
			splashName = argv[0];
		}
		
		myFrame = new JFrame("SwingStarter"); 
		// This is done so that we can use the QTCanvas in the QuickTime
		// player software. Swing uses something called lightweight components
		// while QTCanvas uses heavyweight (like AWT) components. The result
		// is that the Swing menus draw behind the QuickTime player and the
		// menu items cannot be seen. Setting this forces them to be heavy-
		// weights that show up properly. This is one of several possible solutions.
		JPopupMenu.setDefaultLightWeightPopupEnabled(false);
		
		// Create the menubar, menus and menu items along with the event
		// handlers
		createMenus();
		
		// Create the popup menu for the main window
		createPopupMenu();
		
		myFrame.addWindowListener(new WindowAdapter () {
			public void windowClosing (WindowEvent e) {
				close();
			}
			
			public void windowClosed (WindowEvent e) { 
				close();
			}
		});		
		
		// Create and display the splash screen
		splash = new JSplash(myFrame, splashName, 5000);
		JLabel backgroundImage = new JLabel(new ImageIcon(backFile));
		JLabel psa1 = new JLabel("Courtesy of Pepsan & Associates, Inc.", SwingConstants.CENTER);
		psa1.setPreferredSize(new Dimension(300,20));
		JLabel psa2 = new JLabel("http://www.pepsan.com", SwingConstants.CENTER);
		psa2.setPreferredSize(new Dimension(300,20));
		JPanel btm = new JPanel();
		btm.setLayout(new BorderLayout());
		btm.add(psa1, BorderLayout.NORTH);
		btm.add(psa2, BorderLayout.SOUTH);
		myFrame.getContentPane().add(backgroundImage, BorderLayout.NORTH);
		myFrame.getContentPane().add(btm, BorderLayout.SOUTH);
		myFrame.pack();
		splash.show();
		splash.requestFocus(); 
		// This causes the whole application to block until the splash is gone
		splash.block();
		// Once splash is done, show main window
		myFrame.show(); 
		
		// ..and at the same time the tips window. Note that if the "show"
		// flag in properties is false the tips window will not actually
		// be displayed, just initialized
		tipsProperties = getTipsProperties();
		tips = new JTips("Tips.txt", tipsProperties, iconFile, heading); 
		tips.startup(); 
	}
	
	// Create menubar and menus for the main window
	private static void createMenus() {
		JMenuBar 	myMenuBar;
	    JMenu 		fileMenu;
	    JMenu		infoMenu;
		JMenuItem	closeItem, aboutItem, tipsItem;
		
		// Build the menubar
		myMenuBar = new JMenuBar();
		
		// Build File menu
		fileMenu = new JMenu("File");
		myMenuBar.add(fileMenu);
		closeItem = new JMenuItem("Close");
		fileMenu.add(closeItem);
		
		// Build the Info menu
		infoMenu = new JMenu("Info");
		myMenuBar.add(infoMenu);
		aboutItem = new JMenuItem("About...");
		infoMenu.add(aboutItem);
		infoMenu.addSeparator();
		tipsItem = new JMenuItem("Tips...");
		infoMenu.add(tipsItem);
		
		// Set up Action Listeners for the Menu items
		closeItem.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				// Close down the application
				close();
			}
		});
		
		aboutItem.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				// Show the splash screen
				showSplash();
			}
		});

		tipsItem.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				// Show the tips
				showTips();
			}
		});

		myFrame.setJMenuBar(myMenuBar);	
	}
	
	// Create a Popup menu on the main window
	private static void createPopupMenu() {
		final JPopupMenu	popup;
		JMenuItem			quickTimeDemo, stockQuoteTable;
		
		popup = new JPopupMenu();
		quickTimeDemo = new JMenuItem("QuickTime Demo");
		quickTimeDemo.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				// Play QuickTime content
				new PlayQuickTime();
			}
		});
		
		stockQuoteTable = new JMenuItem("Stock Quote Table");
		stockQuoteTable.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				// Open the table of stock quotes
				new StockTable();
			}
		});
		
		popup.add(quickTimeDemo);
		popup.add(stockQuoteTable);
		
		myFrame.getContentPane().addMouseListener(new MouseAdapter() {
			public void mousePressed(MouseEvent e) {
				showPopup(e, popup);
			}
			public void mouseClicked(MouseEvent e) {
				showPopup(e, popup);
			}
			public void mouseReleased(MouseEvent e) {
				showPopup(e, popup);
			}
		});
	}
	
	// Will show the Popup menu if the popup trigger occurred
	private static void showPopup(MouseEvent e, JPopupMenu popup) {
		if (e.isPopupTrigger()) {
			popup.show(myFrame, e.getX(), e.getY());
		}
	}
	
	// Creates a new Properties object to hold the Tips system settings
	// if one doesn't exist. If one already exists it reads in the
	// settings.
	private static Properties getTipsProperties() {
		FileInputStream		tipsPropertiesStream;
		
		Properties tipsProperties = new Properties();
		try {
			tipsPropertiesStream = new FileInputStream(tipsPropertiesFile);
			try {
				tipsProperties.load(tipsPropertiesStream);
			} catch (IOException e) {
				// We continue even after this, using an empty
				// properties object
				System.err.println("IO Exception reading Tips properties file.");
			}
		} catch (FileNotFoundException e) {
			// The fact the file doesn't exist is not an error
		}
		
		return tipsProperties;
	}
	
	// Saves the Tips properties (should be called when application exits)
	private static void saveTipsProperties(Properties tipsProperties) {
		FileOutputStream tipsPropertiesStream;
		
		try {
			tipsPropertiesStream = new FileOutputStream(tipsPropertiesFile);
			tipsProperties.save(tipsPropertiesStream, "Tips Properties");
			tipsPropertiesStream.close();
		} catch (IOException e) {
			System.err.println("Couldn't write Tips properties!");
		}	
	}
	// User wants us to go away now...
	private static void close() {
		saveTipsProperties(tipsProperties);
		myFrame.setVisible(false);
		System.exit( 0 );		
	}
	
	private static void showSplash() {
		splash.show();
		// Critical - this focus request must be here after 
		// showing the parent to get keystrokes properly. 
		splash.requestFocus(); 	
		// Note, we don't block when showing this after startup
		// so a mouse click will dismiss it
	}
	
	private static void showTips() {
		tips.setVisible(true);
	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -