📄 setoperationspanel.java
字号:
package Strategy;import java.awt.*;import java.awt.event.*;import javax.swing.*;import javax.swing.text.*;import java.io.*;import java.util.StringTokenizer;import java.util.ArrayList;/** * TextInputDemo.java is a 1.4 application that uses * these additional files: * SpringUtilities.java * ... */public class SetOperationsPanel extends JPanel implements ActionListener, FocusListener{ private JTextField wordField; Font regularFont, italicFont; JTextArea resultDisplay; private JScrollBar slider; private String operation; final static int GAP = 10; Comparable [] a; int i_flag=1; JRadioButton btn1; JRadioButton btn2; JRadioButton btn3; JRadioButton btn4; JRadioButton btn5; JRadioButton btn6; JRadioButton btn7; JRadioButton btn8; ButtonGroup grp1; public SetOperationsPanel() { setLayout(new BoxLayout(this, BoxLayout.LINE_AXIS)); JPanel leftHalf = new JPanel() { //Don't allow us to stretch vertically. public Dimension getMaximumSize() { Dimension pref = getPreferredSize(); return new Dimension(Integer.MAX_VALUE,pref.height); } }; leftHalf.setLayout(new BoxLayout(leftHalf, BoxLayout.PAGE_AXIS)); leftHalf.add(createEntryFields()); leftHalf.add(createButtons()); leftHalf.add(createRadioButtons()); add(leftHalf); add(createOutputDiaplay()); rememberOperations("ShellSort"); } protected JComponent createRadioButtons() { JPanel panel = new JPanel(new GridLayout(2,4,5,5)); btn1=new JRadioButton("Do ShellSort",true); btn2=new JRadioButton("Do QuickSort"); btn3=new JRadioButton("Do MergeSort"); btn4=new JRadioButton("Do HeapSort"); btn5=new JRadioButton("Do BinSort"); btn6=new JRadioButton("Do BubbleSort"); btn7=new JRadioButton("Do RankSort"); btn8=new JRadioButton("Do SelectionSort"); btn1.setActionCommand("ShellSort"); btn2.setActionCommand("QuickSort"); btn3.setActionCommand("MergeSort"); btn4.setActionCommand("HeapSort"); btn5.setActionCommand("BinSort"); btn6.setActionCommand("BubbleSort"); btn7.setActionCommand("RankSort"); btn8.setActionCommand("SelectionSort"); btn1.addActionListener(this); btn2.addActionListener(this); btn3.addActionListener(this); btn4.addActionListener(this); btn5.addActionListener(this); btn6.addActionListener(this); btn7.addActionListener(this); btn8.addActionListener(this); panel.add(btn1); panel.add(btn2); panel.add(btn3); panel.add(btn4); panel.add(btn5); panel.add(btn6); panel.add(btn7); panel.add(btn8); grp1=new ButtonGroup(); grp1.add(btn1); grp1.add(btn2); grp1.add(btn3); grp1.add(btn4); grp1.add(btn5); grp1.add(btn6); grp1.add(btn7); grp1.add(btn8); return panel; } protected JComponent createButtons() { JPanel panel = new JPanel(new FlowLayout(FlowLayout.TRAILING)); JButton button1 = new JButton("Run Program"); button1.setActionCommand("run"); button1.addActionListener(this); panel.add(button1); JButton button2 = new JButton("Reset"); button2.addActionListener(this); button2.setActionCommand("clear"); panel.add(button2); //Match the SpringLayout's gap, subtracting 5 to make //up for the default gap FlowLayout provides. panel.setBorder(BorderFactory.createEmptyBorder(0, 0, GAP-5, GAP-5)); return panel; } /** * 监听者,当你按按钮时,它将根据你按哪个按钮,决定做什么动作 */ public void actionPerformed(ActionEvent e) { String oper = e.getActionCommand(); rememberOperations(oper); String s_output,s_count; int i_count=0; if ("clear".equals(e.getActionCommand())) { wordField.setText(""); resultDisplay.setText(""); } else if ("run".equals(e.getActionCommand())) { String setB = wordField.getText(); prompt(setB); StringTokenizer tokensSet2 = new StringTokenizer(setB," "); int n=tokensSet2.countTokens(); String[] b= new String[n]; for ( int j=0;j<n; j++) b[j] = tokensSet2.nextToken(); Sorter sorter = new Sorter(); doSetOperation(sorter, b); } // end of run } //end of actionPerformed // listen to the listener and remember the operations needed // to do from the radio button, variable operation is assigned // a vulue by this method protected void rememberOperations(String operat) { if( operat.equals("ShellSort")) { operation="ShellSort"; } else if(operat.compareTo("QuickSort") == 0 ) { operation="QuickSort"; } else if(operat.compareTo("MergeSort") == 0 ) { operation="MergeSort"; } else if(operat.compareTo("HeapSort") == 0 ) { operation="HeapSort"; } else if(operat.compareTo("BinSort") == 0 ) { operation="BinSort"; } else if(operat.compareTo("BubbleSort") == 0 ) { operation="BubbleSort"; } else if(operat.compareTo("RankSort") == 0 ) { operation="RankSort"; } else if(operat.compareTo("SelectionSort") == 0 ) { operation="SelectionSort"; } } // print the resultant set operation result to the display penal protected void printToPanel(String[] c) { resultDisplay.append("{ "); for (int s=0; s<c.length;s++) { if (s != c.length-1) resultDisplay.append(c[s]+", "); else resultDisplay.append(c[s]); } resultDisplay.append(" }\n"); } // if the user doesn't put anything in text Field A or B // this meethod will tell him that he need to fill out // text area A or B protected void prompt(String textInput) { if(textInput.length()==0) { wordField.setText("Input Set B "); return; } } // this method will call some methods to do set operations and // after get the returned values, the returned values will be // print on to the output panel protected void doSetOperation(Sorter sorter, String[] b) { a = new Long[b.length]; for(int i = 0; i < b.length; i++) { a[i] = Long.valueOf(b[i]); System.out.println(a[i]); } if(operation.compareTo("ShellSort")==0) { sorter.setStrategy("ShellSort"); String[] c = sorter.sort(a); printToPanel(c); } else if(operation.compareTo("QuickSort")==0) { sorter.setStrategy("QuickSort"); String[] c = sorter.sort(a); printToPanel(c); } else if(operation.compareTo("MergeSort")==0) { sorter.setStrategy("MergeSort"); String[] c = sorter.sort(a); printToPanel(c); } else if(operation.compareTo("HeapSort")==0) { sorter.setStrategy("HeapSort"); String[] c = sorter.sort(a); printToPanel(c); } else if(operation.compareTo("BinSort")==0) { sorter.setStrategy("BinSort"); String[] c = sorter.sort(a); printToPanel(c); } else if(operation.compareTo("BubbleSort")==0) { sorter.setStrategy("BubbleSort"); String[] c = sorter.sort(a); printToPanel(c); } else if(operation.compareTo("RankSort")==0) { sorter.setStrategy("RankSort"); String[] c = sorter.sort(a); printToPanel(c); } else if(operation.compareTo("SelectionSort")==0) { sorter.setStrategy("SelectionSort"); String[] c = sorter.sort(a); printToPanel(c); } }/*2005-04-12 张小东修改*/ protected JComponent createOutputDiaplay() { JPanel panel = new JPanel(new BorderLayout()); resultDisplay=new JTextArea(); JScrollPane p=new JScrollPane(resultDisplay); resultDisplay.getAutoscrolls(); resultDisplay.setLineWrap(true); resultDisplay.setAutoscrolls(true); regularFont = resultDisplay.getFont().deriveFont(Font.PLAIN, 16.0f); italicFont = regularFont.deriveFont(Font.ITALIC); //Lay out the panel. panel.setBorder(BorderFactory.createEmptyBorder( GAP/2, //top 0, //left GAP/2, //bottom 0)); //right panel.add(new JSeparator(JSeparator.VERTICAL), BorderLayout.LINE_START); panel.add(p,BorderLayout.CENTER); panel.setPreferredSize(new Dimension(200, 150)); return panel; } /** * Called when one of the fields gets the focus so that * we can select the focused field. */ public void focusGained(FocusEvent e) { Component c = e.getComponent(); if (c instanceof JFormattedTextField) { selectItLater(c); } else if (c instanceof JTextField) { ((JTextField)c).selectAll(); } } //Workaround for formatted text field focus side effects. protected void selectItLater(Component c) { if (c instanceof JFormattedTextField) { final JFormattedTextField ftf = (JFormattedTextField)c; SwingUtilities.invokeLater(new Runnable() { public void run() { ftf.selectAll(); } }); } } //Needed for FocusListener interface. public void focusLost(FocusEvent e) { } //ignore protected JComponent createEntryFields() { JPanel panel = new JPanel(new SpringLayout()); String[] labelStrings = { "Input Number: "}; JLabel[] labels = new JLabel[labelStrings.length]; JComponent[] fields = new JComponent[labelStrings.length]; int fieldNum = 0; wordField = new JTextField(); wordField.setColumns(20); fields[fieldNum++] = wordField; //Associate label/field pairs, add everything, and lay it out. for (int i = 0; i < labelStrings.length; i++) { labels[i] = new JLabel(labelStrings[i], JLabel.TRAILING); labels[i].setLabelFor(fields[i]); panel.add(labels[i]); panel.add(fields[i]); } SpringUtilities.makeCompactGrid(panel, labelStrings.length, 2, GAP, GAP, //init x,y GAP, GAP/2);//xpad, ypad return panel; } public JFormattedTextField getTextField(JSpinner spinner) { JComponent editor = spinner.getEditor(); if (editor instanceof JSpinner.DefaultEditor) { return ((JSpinner.DefaultEditor)editor).getTextField(); } else { System.err.println("Unexpected editor type: " + spinner.getEditor().getClass() + " isn't a descendant of DefaultEditor"); return null; } } /** * Create the GUI and show it. For thread safety, * this method should be invoked from the * event-dispatching thread. */ private static void createAndShowGUI() { //Make sure we have nice window decorations. JFrame.setDefaultLookAndFeelDecorated(true); //Create and set up the window. JFrame frame = new JFrame("Set Operations"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //Create and set up the content pane. JComponent newContentPane = new SetOperationsPanel(); newContentPane.setOpaque(true); //content panes must be opaque frame.setContentPane(newContentPane); //Display the window. frame.pack(); frame.setVisible(true); } public static void main(String[] args) { //Schedule a job for the event-dispatching thread: //creating and showing this application's GUI. javax.swing.SwingUtilities.invokeLater(new Runnable() { public void run() { createAndShowGUI(); } }); }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -