?? dictionary.java
字號(hào):
import javax.swing.*;
import javax.swing.event.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.*;
import java.io.*;
public class Dictionary extends JFrame implements ActionListener,ListSelectionListener{
//four panels that hold the four parts of the dictionary;
private JPanel upPanel;
private JPanel rightPanel;
private JScrollPane leftPanel;
private JScrollPane centerPanel;
private JPanel bottomPanel;
private JLabel jbCheck; //"單詞查詢(xún)提示語(yǔ)"
private JTextField inputTextField; //prompt user to input words
private JTextField baiduTextField; //enter the things searched will be done by link to www.baidu.com
private JButton enterButton; //be sure about the words
private JButton baiduButton; //the button be used to baidu
private JLabel rightLabel; //be used to beautifulize the GUI
private JLabel baiduLabel; //print out the baidu icon
private JLabel bottomLabel; //set icon in the bottom
private JList listArea; //print out all the word that is near the searched word \
private JTextArea outputArea; //Print out the word's explanation
private JMenuBar jmb; // 菜單欄
private JMenu jmn1; //"文件菜單選項(xiàng)"
private JMenu jmn2; //選項(xiàng)菜單選項(xiàng)
private JMenu jmn3; // "幫助菜單選項(xiàng)"
private JMenuItem jmuItem1; //添加詞匯項(xiàng)目
private JMenuItem jmuItem2; //更換詞庫(kù)項(xiàng)目
private JMenuItem jmuItem3; //退出項(xiàng)目
private JMenuItem jmuItem4; //備份詞庫(kù)項(xiàng)目
private JMenuItem jmuItem5; //關(guān)于項(xiàng)目
//create an array to save the file's data
public static String[] libString=new String[1000000]; //all the words and explanation from the library
//all the explanations to words from the library
private int wordIndex=0;
private String[] nearbyWord; //all the related words
private String inputWord; //the word that that user want to search
private String inputTextToInputWord; //the explanation that will show to the user
public static void main(String[] args) throws FileNotFoundException {
int i=0;
Dictionary dic=new Dictionary();
dic.pack();
dic.setTitle("吳氏小小小詞典(不再寒酸版)");
dic.setLocationRelativeTo(null); //center the frame
dic.setSize(800,600);
dic.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
dic.setVisible(true);
//load the words from the library file
File libFile=new File("library.txt");
if(!libFile.exists()){
System.out.println("The file doesn't exists");
System.exit(0);
}
Scanner libReader=new Scanner(libFile);
//read the data of the file
while(libReader.hasNext()){
Dictionary.libString[i]=libReader.next();
i++;
}
}
//the constructor
public Dictionary(){
// attach a menu to a frame
JMenuBar jmb = new JMenuBar();
Dimension d = new Dimension(30,30);
jmb.setSize(new Dimension( d));
Color jmbBack = new Color(160,216,237);
jmb.setBackground(jmbBack);
// add JMenuBar to the frame
setJMenuBar(jmb);
// create menu item
jmn1 = new JMenu("文件");
jmn2 = new JMenu("選項(xiàng)");
jmn3 = new JMenu("幫助");
jmb.add(jmn1);
jmb.add(jmn2);
jmb.add(jmn3);
// JMenuItem for the 文件
jmuItem1 = new JMenuItem("添加詞匯");
jmuItem2 = new JMenuItem("刪除詞匯");
jmuItem3 = new JMenuItem("退出");
jmn1.add(jmuItem1);
jmn1.add(jmuItem2);
jmn1.add(jmuItem3);
// JMenuItem for the 選項(xiàng)
jmuItem4 = new JMenuItem("備份詞庫(kù)");
jmn2.add(jmuItem4);
jmuItem5 = new JMenuItem("關(guān)于");
jmn3.add(jmuItem5);
// set the frame's layout
setLayout(new BorderLayout());
//the first part of the Dictionary
upPanel=new JPanel();
upPanel.setLayout(new FlowLayout(5));
//locate the jbCheck label;
//make icons!!
ImageIcon icon1=new ImageIcon("search.jpg");
ImageIcon icon2=new ImageIcon("forward.jpg");
ImageIcon icon3=new ImageIcon("baidu.gif");
ImageIcon icon4=new ImageIcon("bg.jpg");
ImageIcon icon5=new ImageIcon("boxiu.gif");
ImageIcon icon6=new ImageIcon("boxiu.gif");
jbCheck=new JLabel("單詞查詢(xún)",SwingConstants.CENTER);
jbCheck.setToolTipText("在右邊欄輸入輸入單詞查詢(xún),");
jbCheck.setIcon(icon2);
jbCheck.setHorizontalAlignment(SwingConstants.CENTER);
jbCheck.setVerticalAlignment(SwingConstants.CENTER);
inputTextField=new JTextField(/*"請(qǐng)?jiān)诖颂庉斎氩樵?xún)的單詞",*/35);
//set the property for the inputTextField
inputTextField.setFont(new Font("Serif",Font.PLAIN,12));
//make the inputTextField exitable
inputTextField.setEditable(true);
//////baidu part
baiduTextField=new JTextField(10);
baiduTextField.setFont(new Font("Serif",Font.PLAIN,12));
baiduTextField.setEditable(true);
enterButton=new JButton("Search");
enterButton.setIcon(icon1);
baiduButton=new JButton("百度一下");
baiduLabel=new JLabel(icon3);
// add componet to the upPanel
upPanel.add(jbCheck);
upPanel.add(inputTextField);
upPanel.add(enterButton);
upPanel.add(baiduLabel);
upPanel.add(baiduTextField);
upPanel.add(baiduButton);
//the second and the third part of the dictionary
listArea=new JList();
listArea.setFixedCellWidth(100);
outputArea=new JTextArea();
leftPanel=new JScrollPane(listArea); //make the textareas can scroll
leftPanel.setSize(50, 500);
centerPanel=new JScrollPane(outputArea);
//the fourth part
rightPanel=new JPanel();
rightLabel=new JLabel(/*"暫時(shí)空缺】\n 以后會(huì)加入圖片等。"*/);
rightLabel.setIcon(icon4);
rightPanel.add(rightLabel);
//the bottom
bottomLabel=new JLabel(icon5);
bottomPanel=new JPanel();
bottomPanel.setLayout(new FlowLayout());
bottomPanel.add(bottomLabel);
//combine the four parts toghter
add(upPanel,BorderLayout.NORTH);
add(rightPanel,BorderLayout.EAST);
add(centerPanel,BorderLayout.CENTER);
add(bottomPanel,BorderLayout.SOUTH);
add(leftPanel,BorderLayout.WEST);
// initial the textfiel
outputArea.setText("歡迎使用該詞典。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。\n*******該詞典由 吳甲城 和 吳龍威 合作完成 !!!!!!!!!!!!!!!");
/* Handle action event from buttons and menu items*/
enterButton.addActionListener( this);
inputTextField.addActionListener(this);
?? 快捷鍵說(shuō)明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號(hào)
Ctrl + =
減小字號(hào)
Ctrl + -