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

📄 frame1.java

📁 JAVA课程设计!用线程来实现比较查找、排序算法的运行时间
💻 JAVA
字号:
package myjava;

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import com.borland.jbcl.layout.*;

/**
 * <p>Title: JAVA课程设计</p>
 * <p>Description: 第三题</p>
 * <p>Copyright: Copyright (c) 2005</p>
 * @author 温小林
 * @version 1.0
 */
class find extends Thread
         {
                 JTextArea fjt;
                 JTextArea object;
                 int find;
                 int option;
                 int[] sz;
                 public find(JTextArea fjt,JTextArea object,int find,int option)
                 {

                         this.fjt=fjt;
                         this.find=find;
                         this.option=option;
                         this.object=object;
                         String[] temp=fjt.getText().split(",");
                                 this.sz=new int[temp.length];
                                 for(int i=0;i<temp.length;i++)
                                         sz[i]=Integer.parseInt(temp[i]);

                 }
                   public int Seq(int[] a,int key)		//顺序查找//
              {
                      int i;
                      for(i=0;i<50000000;i++)
                      {
                      //
                      }
                      for(i=0;i<a.length;i++)
                 {
                         if(a[i]==key)
                                 break;
                 }
                 if(i==a.length)
                         return -1;
                 else
                         return i;
              }
                  public int half(int[] a,int key)	//折半查找
          // c为有序数列,a、b分别为查找区间的起点和终点,key为查找关键字 //
             {
                          int start=0;
                          int end=a.length-1;
                          int result=0;
                          int i;
                          int mid=(end-start)/2;
                          for(i=0;i<50000000;i++)
                         {
                           //
                         }

                          while(end!=start)
                          {
                                  if(a[mid]==key||a[start]==key||a[end]==key)
                                          break;
                                  if(a[mid]>key)
                                  {
                                          end=mid;
                                          mid=(end-start)/2+start;
                                  }else
                                  {
                                          start=mid;
                                          mid=(end-start)/2+start;
                                  }

                          }

                          if(start==end)
                                  return -1;
                          else
                                  return mid;
             }
                 public void run()
                 {
                         long begin = System.currentTimeMillis();
                         if(option==1)
                         {
                                 int pos=Seq(sz,find);
                                 if(pos==-1)
                                   object.append("所要查询的关键字未找到!");
                                 else
                                   object.append("找到:"+find+"   所在位置:"+pos);
                         }else
                         {
                                 int swap;
                                 for(int i=0;i<sz.length-1;i++)
                                         for(int j=i+1;j<sz.length;j++)
                                         {
                                                 if(sz[i]>sz[j])
                                                 {
                                                         swap=sz[i];
                                                         sz[i]=sz[j];
                                                         sz[j]=swap;
                                                 }
                                         }
                                 begin = System.currentTimeMillis();

                                 int pos=half(sz,find);
                                 if(pos==-1)
                                         object.append("所要查询的关键字未找到!");
                                 else
                                         object.append("找到:"+find+"   所在位置:"+pos);
                         }
                         long end = System.currentTimeMillis();
                         object.append("\n用时:"+String.valueOf(end-begin));
                 }
         }

public class Frame1 extends JFrame {
  JPanel contentPane;
  XYLayout xYLayout1 = new XYLayout();
  JLabel jLabel1 = new JLabel();
  JTextArea jTextArea4 = new JTextArea();
  JLabel jLabel2 = new JLabel();
  JTextArea jTextArea1 = new JTextArea();
  JLabel jLabel3 = new JLabel();
  JLabel jLabel4 = new JLabel();
  JTextArea jTextArea2 = new JTextArea();
  JLabel jLabel5 = new JLabel();
  JLabel jLabel6 = new JLabel();
  JTextField jTextField2 = new JTextField();
  JButton jButton1 = new JButton();
  JButton jButton2 = new JButton();
  JButton jButton3 = new JButton();

  //Construct the frame
  public Frame1() {
    enableEvents(AWTEvent.WINDOW_EVENT_MASK);
    try {
      jbInit();
    }
    catch(Exception e) {
      e.printStackTrace();
    }
  }

