📄 dictionary.java
字号:
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; //"单词查询提示语"
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; //"文件菜单选项"
private JMenu jmn2; //选项菜单选项
private JMenu jmn3; // "帮助菜单选项"
private JMenuItem jmuItem1; //添加词汇项目
private JMenuItem jmuItem2; //更换词库项目
private JMenuItem jmuItem3; //退出项目
private JMenuItem jmuItem4; //备份词库项目
private JMenuItem jmuItem5; //关于项目
//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("选项");
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 选项
jmuItem4 = new JMenuItem("备份词库");
jmn2.add(jmuItem4);
jmuItem5 = new JMenuItem("关于");
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("单词查询",SwingConstants.CENTER);
jbCheck.setToolTipText("在右边栏输入输入单词查询,");
jbCheck.setIcon(icon2);
jbCheck.setHorizontalAlignment(SwingConstants.CENTER);
jbCheck.setVerticalAlignment(SwingConstants.CENTER);
inputTextField=new JTextField(/*"请在此处输入查询的单词",*/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(/*"暂时空缺】\n 以后会加入图片等。"*/);
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);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -