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

📄 wordfinder.java

📁 简易词典
💻 JAVA
字号:
import java.awt.*;
import java.io.*;
import java.util.*;

import javax.swing.*;

public class WordFinder extends JFrame
{
     private Container contentPane;
     private JPanel northPane = new JPanel();
     private JPanel centerPane = new JPanel();
     private JPanel westPane = new JPanel();
     private JPanel eastPane = new JPanel();
     private JLabel find_L = new JLabel();
     private JLabel message_L = new JLabel();
     private JTextField search_TF = new JTextField();
     private JButton clear_B = new JButton();
     private JList wordList = new JList();
     private JMenuBar menuBar = new JMenuBar();
     private JMenu fileMenu = new JMenu();
     private JMenuItem openItem = new JMenuItem();
     private JMenuItem exitItem = new JMenuItem();
     private WordList dictionary;

     public WordFinder()
     {
          try {
               dictionary = new WordList();
               jbInit();
          } catch(IOException ioe) {
               ioe.printStackTrace();
          } catch(Exception ex) {
               ex.printStackTrace();
          }
     }

     private void jbInit() throws Exception
     {
          this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
          this.setTitle("Word Finder"); // 设置窗口标题
          this.setSize(new Dimension(400, 270)); // 设置窗口大小
          Dimension scrSize = this.getToolkit().getScreenSize();
          this.setLocation((scrSize.width - this.getWidth()) / 2, // 设置窗口位置
                           (scrSize.height - this.getHeight()) / 2);

          fileMenu.setText("File");
          openItem.setText("Open...");
          openItem.addActionListener(new AL_openItem(this, dictionary));
          exitItem.setText("Exit");
          exitItem.addActionListener(new AL_exitItem(this));
          message_L.setMaximumSize(new Dimension(320, 20));
          message_L.setMinimumSize(new Dimension(320, 20));
          message_L.setPreferredSize(new Dimension(320, 20));

          fileMenu.add(openItem);
          fileMenu.add(exitItem);
          menuBar.add(fileMenu);
          menuBar.setBackground(Color.lightGray);
          this.setJMenuBar(menuBar);

          contentPane = this.getContentPane(); // 获取帧的内容面板

          find_L.setText("Find:");
          find_L.setFont(new Font("Dialog", Font.BOLD, 14));
          search_TF.setColumns(10);
          search_TF.getDocument().addDocumentListener(new DL_searchTF(this, dictionary));

          clear_B.setText("    Clear    ");
          clear_B.setFont(new Font("Dialog", Font.BOLD, 14));
          clear_B.setMaximumSize(new Dimension(73, 30));
          clear_B.setMinimumSize(new Dimension(73, 30));
          clear_B.setPreferredSize(new Dimension(73, 30));
          clear_B.setBackground(Color.lightGray);
          clear_B.setBorder(BorderFactory.createEtchedBorder()); // 设置边框
          clear_B.addActionListener(new AL_clearButton(this)); // 添加侦听器

          northPane.setBackground(Color.lightGray);
          northPane.setLayout(new BoxLayout(northPane, BoxLayout.LINE_AXIS));
          northPane.add(Box.createRigidArea(new Dimension(5, 0))); // 添加水平空白
          northPane.add(find_L);
          northPane.add(search_TF);
          northPane.add(clear_B);
          northPane.add(Box.createRigidArea(new Dimension(5, 0))); // 添加水平空白

          message_L.setText("0 words total");
          message_L.setFont(new Font("Dialog", Font.BOLD, 14));

          wordList.setPrototypeCellValue("Char");
          wordList.setListData(dictionary.getDictionary()); // 显示词典
          setTotal(dictionary.getDictionarySize());

          centerPane.setBackground(Color.lightGray);
          centerPane.setLayout(new BoxLayout(centerPane, BoxLayout.Y_AXIS));
          centerPane.add(Box.createRigidArea(new Dimension(0, 5))); // 添加顶部垂直空白
          centerPane.add(message_L);
          centerPane.add(new JScrollPane(wordList)); // 添加列表框
          centerPane.add(Box.createRigidArea(new Dimension(0, 10))); // 添加底部垂直空白

          westPane.setBackground(Color.lightGray);
          westPane.setLayout(new BoxLayout(westPane, BoxLayout.X_AXIS));
          westPane.add(Box.createRigidArea(new Dimension(40, 0))); // 添加水平空白

          eastPane.setBackground(Color.lightGray);
          eastPane.setLayout(new BoxLayout(eastPane, BoxLayout.X_AXIS));
          eastPane.add(Box.createRigidArea(new Dimension(77, 0))); // 添加水平空白

          contentPane.add(northPane, BorderLayout.NORTH);
          contentPane.add(centerPane, BorderLayout.CENTER);
          contentPane.add(westPane, BorderLayout.WEST);
          contentPane.add(eastPane, BorderLayout.EAST);
     }

     public File openFile()
     {
          JFileChooser fc = new JFileChooser();
          int returnVal = fc.showOpenDialog(this); // 显示文件选择器
          if(returnVal == JFileChooser.APPROVE_OPTION) { // 选择了文件
               File theFile = fc.getSelectedFile();
               return theFile;
          }
          // 没有选择任何文件
          return null;
     }

     public void clearSearch_TF()
     {
          search_TF.setText("");
     }

     public String getInput()
     {
          return search_TF.getText();
     }

     public void setList(Vector<String> words)
     {
          wordList.setListData(words);
     }

     public void setTotal(int amount)
     {
          message_L.setText(amount + " words total");
     }

     public void setStatistic(int amount)
     {
          message_L.setText(amount + " words containing '" + getInput() + "'");
     }
}

⌨️ 快捷键说明

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