  //Component initialization
  private void jbInit() throws Exception  {
    contentPane = (JPanel) this.getContentPane();
    jLabel1.setFont(new java.awt.Font("Dialog", 0, 12));
    jLabel1.setDoubleBuffered(false);
    jLabel1.setPreferredSize(new Dimension(159, 18));
    jLabel1.setRequestFocusEnabled(true);
    jLabel1.setDisplayedMnemonic('0');
    jLabel1.setText("请输入数组。并用“逗号”分割!");
    contentPane.setLayout(xYLayout1);
    this.setResizable(false);
    this.setSize(new Dimension(403, 295));
    this.setState(Frame.NORMAL);
    this.setTitle("第三题");
    contentPane.setMaximumSize(new Dimension(2147483647, 2147483647));
    jTextArea4.setBorder(BorderFactory.createLineBorder(Color.black));
    jTextArea4.setText("12,45,36,68,29,17,10,32,30,10,50,96,48,67,30,126,965,41,43,48");
    jLabel2.setFont(new java.awt.Font("Dialog", 0, 12));
    jLabel2.setText("顺序排序算法");
    jTextArea1.setBackground(Color.white);
    jTextArea1.setAlignmentX((float) 1.0);
    jTextArea1.setAlignmentY((float) 1.0);
    jTextArea1.setBorder(BorderFactory.createLineBorder(Color.black));
    jTextArea1.setText("系统等待中。。。");
    jTextArea1.setTabSize(8);
    jLabel3.setText("....................................................................................................." +
    ".....................");
    jLabel4.setFont(new java.awt.Font("Dialog", 0, 12));
    jLabel4.setToolTipText("");
    jLabel4.setText("折半查找算法(先排序后查找)");
    jTextArea2.setBorder(BorderFactory.createLineBorder(Color.black));
    jTextArea2.setText("系统等待中。。。");
    jLabel5.setFont(new java.awt.Font("Dialog", 0, 12));
    jLabel5.setPreferredSize(new Dimension(121, 16));
    jLabel5.setRequestFocusEnabled(true);
    jLabel5.setText("请输入您要查找的数字:");
    jLabel6.setText("....................................................................................................." +
    ".....................");
    jTextField2.setFont(new java.awt.Font("Dialog", 0, 12));
    jTextField2.setBorder(BorderFactory.createLineBorder(Color.black));
    jTextField2.setText("");
    jTextField2.addActionListener(new Frame1_jTextField2_actionAdapter(this));
    jButton1.setFont(new java.awt.Font("Dialog", 0, 12));
    jButton1.setBorder(BorderFactory.createLineBorder(Color.black));
    jButton1.setText("确  定");
    jButton1.addActionListener(new Frame1_jButton1_actionAdapter(this));
    jButton2.setText("退  出");
    jButton2.addMouseListener(new Frame1_jButton2_mouseAdapter(this));
    jButton2.setBorder(BorderFactory.createLineBorder(Color.black));
    jButton2.setActionCommand("确  定");
    jButton2.setFont(new java.awt.Font("Dialog", 0, 12));
    jButton3.setFont(new java.awt.Font("Dialog", 0, 12));
    jButton3.setBorder(BorderFactory.createLineBorder(Color.black));
    jButton3.setToolTipText("");
    jButton3.setText("产生随机数");
    jButton3.addActionListener(new Frame1_jButton3_actionAdapter(this));
    contentPane.add(jTextArea1, new XYConstraints(16, 86, 367, 41));
    contentPane.add(jTextArea4, new XYConstraints(17, 25, 366, 22));
    contentPane.add(jLabel1, new XYConstraints(16, 5, 180, 20));
    contentPane.add(jLabel4,  new XYConstraints(17, 137, 326, 19));
    contentPane.add(jTextArea2, new XYConstraints(16, 157, 365, 49));
    contentPane.add(jLabel6, new XYConstraints(15, 212, 376, 13));
    contentPane.add(jLabel2,             new XYConstraints(17, 65, 184, 21));
    contentPane.add(jLabel5,            new XYConstraints(15, 230, 137, 25));
    contentPane.add(jTextField2,        new XYConstraints(149, 232, 55, 18));
    contentPane.add(jButton2,  new XYConstraints(312, 230, 68, 22));
    contentPane.add(jButton1, new XYConstraints(230, 230, 68, 22));
    contentPane.add(jLabel3, new XYConstraints(16, 49, 376, 13));
    contentPane.add(jButton3,       new XYConstraints(279, 5, 104, 16));
  }

  //Overridden so we can exit when window is closed
  protected void processWindowEvent(WindowEvent e) {
    super.processWindowEvent(e);
    if (e.getID() == WindowEvent.WINDOW_CLOSING) {
      System.exit(0);
    }
  }

  void jButton2_mouseReleased(MouseEvent e) {
System.exit(0);
  }

  void jButton1_actionPerformed(ActionEvent e) {
    try
    {
      if (jTextField2.getText().equals("")) {
        System.out.println("错误,你没有输入要查找的关键字");
      }
      else {
        String str = jTextField2.getText();
        jTextArea1.setText("");
        jTextArea2.setText("");
        find fd1 = new find(jTextArea4, jTextArea1, Integer.parseInt(str), 1);
        fd1.start();
        find fd2 = new find(jTextArea4, jTextArea2, Integer.parseInt(str), 2);
        fd2.start();

      }
    }
    catch(NumberFormatException er)
                        {
                                System.out.println("操作错误! ");
                        }
return;
  }

  void jTextField2_actionPerformed(ActionEvent e) {

  }

  void jButton3_actionPerformed(ActionEvent e) {
    jTextArea4.setText("");
    int table[]=new int [20];
    int i;
    for(i=0;i<table.length;i++)
      table[i]=(int)(Math.random()*100);
    for(i=0;i<table.length;i++)
    {
    jTextArea4.append(table[i]+",");
    }

  }
}

class Frame1_jButton2_mouseAdapter extends java.awt.event.MouseAdapter {
  Frame1 adaptee;

  Frame1_jButton2_mouseAdapter(Frame1 adaptee) {
    this.adaptee = adaptee;
  }
  public void mouseReleased(MouseEvent e) {
    adaptee.jButton2_mouseReleased(e);
  }
}

class Frame1_jButton1_actionAdapter implements java.awt.event.ActionListener {
  Frame1 adaptee;

  Frame1_jButton1_actionAdapter(Frame1 adaptee) {
    this.adaptee = adaptee;
  }
  public void actionPerformed(ActionEvent e) {
    adaptee.jButton1_actionPerformed(e);
  }

}

class Frame1_jTextField2_actionAdapter implements java.awt.event.ActionListener {
  Frame1 adaptee;

  Frame1_jTextField2_actionAdapter(Frame1 adaptee) {
    this.adaptee = adaptee;
  }
  public void actionPerformed(ActionEvent e) {
    adaptee.jTextField2_actionPerformed(e);
  }
}

class Frame1_jButton3_actionAdapter implements java.awt.event.ActionListener {
  Frame1 adaptee;

  Frame1_jButton3_actionAdapter(Frame1 adaptee) {
    this.adaptee = adaptee;
  }
  public void actionPerformed(ActionEvent e) {
    adaptee.jButton3_actionPerformed(e);
  }
}

⌨️ 快捷键说明

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