javademoframe.java

来自「有关java的源程序,为讲授java程序设计课程使用」· Java 代码 · 共 1,061 行 · 第 1/2 页

JAVA
1,061
字号
//******************************************************************************
// JavaDemoFrame.java:	
//
//******************************************************************************
import java.awt.*;
import java.awt.image.*;
import IDR_MENU1;
import IDD_DIALOGABOUT;
import IDD_FINDDIALOG;
import IDD_COLORDIALOG;
import IDD_EXITDIALOG;
import IDR_MENU2;
import JavaDemo;
import java.io.*;
import java.util.*;

//import JavaDemo;
//==============================================================================
// STANDALONE APPLICATION SUPPORT
// 	This frame class acts as a top-level window in which the applet appears
// when it's run as a standalone application.
//==============================================================================
class JavaDemoFrame extends Frame
{
	IDR_MENU1 mainMenu;
	IDR_MENU2 subMenu;
	ExitDialogFrame exitDialogFrame;

	// JavaDemoFrame constructor
	//--------------------------------------------------------------------------
	public JavaDemoFrame(String titleString)
	{
		// TODO: Add additional construction code here
		super (titleString);

		setCursor(Frame.HAND_CURSOR);
		setFont(new Font("Helvetica",Font.PLAIN,14));

// change Frame background color 
		JavaDemo.appletBackGround=Color.green;
    	setBackground(JavaDemo.appletBackGround);

//******* Set The Icon Image(but not valid)***********
		String imageFile="images/img0001.gif";
	    setIconImage(Toolkit.getDefaultToolkit().getImage(imageFile));

		mainMenu=new IDR_MENU1(this);
		subMenu=new IDR_MENU2(this);
		mainMenu.CreateMenu();
	}

	
	// The handleEvent() method receives all events generated within the frame
	// window. You can use this method to respond to window events. To respond
	// to events generated by menus, buttons, etc. or other controls in the
	// frame window but not managed by the applet, override the window's
	// action() method.
	//--------------------------------------------------------------------------
	public boolean handleEvent(Event evt)
	{
		switch (evt.id)
		{
			// Application shutdown (e.g. user chooses Close from the system menu).
			//------------------------------------------------------------------
			case Event.WINDOW_DESTROY:
				
				// TODO: Place additional clean up code here
				exitDialogFrame=
				  new ExitDialogFrame("Exit JavaDemo Dialog Frame");		
			
				break;		
				
		}	
		return super.handleEvent(evt);
	}

