📄 sortstarter.java
字号:
// SortStarter.java// Class that allows the program to run under an applet.import java.awt.*;import java.awt.event.*;import java.applet.*;public class SortStarter extends Applet implements ActionListener, ItemListener, FocusListener{ public static final byte BUBBLE = 0, SELECT = 1, INSERT = 2, SHELL = 3, QUICK = 4, MERGE = 5, HEAP = 6, OETRANS = 7, SHEAR = 8; public static final int MAXLENGTH = 25; public SortInfo[] sortInfo; private String[] strChoices = {"Bubble Sort", "Selection Sort", "Insertion Sort", "Shellsort", "Quicksort", "Mergesort", "Heapsort", "O-E Transposition Sort", "Shearsort"}, lengthChoices = {"5", "10", "15", "20", "25", "50", "100", "200", "500", "1000", "2000"}, arrayChoices = {"Random", "Sorted", "Reverse sorted"}; private static final byte RANDOM = 0, SORTED = 1, REVERSE = 2; private Button start, compare, generate; private Choice sortChoice, lengthChoice, arrayChoice; private Checkbox importBox, specialBox; private TextField arrayField; private BubbleSortApp bsa; private SelectionSortApp ssa; private InsertionSortApp isa; private ShellSortApp shsa; private QuickSortApp qsa; private MergeSortApp msa; private HeapSortApp hsa; private OETransSortApp oetsa; private ShearSortApp shrsa; private CompareTable table; private int typeOfSort; private int[] A = null, copySpecialArray = null; private String importStr, specialStr; public void init() { setBGColor(getParameter("background")); GridBagConstraints gbc = new GridBagConstraints(); typeOfSort = BUBBLE; sortInfo = new SortInfo[strChoices.length]; for(int i = 0; i < strChoices.length; i++) sortInfo[i] = new SortInfo(); sortChoice = new Choice(); sortChoice.addItemListener(this); for(int i = 0; i < strChoices.length; i++) sortChoice.add(strChoices[i]); lengthChoice = new Choice(); lengthChoice.addItemListener(this); for(int i = 0; i < lengthChoices.length; i++) lengthChoice.add(lengthChoices[i]); lengthChoice.setEnabled(false); arrayChoice = new Choice(); arrayChoice.addItemListener(this); for(int i = 0; i < arrayChoices.length; i++) arrayChoice.add(arrayChoices[i]); arrayChoice.setEnabled(false); start = new Button(" Start Sorting Demo "); start.addActionListener(this); compare = new Button("Compare Algorithms"); compare.addActionListener(this); generate = new Button(" Regenerate Array "); generate.addActionListener(this); generate.setEnabled(false); Panel topLeftPanel = new Panel(); topLeftPanel.setLayout(new FlowLayout()); topLeftPanel.add(sortChoice); CheckboxGroup cbGroup = new CheckboxGroup(); importBox = new Checkbox("User-Defined", cbGroup, true); importBox.addItemListener(this); specialBox = new Checkbox("Special-Case", cbGroup, false); specialBox.addItemListener(this); Panel cbPanel = new Panel(); cbPanel.setLayout(new FlowLayout()); cbPanel.add(importBox); cbPanel.add(specialBox); Panel topPanel = new Panel(); topPanel.setLayout(new GridLayout(1, 2)); topPanel.add(new Box(topLeftPanel, "Featured Algorithms", Box.LEFT)); topPanel.add(new Box(cbPanel, "Array Type", Box.LEFT)); arrayField = new TextField(50); arrayField.addFocusListener(this); Panel centerPanel = new Panel(); centerPanel.setLayout(new GridBagLayout()); gbc.insets = new Insets(0, 0, 0, 0); gbc.anchor = GridBagConstraints.WEST; gbc.gridwidth = 1; gbc.weightx = 0; gbc.fill = GridBagConstraints.NONE; centerPanel.add(new Label("Array:", Label.RIGHT), gbc); gbc.insets = new Insets(5, 5, 5, 10); gbc.anchor = GridBagConstraints.CENTER; gbc.gridwidth = GridBagConstraints.REMAINDER; gbc.weightx = 100; gbc.fill = GridBagConstraints.HORIZONTAL; centerPanel.add(arrayField, gbc); gbc.insets = new Insets(5, 0, 10, 0); gbc.anchor = GridBagConstraints.WEST; gbc.gridwidth = 1; gbc.weightx = 0; gbc.fill = GridBagConstraints.NONE; centerPanel.add(new Label("Type:", Label.RIGHT), gbc); gbc.insets = new Insets(5, 5, 10, 5); gbc.anchor = GridBagConstraints.WEST; gbc.gridwidth = 1; gbc.weightx = 0; gbc.fill = GridBagConstraints.NONE; centerPanel.add(arrayChoice, gbc); gbc.insets = new Insets(5, 0, 10, 0); gbc.anchor = GridBagConstraints.WEST; gbc.gridwidth = 1; gbc.weightx = 0; gbc.fill = GridBagConstraints.NONE; centerPanel.add(new Label("Length:", Label.RIGHT), gbc); gbc.insets = new Insets(5, 5, 10, 5); gbc.anchor = GridBagConstraints.WEST; gbc.gridwidth = 1; gbc.weightx = 100; gbc.fill = GridBagConstraints.HORIZONTAL; centerPanel.add(lengthChoice, gbc); gbc.insets = new Insets(5, 10, 10, 10); gbc.anchor = GridBagConstraints.EAST; gbc.gridwidth = GridBagConstraints.REMAINDER; gbc.weightx = 0; gbc.fill = GridBagConstraints.NONE; centerPanel.add(generate, gbc); Panel bottomPanel = new Panel(); bottomPanel.setLayout(new GridBagLayout()); gbc.insets = new Insets(10, 0, 0, 5); gbc.anchor = GridBagConstraints.EAST; gbc.gridwidth = 1; gbc.weightx = 100; gbc.fill = GridBagConstraints.NONE; bottomPanel.add(start, gbc); gbc.insets = new Insets(10, 5, 0, 0); gbc.anchor = GridBagConstraints.EAST; gbc.gridwidth = GridBagConstraints.REMAINDER; gbc.weightx = 0; gbc.fill = GridBagConstraints.NONE; bottomPanel.add(compare, gbc); Panel mainPanel = new Panel(); mainPanel.setLayout(new BorderLayout()); mainPanel.add("North", topPanel); mainPanel.add("Center", new Box(centerPanel, "Array Data", Box.LEFT)); mainPanel.add("South", bottomPanel); Panel thePanel = new Panel(); thePanel.setLayout(new GridBagLayout()); thePanel.setBackground(SystemColor.activeCaptionBorder); gbc.insets = new Insets(0, 7, 7, 7); gbc.anchor = GridBagConstraints.CENTER; gbc.gridwidth = GridBagConstraints.REMAINDER; gbc.weightx = 100; gbc.fill = GridBagConstraints.NONE; thePanel.add(mainPanel, gbc); setLayout(new GridBagLayout()); gbc.insets = new Insets(0, 0, 0, 0); gbc.anchor = GridBagConstraints.SOUTH; gbc.gridwidth = GridBagConstraints.REMAINDER; gbc.weightx = 100; gbc.fill = GridBagConstraints.NONE; add(thePanel, gbc); validate(); } public void close() { switch(typeOfSort) { case BUBBLE: bsa.setVisible(false); bsa.dispose(); bsa = null; break; case SELECT: ssa.setVisible(false); ssa.dispose(); ssa = null; break; case INSERT: isa.setVisible(false); isa.dispose(); isa = null; break; case SHELL: shsa.setVisible(false); shsa.dispose(); shsa = null; break; case QUICK: qsa.setVisible(false); qsa.dispose(); qsa = null; break; case MERGE: msa.setVisible(false); msa.dispose(); msa = null; break; case HEAP: hsa.setVisible(false); hsa.dispose(); hsa = null; break; case OETRANS: oetsa.setVisible(false); oetsa.dispose(); oetsa = null; break; case SHEAR: if (shrsa != null) { shrsa.setVisible(false); shrsa.dispose(); shrsa = null; } break; } if (specialBox.getState()) { arrayChoice.setEnabled(true); lengthChoice.setEnabled(true); if (arrayChoice.getSelectedIndex() == RANDOM) generate.setEnabled(true); arrayField.setEditable(false); arrayField.setBackground(SystemColor.activeCaptionBorder); } else if (importBox.getState()) { arrayField.setEditable(true); arrayField.setBackground(Color.white); } arrayField.setEnabled(true); importBox.setEnabled(true); specialBox.setEnabled(true); sortChoice.setEnabled(true); start.setEnabled(true); compare.setEnabled(true); if (table != null) table.toFront(); else sortChoice.requestFocus(); } public void closeTable() { table.setVisible(false); table.dispose(); table = null; } public void actionPerformed(ActionEvent ae) { String s = ""; if (ae.getSource() == start) {
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -