I was asked to make a Java application with main class q2.
How can I make it better and more efficient?
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.util.*;
class Sort extends q2
{
public static double SWITCH;
}
class q2 extends JFrame
{
public static void main(String[] args)
{
q2 window = new q2();
Container cont = window.getContentPane();
window.setDefaultCloseOperation(EXIT_ON_CLOSE);
Dimension dim = Toolkit.getDefaultToolkit().getScreenSize();
window.setVisible(true);
window.setSize(dim);
JPanel LeftPanel = new JPanel();
JPanel RightPanel = new JPanel();
JPanel MiddlePanel = new JPanel();
JPanel Middle1Panel =new JPanel();
JPanel Middle2Panel = new JPanel();
JPanel Middle3Panel = new JPanel();
MiddlePanel.add(Middle1Panel);
MiddlePanel.add(Middle2Panel);
MiddlePanel.add(Middle3Panel);
cont.setLayout(new GridLayout(1,3));
cont.add(LeftPanel);
cont.add(MiddlePanel);
cont.add(RightPanel);
LeftPanel.setBackground(Color.red);
RightPanel.setBackground(Color.black);
JRadioButton BubbleButton = new JRadioButton("BubbleSort");
JRadioButton SelectionButton = new JRadioButton("SelectionSort");
JRadioButton InsertionButton = new JRadioButton("InsertionSort");
RightPanel.add(BubbleButton);
RightPanel.add(SelectionButton);
RightPanel.add(InsertionButton);
ButtonGroup Group1 = new ButtonGroup();
Group1.add(InsertionButton);
Group1.add(SelectionButton);
Group1.add(BubbleButton);
BubbleButton.addItemListener(new ItemListener() {
public void itemStateChanged(ItemEvent e)
{
Sort.SWITCH = 1;
}
});
SelectionButton.addItemListener(new ItemListener() {
public void itemStateChanged(ItemEvent e)
{
Sort.SWITCH = 2;
}
});
InsertionButton.addItemListener(new ItemListener() {
public void itemStateChanged(ItemEvent e)
{
Sort.SWITCH = 3;
}
});
LeftPanel.setLayout(new GridLayout(2,1));
JPanel VariablePanel = new JPanel();
JPanel ResultPanel = new JPanel();
LeftPanel.add(VariablePanel);
LeftPanel.add(ResultPanel);
VariablePanel.setLayout(new GridLayout(10,1));
final JTextField [] VarField = new JTextField[20];
for(int q=0;q<10;q++)
{
VarField[q] = new JTextField();
VariablePanel.add(VarField[q]);
}
ResultPanel.setBackground(Color.black);
ResultPanel.setLayout(new GridLayout(10,10));
JButton SortButton = new JButton("Sort");
ResultPanel.add(SortButton);
Middle1Panel.setLayout(new GridLayout(4,2));
final JTextField Output = new JTextField(50);
Output.setText("OutputMinute");
ResultPanel.add(Output);
JLabel Min = new JLabel("Min");
Middle1Panel.add(Min);
final JTextField OutputMin = new JTextField(50);
Output.setText("OutputSecond");
Middle1Panel.add(OutputMin);
JLabel Sec = new JLabel("Sec");
Middle1Panel.add(Sec);
final JTextField OutputSec = new JTextField(50);
Output.setText("Output");
Middle1Panel.add(OutputSec);
JLabel Mill = new JLabel("Millisec");
Middle1Panel.add(Mill);
final JTextField OutputMill = new JTextField(50);
Output.setText("Output");
Middle1Panel.add(OutputMill);
JButton Randomise = new JButton("Randomise");
Middle1Panel.add(Randomise);
final Random randomise = new Random();
Randomise.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent a)
{
for(int q=0;q<=9;q++)
{
VarField[q].setText(Integer.toString(randomise.nextInt()));
}
}
});
SortButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent a)
{
if (Sort.SWITCH == 1)
{
Calendar bubblestart = Calendar.getInstance();
int w,mindiff,secdiff,millsecdiff,q;
int[] i =new int[10];
for(q=0;q<10;q++)
{
i[q]=Integer.parseInt(VarField[q].getText());
}
while (q != 0)
{
q=0;
for(int r=0;r<=8;r++)
{
if (i[r]>i[r+1])
{
q=1;
w=i[r];
i[r]=i[r+1];
i[r+1]=w;
}
}
}
Calendar bubbleend = Calendar.getInstance();
Output.setText(Integer.toString(i[0])+" "+Integer.toString(i[1])+" "+Integer.toString(i[2])+" "+Integer.toString(i[3])+" "+Integer.toString(i[4])+" "+Integer.toString(i[5])+" "+Integer.toString(i[6])+" "+Integer.toString(i[7])+" "+Integer.toString(i[8])+" "+Integer.toString(i[9]));
mindiff=bubbleend.get(Calendar.MINUTE)-bubblestart.get(Calendar.MINUTE);
secdiff=bubbleend.get(Calendar.SECOND)-bubblestart.get(Calendar.SECOND);
millsecdiff=bubbleend.get(Calendar.MILLISECOND)-bubblestart.get(Calendar.MILLISECOND);
OutputMin.setText(Integer.toString(mindiff));
OutputSec.setText(Integer.toString(secdiff));
OutputMill.setText(Integer.toString(millsecdiff));
}
else if (Sort.SWITCH == 2)
{
Calendar selectstart = Calendar.getInstance();
int w,serial=0,mindiff,secdiff,millsecdiff,q;
int[] i =new int[10];
for(q=0;q<10;q++)
{
i[q]=Integer.parseInt(VarField[q].getText());
}
for (q=0;q<9;q++)
{
w=i[q];
for(int r=q;r<=9;r++)
{
if(i[r]<w)
{
w=i[r];
serial=r;
}
}
if(i[q]!=w)
{
w=i[q];
i[q]=i[serial];
i[serial]=w;
}
}
Calendar selectend = Calendar.getInstance();
Output.setText(Integer.toString(i[0])+" "+Integer.toString(i[1])+" "+Integer.toString(i[2])+" "+Integer.toString(i[3])+" "+Integer.toString(i[4])+" "+Integer.toString(i[5])+" "+Integer.toString(i[6])+" "+Integer.toString(i[7])+" "+Integer.toString(i[8])+" "+Integer.toString(i[9]));
mindiff=selectend.get(Calendar.MINUTE)-selectstart.get(Calendar.MINUTE);
secdiff=selectend.get(Calendar.SECOND)-selectstart.get(Calendar.SECOND);
millsecdiff=selectend.get(Calendar.MILLISECOND)-selectstart.get(Calendar.MILLISECOND);
OutputMin.setText(Integer.toString(mindiff));
OutputSec.setText(Integer.toString(secdiff));
OutputMill.setText(Integer.toString(millsecdiff));
}
else if (Sort.SWITCH == 3)
{
Calendar insertstart = Calendar.getInstance();
int q,mindiff,secdiff,millsecdiff,loop1,loop2,temp1,temp2=0;
int[] i =new int[10];
for(q=0;q<10;q++)
{
i[q]=Integer.parseInt(VarField[q].getText());
}
for(loop1=1;loop1<=9;loop1++)
{
int in, out;
for(out=1; out<10; out++)
{
int temp = i[out];
in = out;
while(in>0 && i[in-1] >= temp)
{
i[in] = i[in-1];
--in;
}
i[in] = temp;
}
}
Calendar insertend = Calendar.getInstance();
Output.setText(Integer.toString(i[0])+" "+Integer.toString(i[1])+" "+Integer.toString(i[2])+" "+Integer.toString(i[3])+" "+Integer.toString(i[4])+" "+Integer.toString(i[5])+" "+Integer.toString(i[6])+" "+Integer.toString(i[7])+" "+Integer.toString(i[8])+" "+Integer.toString(i[9]));
mindiff=insertend.get(Calendar.MINUTE)-insertstart.get(Calendar.MINUTE);
secdiff=insertend.get(Calendar.SECOND)-insertstart.get(Calendar.SECOND);
millsecdiff=insertend.get(Calendar.MILLISECOND)-insertstart.get(Calendar.MILLISECOND);
OutputMin.setText(Integer.toString(mindiff));
OutputSec.setText(Integer.toString(secdiff));
OutputMill.setText(Integer.toString(millsecdiff));
}
else
{
Output.setText("Select a Sorting method");
}
}
});
}
}