	public boolean action(Event event,Object obj)
	{
		if(event.target instanceof MenuItem)
		{
// File Menu Content
		if(event.target==mainMenu.IDM_OPEN)
			{
				   FileDialog fileDialog=new FileDialog(this,"Open File Dialog");
				   fileDialog.show();
				   System.out.println(fileDialog.getFile());// get selected file name
									  
			}	
		else if(event.target==mainMenu.IDM_SAVE)
			{

			}
		else if(event.target==mainMenu.IDM_SAVEAS)
			{
				   FileDialog fileDialog=new FileDialog(this,"Save File Dialog",FileDialog.SAVE);
				   fileDialog.show();
			}
		else if(event.target==mainMenu.IDM_BLINK)
			{
				  BlinkTextFrame blinkTextFrame=
				  new BlinkTextFrame("This Is Blink Text Frame",500,400);		
				   
			}
		else if(event.target==mainMenu.IDM_EXIT)
			{
			  exitDialogFrame=
				  new ExitDialogFrame("Exit JavaDemo Dialog Frame");		
				   
			}
// Edit Menu Content
		else if(event.target==mainMenu.IDM_CARDLAYOUT)
		{
			  CardTestFrame cardTestFrame=new CardTestFrame();		
				
		}
		else if(event.target==mainMenu.IDM_COPY)
		{
		}
		else if(event.target==mainMenu.IDM_PASTE)
		{
		}
		else if(event.target==mainMenu.IDM_DELETE)
		{
		}
	    else if(event.target==mainMenu.IDM_SELECTALL)
		{
		}
		else if(event.target==mainMenu.IDM_FIND)
		{
			  FindDialogFrame findDialogFrame=
				  new FindDialogFrame("This Is Find Out Text Dialog Frame");		
		}
		else if(event.target==mainMenu.IDM_GOTO)
		{		
		}
// Main View Menu Content
		else if(event.target==mainMenu.IDM_CHANGESUB)
		{
			subMenu=new IDR_MENU2(this);
		    subMenu.CreateMenu();
		}
		else if((event.target==mainMenu.IDM_COLORMAIN)||
				(event.target==subMenu.IDM_COLORSUB))
		{
			  JavaDemo.frameRed=JavaDemo.appletBackGround.getRed();				
			  JavaDemo.frameGreen=JavaDemo.appletBackGround.getGreen();				
			  JavaDemo.frameBlue=JavaDemo.appletBackGround.getBlue();				
			  ColorDialogFrame colorDialogFrame=
				  new ColorDialogFrame("This Is Modify Window Color Dialog");					
		}
	


// Sub View Menu Content
		else if(event.target==subMenu.IDM_CHANGEMAIN)
		{
			mainMenu=new IDR_MENU1(this);
		    mainMenu.CreateMenu();
		}
// Main Help Menu Content
		else if((event.target==mainMenu.IDM_ABOUT)||
			    (event.target==subMenu.IDM_SUBABOUT))
			{
			  AboutDialogFrame aboutDialogFrame=new AboutDialogFrame();	
			  			
			}
// Sub Help Menu Content
		
		}

		return false;
	}
}

class AboutDialogFrame extends Frame implements Runnable
{
	IDD_DIALOGABOUT aboutDialog;
	Thread aboutThread=null;

	public AboutDialogFrame()
	{
		
		setTitle("This Is Help About Frame Window");
		setCursor(Frame.WAIT_CURSOR);
		setBackground(Color.lightGray);
		setForeground(Color.red);
// set The Dialog and Frame Font, if not setup font, then use default font
//		setFont(new Font("Helvetica",Font.PLAIN,14));	//It is not Used !!	
		pack();

		aboutDialog=new IDD_DIALOGABOUT(this);
        aboutDialog.CreateControls();

		setResizable(false);
// setIconImage()-----It Is Invalid on the plateform of Windows 95
		setIconImage(JavaDemo.m_Images[0]);
		show();
		if(aboutThread==null)
			aboutThread=new Thread(this,"About Thread");
		aboutThread.start();
	}

	public boolean handleEvent(Event evt)
	{
		switch (evt.id)
		{
			// Application shutdown (e.g. user chooses Close from the system menu).
			//------------------------------------------------------------------
			case Event.WINDOW_DESTROY:
				// TODO: Place additional clean up code here
				hide();
				dispose();
				break;
		}	
		return super.handleEvent(evt);
	}
	public boolean action(Event event,Object obj)
	{
		if(event.target==aboutDialog.IDOK)
		{
			hide();
			dispose();
		}
		return false;
	}
	
	public void run()
	{
		Thread.currentThread().setPriority(Thread.MIN_PRIORITY);
		Date todayDate=new Date();
		int minutes=todayDate.getMinutes();
		int hours=todayDate.getHours();
		int seconds=todayDate.getSeconds();
		while(Thread.currentThread()==aboutThread)
		{
			aboutDialog.IDCTIME.setText(
				        todayDate.getHours()+":"
			           +todayDate.getMinutes()+":"	
					   +todayDate.getSeconds());	
			try
			{
				Thread.sleep(1000);
			}
			catch(InterruptedException e)
			{
			}
			seconds++;
			if(seconds>59)
			{
				seconds=0;
				minutes++;
				if(minutes>59)
				{
					minutes=0;
					hours++;
				}
			}
			todayDate.setHours(hours);
			todayDate.setMinutes(minutes);
			todayDate.setSeconds(seconds);
		}
		
	}

