import java.awt.*;
import java.awt.event.*;
import java.util.*;
import javax.swing.*;
import javax.swing.text.*;
import java.io.*;
import javax.swing.Timer;
import javax.swing.text.Highlighter.HighlightPainter;
public class BubbleSortGUI extends JFrame
{
JPanel panel1, panel2, panel3, panel4;
JFrame frame = new JFrame();
JLabel listLbl;
JTextField addTxt;
JTextArea listTxt, processTxt;
JRadioButton rbInt, rbFlt, rbStr, rbAsc, rbDesc;
JButton addBtn, delBtn, sortBtn;
ButtonGroup sortGroup, typeGroup;
HandlerAction action = new HandlerAction();
ArrayList<String> strList = new ArrayList<String>();
ArrayList<Integer> intList = new ArrayList<Integer>();
ArrayList<Float> fltList = new ArrayList<Float>();
public BubbleSortGUI() {
super("Bubble Sorter");
setLayout(null);
setLocation(200,100);
displayContent();
}
public void createAndShowGUI() {
setResizable(false);
setVisible(true);
setSize(800,520);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public void displayContent() {
panel1 = new JPanel();
panel1.setBorder(BorderFactory.createTitledBorder("Add/Delete Elements"));
panel1.setBounds(20,20,260,250);
panel1.setLayout(null);
add(panel1);
rbInt = new JRadioButton("Integer");
rbInt.setBounds(5,25,100,20);
rbInt.setSelected(true);
rbInt.addActionListener(action);
panel1.add(rbInt);
rbFlt = new JRadioButton("Float");
rbFlt.setBounds(5,50,100,20);
rbFlt.addActionListener(action);
panel1.add(rbFlt);
rbStr = new JRadioButton("String");
rbStr.setBounds(5,75,100,20);
rbStr.addActionListener(action);
typeGroup = new ButtonGroup();
typeGroup.add(rbInt);
typeGroup.add(rbFlt);
typeGroup.add(rbStr);
panel1.add(rbStr);
addTxt = new JTextField("Input Data");
addTxt.setBounds(5,110,250,20);
panel1.add(addTxt);
addTxt.addMouseListener(new MouseAdapter(){
public void mouseReleased(MouseEvent e) {
addTxt.setText("");
}});
addBtn = new JButton("Add to List");
addBtn.setBounds(5,140,120,20);
panel1.add(addBtn);
addBtn.addActionListener(action);
delBtn = new JButton("Delete from List");
delBtn.setBounds(130,140,125,20);
panel1.add(delBtn);
delBtn.addActionListener(action);
listTxt = new JTextArea("List Elements");
listTxt.setEditable(false);
JScrollPane sList = new JScrollPane(listTxt);
sList.setBounds(5,170,250,40);
listTxt.setBorder(BorderFactory.createLoweredBevelBorder());
panel1.add(sList);
panel2 = new JPanel();
panel2.setBorder(BorderFactory.createTitledBorder("Sort Behavior"));
panel2.setBounds(20,270,260,100);
panel2.setLayout(null);
add(panel2);
rbAsc = new JRadioButton("Ascending Order", true);
rbAsc.setBounds(10,30,150,20);
rbDesc = new JRadioButton("Descending Order");
rbDesc.setBounds(10,60,150,20);
sortGroup = new ButtonGroup();
sortGroup.add(rbAsc);
sortGroup.add(rbDesc);
panel2.add(rbAsc);
panel2.add(rbDesc);
sortBtn = new JButton("SORT!");
sortBtn.setBounds(180,30,70,50);
panel2.add(sortBtn);
sortBtn.addActionListener(action);
panel3 = new JPanel();
panel3.setBorder(BorderFactory.createTitledBorder("Legend"));
panel3.setBounds(20,375,260,100);
panel3.setLayout(null);
add(panel3);
panel4 = new JPanel();
panel4.setLayout(null);
panel4.setBorder(BorderFactory.createTitledBorder("Process"));
panel4.setBounds(300,20,470,455);
processTxt = new JTextArea();
processTxt.setEditable(false);
processTxt.setBorder(BorderFactory.createLoweredBevelBorder());
JScrollPane sProcess = new JScrollPane(processTxt);
sProcess.setBounds(5,22,460,420);
sProcess.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED);
panel4.add(sProcess);
add(panel4);
}
private class HandlerAction implements ActionListener {
@Override
public void actionPerformed(ActionEvent e) {
if (e.getSource()==addBtn&&rbInt.isSelected()) {
try {
intList.add(Integer.parseInt(addTxt.getText()));
} catch (NumberFormatException err) {
JOptionPane.showMessageDialog(frame, "Please Input A Whole Number.");
}
listTxt.setText(intList.toString());
addTxt.setText("");
addTxt.requestFocusInWindow();
}
else if (e.getSource()==addBtn&&rbFlt.isSelected()) {
try {
fltList.add(Float.parseFloat(addTxt.getText()));
} catch (NumberFormatException err) {
JOptionPane.showMessageDialog(frame, "Please Input A Decimal Number.");
}
listTxt.setText(fltList.toString());
addTxt.setText("");
addTxt.requestFocusInWindow();
}
else if (e.getSource()==addBtn&&rbStr.isSelected()) {
strList.add(addTxt.getText());
listTxt.setText(strList.toString());
addTxt.setText("");
addTxt.requestFocusInWindow();
}
else if (e.getSource()==delBtn) {
int index;
if (rbInt.isSelected()) {
try {
index = intList.indexOf(Integer.parseInt(addTxt.getText()));
if (index<0) {
JOptionPane.showMessageDialog(frame, "Item Does Not Exist On The List.");
}
else {
intList.remove(index);
}
} catch (NumberFormatException err) {
JOptionPane.showMessageDialog(frame, "Please Input A Whole Number.");
}
listTxt.setText(intList.toString());
}
else if (rbFlt.isSelected()) {
try {
index = fltList.indexOf(Float.parseFloat(addTxt.getText()));
if (index<0) {
JOptionPane.showMessageDialog(frame, "Item Does Not Exist On The List.");
}
else {
fltList.remove(index);
}
} catch (NumberFormatException err) {
JOptionPane.showMessageDialog(frame, "Please Input A Decimal Number.");
}
listTxt.setText(fltList.toString());
}
else if (rbStr.isSelected()) {
index = strList.indexOf(addTxt.getText());
if (index<0) {
JOptionPane.showMessageDialog(frame, "Item Does Not Exist On The List.");
}
else {
strList.remove(index);
}
listTxt.setText(strList.toString());
}
addTxt.setText("");
addTxt.requestFocusInWindow();
}
else if (e.getSource()==sortBtn) {
Timer timer = new Timer(1000, this);
timer.start();
int sizeInt = intList.size();
int sizeFlt = fltList.size();
int sizeStr = strList.size();
processTxt.setText("");
if (rbAsc.isSelected()) {
if (rbInt.isSelected()) {
sort("int","asc",sizeInt);}
listTxt.setText(intList.toString());
}
else if (rbFlt.isSelected()) {
sort("flt","asc",sizeFlt);
listTxt.setText(fltList.toString());
}
else if (rbStr.isSelected()) {
sort("str","asc",sizeStr);
listTxt.setText(strList.toString());
}
else if (rbDesc.isSelected()) {
if (rbInt.isSelected()) {
sort("int","desc",sizeInt);
listTxt.setText(intList.toString());
}
else if (rbFlt.isSelected()) {
sort("flt","desc",sizeFlt);
listTxt.setText(fltList.toString());
}
else if (rbStr.isSelected()) {
sort("str","desc",sizeStr);
listTxt.setText(strList.toString());
}
}
}
else if (e.getSource()==rbInt) {
listTxt.setText(intList.toString());
}
else if (e.getSource()==rbFlt) {
listTxt.setText(fltList.toString());
}
else if (e.getSource()==rbStr) {
listTxt.setText(strList.toString());
}
}
public void sort(String type, String order, int size) {
int tempInt, int1, int2, p0, p1;
float tempFlt, flt1, flt2;
String tempStr, str1, str2;
processTxt.append("Pass 0:\n");
for (int i=0; i<size; i++) {
if (type.equals("int")) {
processTxt.append(intList.get(i)+" ");
}
else if (type.equals("flt")) {
processTxt.append(fltList.get(i)+" ");
}
else if (type.equals("str")) {
processTxt.append(strList.get(i)+" ");
}
}
processTxt.append("\n");
for (int i=0; i<size-1; i++) {
int x=0;
processTxt.append("Pass "+(i+1)+": \n");
while (x<size-1) {
int y = x + 1;
if (type.equals("int")) {
int1 = intList.get(x);
int2 = intList.get(y);
if (order.equals("asc")) {
if (int1 >= int2) {
tempInt = int1;
intList.set(x, int2);
intList.set(y, int1);
}
}
else if (order.equals("desc")) {
if (int1 <= int2) {
tempInt = int1;
intList.set(x, int2);
intList.set(y, int1);
}
}
}
else if (type.equals("flt")) {
flt1 = fltList.get(x);
flt2 = fltList.get(y);
if (order.equals("asc")) {
if (flt1 >= flt2) {
tempFlt = flt1;
fltList.set(x, flt2);
fltList.set(y, flt1);
}
}
else if (order.equals("desc")) {
if (flt1 <= flt2) {
tempFlt = flt1;
fltList.set(x, flt2);
fltList.set(y, flt1);
}
}
}
else if (type.equals("str")) {
str1 = strList.get(x);
str2 = strList.get(y);
if (order.equals("asc")) {
if (str1.compareTo(str2)>=0) {
tempStr = str1;
strList.set(x, str2);
strList.set(y, str1);
}
}
else if (order.equals("desc")) {
if (str1.compareTo(str2)<=0) {
tempStr = str1;
strList.set(x, str2);
strList.set(y, str1);
}
}
}
for (int z=0; z<size; z++) {
if (type.equals("int")) {
processTxt.append(intList.get(z)+" ");
}
else if (type.equals("flt")) {
processTxt.append(fltList.get(z)+" ");
}
else if (type.equals("str")) {
processTxt.append(strList.get(z)+" ");
}
}
processTxt.append("\n");
x++;
}
}
processTxt.append("Sorted:\n");
for (int i=0; i<size; i++) {
if (type.equals("int")) {
processTxt.append(intList.get(i)+" ");
}
else if (type.equals("flt")) {
processTxt.append(fltList.get(i)+" ");
}
else if (type.equals("str")) {
processTxt.append(strList.get(i)+" ");
}
}
}
}
}
I want to add a timer so i could delay the processTxt.append(). I've been reading about Swing timers but I don't know where in my code would I put it. I've been working on this since last month but I still couldn't get this. Thanks for all the help.
Aucun commentaire:
Enregistrer un commentaire