@@ -11,7 +11,7 @@ public class Sorting extends Main {
1111 JPanel pPanel1 , pPanel2 ;
1212
1313 // Sorting Buttons
14- JButton jbtRandomize , jbtReset , jbtBubble , jbtInsertion , jbtSelection , jbtStart ; // Sorting Buttons
14+ JButton jbtRandomize , jbtMerge , jbtBubble , jbtInsertion , jbtSelection , jbtStart ; // Sorting Buttons
1515
1616 // Random Creator
1717 Random rand = new Random ();
@@ -23,18 +23,18 @@ public class Sorting extends Main {
2323 // Create Panel
2424 // Panel for options (bubble sort, insertion sort...)
2525 pPanel1 = new JPanel ();
26- pPanel1 .setLayout (new GridLayout (6 , 1 ));
26+ pPanel1 .setLayout (new GridLayout (1 , 7 ));
2727
2828 // Panel for main algorithm
2929 pPanel2 = new JPanel ();
30- pPanel2 .setLayout (new GridLayout (10 ,1 ));
30+ pPanel2 .setLayout (new GridLayout (10 , 1 ));
3131
3232 // Buttons for sorting
3333 jbtRandomize = new JButton ("Randomize" );//create button
34- jbtReset = new JButton ("Reset " );//create button
35- jbtBubble = new JButton ("Bubble sort " );//create button
36- jbtInsertion = new JButton ("Insertion sort " );//create button
37- jbtSelection = new JButton ("Selection sort " );//create button
34+ jbtMerge = new JButton ("Merge Sort " );//create button
35+ jbtBubble = new JButton ("Bubble Sort " );//create button
36+ jbtInsertion = new JButton ("Insertion Sort " );//create button
37+ jbtSelection = new JButton ("Selection Sort " );//create button
3838 jbtStart = new JButton ("Start" );//create button
3939 jbtStart .setBackground (Color .GREEN );
4040
@@ -44,7 +44,7 @@ public class Sorting extends Main {
4444 jb1 .setStringPainted (true );
4545
4646 // Adding elements to Panel 1
47- pPanel1 .add (jbtRandomize ); pPanel1 .add (jbtReset ); pPanel1 .add (jbtSelection );
47+ pPanel1 .add (jbtRandomize ); pPanel1 .add (jbtMerge ); pPanel1 .add (jbtSelection );
4848 pPanel1 .add (jbtBubble ); pPanel1 .add (jbtInsertion ); pPanel1 .add (jbtStart );
4949
5050 // Adding elements to Panel 2
@@ -54,9 +54,34 @@ public class Sorting extends Main {
5454 pPanel1 .setBackground (Color .CYAN );
5555 pPanel2 .setBackground (Color .YELLOW );
5656
57+ // Register listeners
58+ ListenerClass listener = new ListenerClass ();
59+ jbtRandomize .addActionListener (listener );
60+ jbtMerge .addActionListener (listener );
61+ jbtBubble .addActionListener (listener );
62+ jbtInsertion .addActionListener (listener );
63+ jbtSelection .addActionListener (listener );
64+ jbtStart .addActionListener (listener );
65+
5766 // Add Panels to the panel
5867 add (pPanel1 , BorderLayout .NORTH );
5968 add (pPanel2 , BorderLayout .CENTER );
60-
6169 }
70+
71+ class ListenerClass implements ActionListener {
72+ public void actionPerformed (ActionEvent e ) {
73+ if (e .getSource () == jbtRandomize )
74+ System .out .println ("jbtRandomize button clicked" );
75+ else if (e .getSource () == jbtMerge )
76+ System .out .println ("jbtMerge button clicked" );
77+ else if (e .getSource () == jbtBubble )
78+ System .out .println ("jbtBubble button clicked" );
79+ else if (e .getSource () == jbtInsertion )
80+ System .out .println ("jbtInsertion button clicked" );
81+ else if (e .getSource () == jbtSelection )
82+ System .out .println ("jbtSelection button clicked" );
83+ else if (e .getSource () == jbtStart )
84+ System .out .println ("jbtStart button clicked" );
85+ }
86+ }
6287}
0 commit comments