	public void paint(Graphics g)
	{
		Dimension rectSize=size();			
		Color foreGround=getForeground();
		g.setColor(Color.red);
		g.draw3DRect(0,0,rectSize.width-1,rectSize.height-1,true);
		g.draw3DRect(3,3,rectSize.width-7,rectSize.height-7,false);
		g.setColor(foreGround);

		g.drawImage(JavaDemo.m_Images[0],
		  	        JavaDemo.m_Images[0].getWidth(this),
					this.preferredSize().height-
					          JavaDemo.m_Images[0].getHeight(this)*2,
					this);
		g.drawImage(JavaDemo.m_Images[1],
		  	        this.preferredSize().width-
					          JavaDemo.m_Images[1].getWidth(this)*2,
					this.preferredSize().height-
					          JavaDemo.m_Images[1].getHeight(this)*2,
			        this);

	}
}

class FindDialogFrame extends Frame implements Runnable 
{
	IDD_FINDDIALOG findDialog;
	Image mapImage;

	Thread findThread=null;
	boolean  m_imageAllLoaded = false;
	Image m_Images[];
	int	  m_nCurrImage;
	final int NUM_IMAGES = 18;
	int   m_nImgWidth  = 0;
	int   m_nImgHeight = 0;
	Graphics m_Graphics;

	public FindDialogFrame(String titleString)
	{
		super(titleString);
		//setTitle("This Is Find out Text About Frame Window");
		setCursor(Frame.CROSSHAIR_CURSOR);
// set The Dialog and Frame Font
		setFont(new Font("Helvetica",Font.PLAIN,14));		
		setBackground(Color.lightGray);
		setForeground(Color.blue);
		pack();

	    int w=100;
		int h=100;
		int pixels[]=new int[w*h];
		int index=0;
		for(int y=0;y<h;y++)
		{
			int red=(y*255)/(h-1);
			for(int x=0;x<w;x++)
			{
				int blue=(x*255)/(w-1);
				pixels[index++]=(255<<24)|(red<<16)|blue;
			}
		}
		mapImage=createImage(new MemoryImageSource(w,h,pixels,0,w));

		findDialog=new IDD_FINDDIALOG(this);
		findDialog.CreateControls();
// set Dialog SubControl initial State value
		findDialog.IDCDOWN.setState(true);	//checkboxGroup
//TextField
		findDialog.IDCFINDTEXT.setText("This Is Yang's Find Out Dialog");
//List(list VJ++ font list)
		String fontList[]=Toolkit.getDefaultToolkit().getFontList();
		for(int i=0;i<fontList.length;i++)
				findDialog.IDCFONT.addItem(fontList[i]);
// Show The Dialog & Its Frame Window
		setResizable(false);
		show();

    	if(findThread==null)
			findThread=new Thread(this,"Find Thread");
		findThread.start();

	}

