I've been doing a program with the Bubblesort algorithm. I have made a GUI with the algorithm, but my ending array with the number from smallest to biggest do not seem to appear, the only thing that appears is the last (and biggest) number. I know the algorithm is right because I did some testing using System.out.print
to check if it's right and it is. I'm using a JTextArea
and my code is as follows:
JButton button = new JButton("Berechnen");
JTextField z1 = new JTextField();
JTextField z2 = new JTextField();
JTextField z3 = new JTextField();
JTextField z4 = new JTextField();
JLabel n1 = new JLabel("Erste Zahl");
JLabel n2 = new JLabel("Zweite Zahl");
JLabel n3 = new JLabel("Dritte Zahl");
JLabel n4 = new JLabel("Vierte Zahl");
JTextArea res = new JTextArea("Ergebnis");
public static void main(String[] args) {
new Sortieren();
}
public Sortieren() {
this.setTitle("Bubblesort");
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
this.setSize(400, 400);
this.setLayout(null);
this.setVisible(true);
this.add(z1);
z1.setBounds(150,10,120,20);
this.add(z2);
z2.setBounds(150,40,120,20);
this.add(z3);
z3.setBounds(150,70,120,20);
this.add(z4);
z4.setBounds(150,100,120,20);
this.add(n1);
n1.setBounds(10,10,120,20);
this.add(n2);
n2.setBounds(10,40,120,20);
this.add(n3);
n3.setBounds(10,70,120,20);
this.add(n4);
n4.setBounds(10,100,120,20);
this.add(res);
res.setBounds(10,130,200,20);
this.add(button);
button.setBounds(70,160,100,20);
button.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
String text1= z1.getText();
String text2= z2.getText();
String text3= z3.getText();
String text4= z4.getText();
int behaelter = 0;
int zahl1 = Integer.parseInt(text1);
int zahl2 = Integer.parseInt(text2);
int zahl3 = Integer.parseInt(text3);
int zahl4 = Integer.parseInt(text4);
int[] sort = {zahl1, zahl2, zahl3, zahl4};
for (int i = 0; i < sort.length; i++) {
for (int j = 1; j < (sort.length - i); j++) {
if (sort[j - 1] > sort[j]) {
behaelter = sort[j - 1];
sort[j - 1] = sort[j];
sort[j] = behaelter;
}
}
}
for (int i = 0; i < sort.length; i++) {
System.out.print(sort[i]+" ");
res.setText(String.valueOf(sort[i]+" "));
}
}
});
}
@Override
public void actionPerformed(ActionEvent e) {
throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}