Olá Galera
Estou fazendo um programa de ordenação de dados. E a cada Jbutton tenho um nome que é o método,sendo eles QuickSort ShellSort E BubbleSort.
Estou com muita dificuldade para colocar o algoritmo de cada um dentro do action Listener. Tenho o algoritmo de cada método.
public void quickSort(int v[], int esquerda, int direita) {
int vetor[] = { 109, 15, 65, 65, 76, 3, 4, 6, 8, 89 };
quickSort(vetor, 0, vetor.length - 1);
for (int i = 0; i < vetor.length; i++) {
System.out.println(" " + vetor[i]);
int esq = esquerda;
int dir = direita;
int pivo = v[(esq + dir) / 2];
int troca;
while (esq <= dir) {
while (v[esq] < pivo) {
esq = esq + 1;
}
while (v[dir] > pivo) {
dir = dir - 1;
}
if (esq <= dir) {
troca = v[esq];
v[esq] = v[dir];
v[dir] = troca;
esq = esq + 1;
dir = dir - 1;
}
}
if (dir > esquerda)
quickSort(v, esquerda, dir);
if (esq < direita)
quickSort(v, esq, direita);
}
}
}
Minha tela é essa;
public PainelOrdena (){
this.setLayout(null);
this.setTitle(“Métodos de Ordenação”);
this.setBounds(200, 100, 450, 460);
this.setResizable(false);
lbNo = new JLabel("Entrada de dados");
lbNo.setBounds(20, 10, 395, 10);
tentrada = new JTextField();
tentrada.setBounds(20, 30,395, 100);
jb1 = new JButton("QuickSort");
jb1.setBounds(20, 160, 95, 25);
jb1.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
try{
QuickSort();
System.out.println(tentrada.getText());
// new QuickSort();
/*for (int i = 0; i < vetor.length; i++) {
System.out.println(" " + vetor[i]);
}*/
// public Teste(){
//System.out.println(" " + this.vetor[1]);
System.out.print("Deu certo Quickou");
}catch(Exception ex){
JOptionPane.showMessageDialog(null, "Não foi possível ");
}
}
private QuickSort[] quickSorts = new QuickSort[10];
private void QuickSort(int v[], int esquerda, int direita) {
int vetor[] = { };
QuickSort(vetor, 0, vetor.length - 1);
for (int i = 0; i < vetor.length; i++) {
System.out.println(" " + vetor[i]);
int esq = esquerda;
int dir = direita;
int pivo = v[(esq + dir) / 2];
int troca;
while (esq <= dir) {
while (v[esq] < pivo) {
esq = esq + 1;
}
while (v[dir] > pivo) {
dir = dir - 1;
}
if (esq <= dir) {
troca = v[esq];
v[esq] = v[dir];
v[dir] = troca;
esq = esq + 1;
dir = dir - 1;
}
}
if (dir > esquerda)
QuickSort(v, esquerda, dir);
if (esq < direita)
QuickSort(v, esq, direita);
}
}
private void QuickSort() {
System.out.print(QuickSort.);
}
});
Font f1 = new Font("Arial", Font.ITALIC, 14);
jtQui = new JTextArea();
sp1 = new JScrollPane(jtQui);
sp1.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
sp1.setBounds(20, 225, 395, 150);
jtQui.setEditable(false);
jtQui.setLineWrap(true);
jtQui.setWrapStyleWord(true);
jtQui.setBackground(new Color(216, 216, 216));
jtQui.setForeground(new Color(100, 0, 0));
jtQui.setFont(f1);
jb2 = new JButton("BubbleSort");
jb2.setBounds(168, 160, 95, 25);
jb2.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
try{
System.out.print("Deu certo Bublleoeuou");
}catch(Exception ex){
JOptionPane.showMessageDialog(null, "Não foi possível ");
}
}
});
// jb2.setBounds(x,y,altura,largura);
jb3 = new JButton(“ShellSort”);
jb3.setBounds(319, 160, 95, 25);
jb3.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
try{
System.out.print("Deu certo Shellloueuou");
}catch(Exception ex){
JOptionPane.showMessageDialog(null, "Não foi possível ");
}
}
});
this.add(lbNo);
this.add(tentrada);
this.add(jb1);
this.add(sp1);
this.add(jb2);
this.add(jb3);
addWindowListener(
new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
}
}
Me Deêm uma força,pode ver que tentei fazer um método ai em cima.