import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class WorkDemo extends JFrame{
private JButton randButton,searchButton,qSortButton;
private JTextField randNumText,outputText,inputText;
private JTextArea qSortText;
private JLabel inputLabel,outputLabel,qSortLabel;
private int[] num;
public WorkDemo()
{
super("课后作业的演示程序");
num=new int[50];
randButton=new JButton("随即产生20个[1,50]的数");
searchButton=new JButton("二分法查找");
randNumText=new JTextField(30);
outputText=new JTextField(30);
inputText=new JTextField(3);
inputLabel=new JLabel("输入你要查询的数值:");
outputLabel=new JLabel("你查询的结果为:");
qSortLabel=new JLabel("快速排序的序列:5 7 1 2 9 4 3 6 8");
qSortButton=new JButton("快速排序");
qSortText=new JTextArea(15,30);
//Layout
JPanel randPanel=new JPanel();
randPanel.add(randButton);
randPanel.add(randNumText);
JPanel inputPanel=new JPanel();
inputPanel.add(inputLabel);
inputPanel.add(inputText);
JPanel searchPanel=new JPanel();
searchPanel.add(searchButton);
searchPanel.add(outputText);
JPanel qSortPanel1=new JPanel();
qSortPanel1.add(qSortLabel);
qSortPanel1.add(qSortButton);
JPanel qSortPanel2=new JPanel();
qSortPanel2.add(qSortText);
Container container=getContentPane();
container.setLayout(new GridLayout(3,2));
container.add(randPanel);
container.add(qSortPanel1);
container.add(inputPanel);
container.add(qSortPanel2);
container.add(searchPanel);
// container.add(outputPanel);
// container.add(qSortPanel3);
randButton.addActionListener(
new ActionListener()
{
public void actionPerformed(ActionEvent event)
{
String str=new String();
RandomNumber randNum=new RandomNumber();
num=randNum.getRandomNum();
//从小到大排序bubbleSort for BinarySort
for(int i=20-1;i>=0;i--)
for(int j=0;j<i;j++)
{ if(num[j]>num[j+1])
{
int temp=num[j];
num[j]=num[j+1];
num[j+1]=temp;
}
}
for(int i=0;i<20;i++)
{
str+=num[i]+" ";
}
randNumText.setText(str);}}
);
searchButton.addActionListener(
new ActionListener()
{
public void actionPerformed(ActionEvent event)
{
String str=new String();
BinarySearch binSearch=new BinarySearch();
if(binSearch.search(num,Integer.parseInt(inputText.getText()))<0)
outputText.setText("所查数不在序列中!");
else
{
str+=binSearch.search(num,Integer.parseInt(inputText.getText()));
outputText.setText("所查数在序列中第"+str+"位置");
}
}}
);
qSortButton.addActionListener(
new ActionListener()
{
public void actionPerformed(ActionEvent event)
{
int[] demoNum={5,7,1,2,9,4,3,6,8};
QSort qs=new QSort();
qs.QuickSort(demoNum,0,demoNum.length-1);
String str=new String();
str=qs.toString();
qSortText.setText(str);
}
});
pack();
setVisible(true);
}
public static void main(String[] args)
{
/*int[] demoNum={9,8,7,6,5,4,3,2,1};
QSort qs=new QSort();
qs.QuickSort(demoNum,0,demoNum.length-1);
String str=new String();
*/WorkDemo application=new WorkDemo();
application.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}