DESIGN GUI APPLICATION JAVA Designed to demonstrate two sor
DESIGN GUI APPLICATION - JAVA
Designed to demonstrate two sorting algorithms: bubble and selection sorting. Design a GUI application as shown below. A sample solution is provided for your reference.Solution
import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java .awt.event.ActionEvent;
import java .awt.event.ActionListner;
import javax.swing.JButton;
import javax.swing.JChekBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JPanel;
import javax.swing.JTextField;
public class Sorter extends JPanel implements ActionListener
{
public static final long serialVersionUID = -5455545454555665455555L;
private static final String Bubble_Sort = \"bubble sort\"
private static final String Selection_Sort = \"selection sort\"
private static final String NO_RESULT = \"no result\";
private static final Dimension TEXT_INPUT_DIM = new Dimension(200, 30);
private static final JMenuBar MENU = new JMenuBar();
private static final JMenu SORT = new JMenu(\"Sorting Algorithms\");
private static final JMenuItem BUBBLE = new JMenuItem(BUBBLE_SORT);
private static final JMenuItem BUBBLE = new JMenuItem(SELECTION_SORT);
private final JTextField textfieldInput = new JTextField();
private final JCheckBox checkbox = new JCheckBox();
private final JLabel orderLabel = new JLabel(\"sort in ascending order\");
private final JLabel labRes = new JLabel(\"sorted result : \");
private final JLabel res = new JLabel(NO_RESULT);
private static String selectedMenu;
public Sorter() {
setLayout(new GridLayout(2, 1));
textfieldInput.setPreferredSize(TEXT_INPUT_DIM);
JPanel top = new JPanel();
top.setLayout(new FlowLayout(10));
top.add(textfieldInput);
top.add(createPanelCheckbox());
top.add(createSortButton());
add(top);
add(createPanelResult());
}
private Component createSortButton() {
JButton sortButton = new JButton(\"Launch\");
sortButton.addActionListener(this);
return sortButton;
}
private final JPanel createPanelResult() {
JPanel p = new JPanel();
p.setLayout(new BorderLayout());
p.add(labRes, BorderLayout.WEST);
p.add(res, BorderLayout.CENTER);
return p;
}
private final JPanel createPanelCheckbox() {
JPanel p = new JPanel();
p.setLayout(new FlowLayout(10));
p.add(checkbox);
p.add(orderLabel);
return p;
}
public static void main(String[] args) {
JFrame f = new JFrame();
f.setSize(500, 300);
f.setLocationRelativeTo(null);
f.add(new Sorter());
f.setJMenuBar(createMenu());
f.setVisible(true);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
private static JMenuBar createMenu() {
BUBBLE.addActionListener(new MenuItemListener());
SORT.add(BUBBLE);
INSERTION.addActionListener(new MenuItemListener());
SORT.add(SELECTION);
MENU.add(SORT);
return MENU;
}
static class MenuItemListener implements ActionListener {
@Override
public void actionPerformed(ActionEvent e) {
selectedMenu = e.getActionCommand();
}
}
@Override
public void actionPerformed(ActionEvent e) {
String content = textfieldInput.getText();
String[] parsedNumbers = content.split(SEPARATOR);
if (isSortable(parsedNumbers) && parsedNumbers.length < 10) {
int[] result = null;
if(selectedMenu==null)
selectedMenu = BUBBLE_SORT;
switch (selectedMenu) {
case BUBBLE_SORT:
result = bubbleSort(checkbox.isSelected());
break;
case SELECTION_SORT:
result = SELECTION_SORT(checkbox.isSelected());
break;
}
res.setText(result == null ? NO_RESULT : fillResultLabel(result));
}
}
private String fillResultLabel(int[] result) {
StringBuilder res = new StringBuilder();
for (int nb : result) {
res.append(nb + \" \");
}
return res.toString();
}
private final boolean isSortable(String[] numbers) {
return true;
}
private int[] bubbleSort(boolean sortAscending) {
return new int[]{1,2,3,4};
}
private int[] selection Sort(boolean sortAscending) {
return new int[]{5,6,7,8};
}
}




