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

📄 dictframe.java

📁 简单的英汉词典 用三层结构(界面层、事务处理层、数据层)编辑
💻 JAVA
字号:

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

 /**
  * Homework 5: English-Chinese dictionary interface layer<br>
  *	Date: May. 24, 2007
  *
  * @author Bai Feng
  * @version 1.8
  */
  public class DictFrame extends JFrame {
  	static private String fileName="";// dictionary file
  	private FileDialog fd; // file dialog
  	
  	private Dict dict1;		// use class Dict to build a dictionary
  	
 	private JLabel eng;		// JLabel for English word
 	private JLabel chi;		// JLabel for Chinese word
 	private JLabel counter;	// JLabel for counter 

 	private JTextField engText;// JTextField for English word
 	private JTextField chiText;// JTextField for Chinese word
 	private JTextField counterText;// JTextField for counter
 	
 	// JPanel for choice button: ADD a word, 
 	// or Search a word in the dict.
 	private JPanel choicePanel;
 	private JRadioButton addRadioButton, searchRadioButton;
 	private ButtonGroup choiceButtonGroup;
 	
 	// JButton for the confirm button to start the action
 	private JButton confirmButton;
 	
 	// JButton for load button to load words from a file
 	private JButton loadButton;
 	
 	// JButton for save button to save all the words into a file
 	private JButton saveButton;
 	
 	// JButton for recite button to start reciting words
 	private JButton reciteButton;
 	
 	// JButton for move first button to recite from the beginning
 	private JButton moveFirstButton;
 	
 	// JButton for stat button
 	private JButton statButton;
 	
 	// JButton for list button
 	private JButton listButton;


 	/**
 	 * constructor for DictFrame, initilizing this class.
 	 */
 	public DictFrame() {
 		createUserInterface();	// call createUserInterface method
 		dict1 = new Dict();		// appoint an instance of Dict
 	}
 	
 	/**
 	 * create the user interface
 	 */
 	public void createUserInterface() {

 		// get the content pane
 		Container contentPane = getContentPane();
 		contentPane.setLayout( null );
 		
 		// set up the label and textfield for English word
 		eng = new JLabel( );
 		eng.setText( "English:" );
 		eng.setBounds( 16, 16, 130, 21 );
 		contentPane.add( eng );
 		
 		engText = new JTextField( );
 		engText.setText( "" );
 		engText.setBounds( 70, 16, 100, 21 );
 		contentPane.add( engText );
 		
 		// set up the label and textfield for Chinese word 		
 		chi = new JLabel( "Chinese:" );
 		chi.setBounds( 16, 48, 130, 21 );
 		contentPane.add( chi );
 		
 		chiText = new JTextField( "" );
 		chiText.setBounds( 70, 48, 100, 21 );
 		contentPane.add( chiText );
 		
 		// set up the label and textfield for counter 		
 		counter = new JLabel( "Counter:" );
 		counter.setBounds( 16, 80, 130, 21 );
 		contentPane.add( counter );
 		 		
 		counterText = new JTextField( "" );
 		counterText.setBounds( 70, 80, 100, 21 );
 		counterText.setEditable( false );
 		contentPane.add( counterText );
 		
 		// set up the choice panel
    	choicePanel = new JPanel();
    	choicePanel.setLayout( null );
    	choicePanel.setBorder( new TitledBorder( 
         new EtchedBorder( EtchedBorder.LOWERED ), "OPTION" ) );
    	choicePanel.setBounds( 200, 5, 120, 80 );
    	contentPane.add( choicePanel );
            
    	// set up addRadioButton, and add it into the choice panel
    	addRadioButton = new JRadioButton();
    	addRadioButton.setText( "ADD" );
    	addRadioButton.setBounds( 10, 18, 50, 30 );
    	choicePanel.add( addRadioButton );

    	// set up searchRadioButton, and add it into the choice panel
    	searchRadioButton = new JRadioButton();
    	searchRadioButton.setText( "SEARCH" );
    	searchRadioButton.setBounds( 10, 40, 100, 30 );
    	choicePanel.add( searchRadioButton );
    	
    	// set up a choice button group containing add and search
    	choiceButtonGroup = new ButtonGroup();
    	choiceButtonGroup.add( addRadioButton );
    	choiceButtonGroup.add( searchRadioButton );
 		
 		// set up the Confirm button
		confirmButton = new JButton( "Confirm" ); 	
		confirmButton.setBounds( 20, 120, 100, 30 );	 
		contentPane.add( confirmButton );
		
		// set up the Recite button
		reciteButton = new JButton( "Recite" ); 	
		reciteButton.setBounds( 130, 120, 100, 30 );	 
		contentPane.add( reciteButton );
		
		// set up the move first button
		moveFirstButton = new JButton( "|<--" ); 	
		moveFirstButton.setBounds( 240, 120, 80, 30 );	 
		contentPane.add( moveFirstButton );
		
		// set up the Stat button
		statButton = new JButton( "Stat." ); 	
		statButton.setBounds( 20, 160, 100, 30 );	 
		contentPane.add( statButton );
		
		// set up the List button
		listButton = new JButton( "List" ); 	
		listButton.setBounds( 130, 160, 100, 30 );	 
		contentPane.add( listButton );
		
		// set up the Load button
		loadButton = new JButton( "Load" ); 	
		loadButton.setBounds( 20, 200, 100, 30 );	 
		contentPane.add( loadButton );
		
		// set up the save button
		saveButton = new JButton( "Save" ); 	
		saveButton.setBounds( 130, 200, 100, 30 );	 
		contentPane.add( saveButton );
		
		// call private method to add all the button action listener
		addButtonActionListener(); 		
									
 		// set properties for the application's window 		 		
 		setTitle( "English-Chinese Dictionary" );
 		setSize( 350, 270 );
 		setVisible( true );
 	}	// end of method createUserInterface()
  	
  	private void addButtonActionListener() {
  		
  		// perform confirmButton action event		
		confirmButton.addActionListener( 
			new ActionListener() {		// anonymous inner class
				public void actionPerformed( ActionEvent e ) {
					confirmButtonActionPerformed( e );	// call event solution method				
					}	// end of actionPerformed 
				} // end of ActionListener()
		);	// end of confiremButton ActionListener()
		
		// perform reciteButton action event		
		reciteButton.addActionListener( 
			new ActionListener() {		// anonymous inner class
				public void actionPerformed( ActionEvent e ) {
					reciteButtonActionPerformed( e );	// call event solution method				
					}	
				} 
		);	// end of reciteButton ActionListener()
		
		// perform moveFirst Button action event		
		moveFirstButton.addActionListener( 
			new ActionListener() {		// anonymous inner class
				public void actionPerformed( ActionEvent e ) {
					moveFirstButtonActionPerformed( e );	// call event solution method				
					}	
				} 
		);	// end of moveFirstButton ActionListener()
		
		// perform statButton action event		
		statButton.addActionListener( 
			new ActionListener() {		// anonymous inner class
				public void actionPerformed( ActionEvent e ) {
					statButtonActionPerformed( e );	// call event solution method				
					}	 
				} 
		);	// end of statButton ActionListener()
		
		// perform listButton action event		
		listButton.addActionListener( 
			new ActionListener() {		// anonymous inner class
				public void actionPerformed( ActionEvent e ) {
					listButtonActionPerformed( e );	// call event solution method				
					}	 
				} 
		);	// end of listButton ActionListener()
		
		// perform loadButton action event		
		loadButton.addActionListener( 
			new ActionListener() {		// anonymous inner class
				public void actionPerformed( ActionEvent e ) {
					loadButtonActionPerformed( e );	// call event solution method				
					}	 
				} 
		);	// end of loadButton ActionListener()
		
		// perform saveButton action event		
		saveButton.addActionListener( 
			new ActionListener() {		// anonymous inner class
				public void actionPerformed( ActionEvent e ) {
					saveButtonActionPerformed( e );	// call event solution method				
					}	 
				} 
		);	// end of saveButton ActionListener()
  	}
  	
  	
  	/**
  	 * perform "confirm" button action
  	 */
 	private void confirmButtonActionPerformed( ActionEvent e ) {
		// when the confirm button is clicked
 		if ( e.getActionCommand().equals( "Confirm" ) ) {
			// when the add radio button is chosen						
			if ( addRadioButton.isSelected() ) {
				// call private method addWord()
				addWord();	
			}	// end of if addRadioButton.isSelected()
						
			// when the search radio button is clicked
			else if ( searchRadioButton.isSelected() ) {
				searchChineseWord();
			}	// end of else if searchRadioButton
		} // end of if equals
	}		// end of confirmButtonActionPerformed method
 	
   /**
 	 * add a new word
 	 *
 	 * @param english English word
 	 * @param chinese Chinese word
 	 */
 	private void addWord( ){
		counterText.setText( "" ); 		 		
 		if ( dict1.addWord( getEnglishWord(), getChineseWord() ) == true ) {
 			engText.setText( "word added." );
 			chiText.setText( "单词已添加" );
 		}
 		else {
 			engText.setText( "word not added." );
 			chiText.setText( "单词重复." ); 		
 		}
 	}
 	
 	/** 
 	 * search a word in the dictionary
 	 *
 	 * @param english English word typed
 	 * @return word found
 	 */
 	private void searchChineseWord() {
 		String found;	// Chinese word found
 		
 		counterText.setText( "" );
 		found = dict1.search( getEnglishWord() );
		if ( !found.equals( "" ) ) 
			{	// call private method searchWord()
				chiText.setText( found );
			}
		else 
			chiText.setText( "没找到!" );
 	}
 	 	
 	/**
  	 * perform "Recite" button action
  	 */
 	private void reciteButtonActionPerformed( ActionEvent e ) {
		// the recite button is pressed
		if ( e.getActionCommand().equals( "Recite" ) ) {
			WordItem wi = dict1.recite(); // call recite method
 		
 		    if ( wi == null ) {
 				engText.setText( "Empty dictinary!" );
 				chiText.setText( "字典还是空的!" );
 			}
 			else {
 				engText.setText( wi.getEnglish() );
 				chiText.setText( wi.getChinese() );
 				counterText.setText( Integer.toString( wi.getCounter() ));
 			}
 		} // end if
 	}		// end of reciteButtonActionPerformed method 	
 	 	
 	/**
  	 * perform move first button action<br>
  	 * move the pointter first.
  	 */ 	
    private void moveFirstButtonActionPerformed( ActionEvent e ) {
    	// the "|<--" button is pressed
    	if ( e.getActionCommand().equals( "|<--" ) ) {
    		dict1.moveFirst();
    	} // end if
    } // end of moveFirstButtonActionPerformed method
     	 
   /**
  	 * perform stat button action
  	 */ 	
    private void statButtonActionPerformed( ActionEvent e ) {
    	// the "stat" button is pressed
    	if ( e.getActionCommand().equals( "Stat." ) ) {
    		WordItem mostRecite = dict1.stat();
    		if ( mostRecite == null ) {
 				engText.setText( "Empty dictinary!" );
 				chiText.setText( "字典还是空的!" );
 			}
 			else {
    			engText.setText( mostRecite.getEnglish() );
 				chiText.setText( mostRecite.getChinese() );
 				counterText.setText( Integer.toString( mostRecite.getCounter() ) );
 			}
    	} // end if
    } // end of statButtonActionPerformed method
    
    /**
  	 * perform list button action
  	 */ 	
    private void listButtonActionPerformed( ActionEvent e ) {
    	// the "list" button is pressed
    	if ( e.getActionCommand().equals( "List" ) ) {
    		System.out.println( "English\tChinese\tTimes");
    		System.out.println( dict1.toString() );
 		} // end if
    } // end of listButtonActionPerformed method
       	 	
 	/**
  	 * perform "save" button action
  	 */
 	private void saveButtonActionPerformed( ActionEvent e ) {
		// the save button is pressed
		
	    if ( fileName.equals("") ) {
			fd = new FileDialog( this );
			fd.setMode( FileDialog.SAVE );
			fd.setVisible( true );
			fileName = fd.getFile();
		}
 		dict1.save( fileName ); // call save method
 		
 	}		// end of saveButtonActionPerformed method 	
 	 	
 	/**
  	 * perform "load" button action
  	 */
 	private void loadButtonActionPerformed( ActionEvent e ) {
		// the load button is pressed
		//if ( fileName.equals( "" ) ) {
			fd = new FileDialog( this );
			fd.setMode( FileDialog.LOAD );
			fd.setVisible( true );
			fileName = fd.getFile();
		//}
 		dict1.load( fileName ); // call load method
	}		// end of loadButtonActionPerformed method  
 	 
 	/**
 	 * get the English word
 	 *
 	 * @return English word
 	 */ 	
 	private String getEnglishWord() {
 		return engText.getText();
 	}

 	/**
 	 * get the Chinese word
 	 *
 	 * @return Chinese word
 	 */ 	
 	private String getChineseWord() {
 		return chiText.getText();
 	}

   /**
 	 * main method
 	 *
 	 * @param args command param of main method
 	 * 
 	 */
 	public static void main( String args[] ) {
 		DictFrame dictApplication = new DictFrame();
 		dictApplication.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
 	}	// end of main method

  }	// end of class DictFrame

⌨️ 快捷键说明

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