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

📄 hashdialog.java

📁 本软件是使用java 开发的
💻 JAVA
字号:
package datastructure;

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

class HashDialog
    extends JDialog
    implements ActionListener {
  JLabel jLabel1 = new JLabel();
  JTextField jTextField1 = new JTextField();
  JButton jButton1 = new JButton();
  JButton jButton2 = new JButton();
  JButton jButton3 = new JButton();
  static int a[] = {
      4, 53, 19, 99, 61, 58, 28, 9, 44, 75, 11, 88, 80, 12, 70};
  static int m = 20;
  static int p = 8;
  static int key = 28;

  public HashDialog(JFrame frame, String title, boolean mode) {
    super(frame, title, mode);
    try {
      jbInit();
    }
    catch (Exception e) {
      e.printStackTrace();
    }
  }

  private void jbInit() throws Exception {
    setSize(400, 300);
    setLocation(100, 150);
    jLabel1.setText("输入1到100之间的整形数据,用逗号分开. (最大个数为15个)");
    jLabel1.setVerticalTextPosition(SwingConstants.CENTER);
    jLabel1.setBounds(new Rectangle(15, 16, 350, 29));
    this.getContentPane().setLayout(null);
    jTextField1.setText("");
    jTextField1.setBounds(new Rectangle(21, 84, 326, 33));
    jButton1.setBounds(new Rectangle(45, 177, 85, 34));
    jButton1.setText("随机生成");
    jButton1.setToolTipText("随机生成演示数据");
    jButton2.setBounds(new Rectangle(160, 177, 79, 38));
    jButton2.setText("确定");
    jButton3.setBounds(new Rectangle(267, 178, 86, 37));
    jButton3.setText("取消");
    jButton1.addActionListener(this);
    jButton2.addActionListener(this);
    jButton3.addActionListener(this);
    this.getContentPane().add(jLabel1, null);
    this.getContentPane().add(jTextField1, null);
    this.getContentPane().add(jButton1, null);
    this.getContentPane().add(jButton2, null);
    this.getContentPane().add(jButton3, null);
  }

  public void actionPerformed(ActionEvent e) {
    if (e.getSource() == jButton1) {
      int number = (int) (Math.random() * 2 + 13);
      String s = "";
      int a[] = new int[number];
      int tag = 0;
      boolean flag = false;
      for (int i = 0; i < a.length; i++)
        a[i] = 0;
      for (int i = 0; i < number; i++) {
        int p = (int) (Math.random() * 99 + 1);
        for (int j = 0; j < tag; j++) {
          if (p == a[j])
            flag = true;
          else
            flag = false;
        }
        a[tag] = p;
        tag++;
        if (!flag) {
          if (i != number - 1)
            s = s + String.valueOf(p) + ",";
          else
            s = s + String.valueOf(p);
        }
      }
      jTextField1.setText(s);
    }
    else if (e.getSource() == jButton2) {
      //用于表示数组a的下标
      int tag1 = 0;
      String s = jTextField1.getText().trim();
      if (s.equals("")) {
        JOptionPane.showConfirmDialog(this,
                                      "   演示数据不能为空\n \n  请从新输入正确的数据\n",
                                      "出错信息", JOptionPane.DEFAULT_OPTION);
        return;
      }
      StringTokenizer str = new StringTokenizer(s, " ,");
      int number = str.countTokens();
      a = new int[number];
      while (str.hasMoreTokens()) {
        String s1 = str.nextToken();
        try {
          a[tag1] = Integer.parseInt(s1);
          if (a[tag1] <= 0 || a[tag1] > 99) {
            JOptionPane.showConfirmDialog(this,
                                          "   输入的数据超出范围\n \n  请从新输入正确的数据\n",
                                          "出错信息", JOptionPane.DEFAULT_OPTION);

            return;

          }

          tag1++;
        }
        catch (NumberFormatException e1) {
          JOptionPane.showConfirmDialog(this,
                                        "   输入的数据不合法\n \n  请从新输入正确的数据\n",
                                        "出错信息", JOptionPane.DEFAULT_OPTION);

          return;
        }
      }
      if (tag1 >= 15) {
        JOptionPane.showConfirmDialog(this,
                                      "   输入的节点个数大于15个\n \n  请从新输入正确的数据\n",
                                      "出错信息", JOptionPane.DEFAULT_OPTION);

        return;

      }
      setVisible(false);
      boolean flag1 = true;
      while (flag1) {
        try {
          m = Integer.parseInt(JOptionPane.showInputDialog(this,
              " 输入哈希表的长度(表长要大于" +
              String.valueOf(tag1) +
              "小于20)", "输入哈希表长度",
              JOptionPane.DEFAULT_OPTION));
          if (m <= tag1 || m > 20) {
            JOptionPane.showConfirmDialog(this,
                                          "   输入的哈希表长度不符合要求\n \n  请从新输入正确的数据\n",
                                          "出错信息", JOptionPane.DEFAULT_OPTION);

          }
          else
            flag1 = false;

        }
        catch (NumberFormatException ee) {
          JOptionPane.showConfirmDialog(this,
                                        "   输入的哈希表长度不符合要求\n \n  请从新输入正确的数据\n",
                                        "出错信息", JOptionPane.DEFAULT_OPTION);

          flag1 = true;
        }
      }
      flag1 = true;
      while (flag1) {
        try {
          p = Integer.parseInt(JOptionPane.showInputDialog(this,
              " 输入正整数p(p要满足小于或等于哈希表的长度" +
              String.valueOf(m) +
              ")", "输入数值p",
              JOptionPane.DEFAULT_OPTION));
          if (p <= 0 || p > m) {
            JOptionPane.showConfirmDialog(this,
                                          "   输入的数据p不符合要求\n \n  请从新输入正确的数据\n",
                                          "出错信息", JOptionPane.DEFAULT_OPTION);

          }
          else
            flag1 = false;

        }
        catch (NumberFormatException ee) {
          JOptionPane.showConfirmDialog(this,
                                        "   输入的数据p不符合要求\n \n  请从新输入正确的数据\n",
                                        "出错信息", JOptionPane.DEFAULT_OPTION);

          flag1 = true;
        }
      }
      flag1 = true;
      while (flag1) {
        try {
          key = Integer.parseInt(JOptionPane.showInputDialog(this,
              " 输入查找元素KEY(KEY小于100)", "输入数值KEY",
              JOptionPane.DEFAULT_OPTION));
          if (key <= -9 || key >= 100)
            JOptionPane.showConfirmDialog(this,
                                          "   输入的数据KEY不符合要求\n \n  请从新输入正确的数据\n",
                                          "出错信息", JOptionPane.DEFAULT_OPTION);

          else
            flag1 = false;

        }
        catch (NumberFormatException ee) {
          JOptionPane.showConfirmDialog(this,
                                        "   输入的数据KEY不符合要求\n \n  请从新输入正确的数据\n",
                                        "出错信息", JOptionPane.DEFAULT_OPTION);

          flag1 = true;
        }
      }

    }
    else {
      setVisible(false);
    }
  }
}

⌨️ 快捷键说明

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