	public boolean handleEvent(Event evt)
	{
		switch (evt.id)
		{
			// Application shutdown (e.g. user chooses Close from the system menu).
			//------------------------------------------------------------------
			case Event.WINDOW_DESTROY:
				// TODO: Place additional clean up code here
				hide();
				dispose();
				break;
		}	
		return super.handleEvent(evt);
	}
	public boolean action(Event event,Object obj)
	{
		boolean findDirection=true;	// find to down

		boolean matchWhole=false;
		boolean matchCase=false;
		boolean regularExpression=false;
		boolean searchDocument=false;

		String displayString;

		if(event.target==findDialog.IDFINDNEXT)
		{
			System.out.println(findDialog.IDCFINDTEXT.getText());	
				if(findDirection)
					displayString="Down";
				else
					displayString="Up";
				displayString="Find Out Direction Is "+displayString;
			System.out.println(displayString);
				if(matchWhole)
					displayString="Selected";
				else
					displayString="Not Selected";
				displayString="Match Whole Word Only Is "+displayString;
			System.out.println(displayString);
				if(matchCase)
					displayString="Selected";
				else
					displayString="Not Selected";
				displayString="Match Case Is "+displayString;
			System.out.println(displayString);
		}
		else if(event.target==findDialog.IDCANCEL)
		{
			hide();
			dispose();
		}
		else if(event.target==findDialog.IDCUP)
		{
				findDirection=false;	
		}
		else if(event.target==findDialog.IDCDOWN)
		{
				findDirection=true;
		}
		else if(event.target==findDialog.IDCMATCH1)
		{
			if(findDialog.IDCMATCH1.getState())
					matchWhole=true;
			else
					matchWhole=false;
		}
		else if(event.target==findDialog.IDCMATCH2)
		{
			if(findDialog.IDCMATCH2.getState())
					matchCase=true;
			else
					matchCase=false;
		}
		else if(event.target==findDialog.IDCMATCH3)
		{
			if(findDialog.IDCMATCH3.getState())
					regularExpression=true;
			else
					regularExpression=false;
		}
		else if(event.target==findDialog.IDCMATCH4)
		{
			if(findDialog.IDCMATCH4.getState())
					searchDocument=true;
			else
					searchDocument=false;
		}
		else if(event.target==findDialog.IDCFONT)
		{
			findDialog.IDCFINDTEXT.setFont(new Font(
		       (String)event.arg,Font.PLAIN,16));	// event.arg is the Font Name	
			findDialog.IDCFINDTEXT.setText("You Select The "+
				       (String)event.arg+" Font");
		}
		return false;
	}

	public void run()
	{
		Thread.currentThread().setPriority(Thread.MIN_PRIORITY);
		m_nCurrImage = 0;
   		String strImagePathFileName;		
        if (!m_imageAllLoaded)
		{
    		repaint();
    		m_Graphics = getGraphics();
    		m_Images = new Image[NUM_IMAGES];
// Load in all the images
    		MediaTracker tracker = new MediaTracker(this);
    		for (int i = 1; i <= NUM_IMAGES; i++)
    		{
// Build path to next image
    			strImagePathFileName=
					"images\\img00" + ((i < 10) ? "0" : "") + i + ".gif";
				m_Images[i-1] =
					Toolkit.getDefaultToolkit().getImage(strImagePathFileName);
    			tracker.addImage(m_Images[i-1], 0);
    		}
// Wait until all images are fully loaded
			try
			{
				tracker.waitForAll();
				m_imageAllLoaded = !tracker.isErrorAny();
			}
			catch (InterruptedException e)
			{
			}
			
			if (!m_imageAllLoaded)
			{
			    findDialog.IDCFINDTEXT.setText("Error loading images!");
			    return;
			}
			// Assuming all images are same width and height.
		    m_nImgWidth  = m_Images[0].getWidth(this);
		    m_nImgHeight = m_Images[0].getHeight(this);
        }	
		repaint();
		long startTime=System.currentTimeMillis();
		while (true)
		{
			try
			{
// Draw next image in animation
				displayImage(m_Graphics);
				m_nCurrImage++;
				if (m_nCurrImage == NUM_IMAGES)
					m_nCurrImage = 0;
// TODO:  Add additional thread-specific code here
				startTime=startTime+50;
				Thread.sleep(Math.max(0,startTime-System.currentTimeMillis()));
			}
			catch (InterruptedException e)
			{
			}
		}
	}

	public void paint(Graphics g)
	{
		Dimension rectSize=size();			
		Color foreGround=getForeground();
		g.setColor(Color.red);
		g.draw3DRect(0,0,rectSize.width-1,rectSize.height-1,true);
		g.draw3DRect(3,3,rectSize.width-7,rectSize.height-7,false);
		g.setColor(foreGround);

		g.drawImage(mapImage,
		  	        this.preferredSize().width-
					          mapImage.getWidth(this)-20,
					this.preferredSize().height-
					          mapImage.getHeight(this)*2,
					this);
//display Image
		if (m_imageAllLoaded)
		{
			displayImage(g);
		}
		else
		{
		}
	}

⌨️ 快捷键说明

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