package sortieren;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
public class SortierenGUI
{
final int n = 100;
String feld1[] = new String[n];
int zaehler = 1;
JFrame form1 = new JFrame("Sortieren");
FlowLayout layout1 = new FlowLayout();
JButton button1, button2, button3;
JTextField edit1 = new JTextField();
DefaultListModel model1 = new DefaultListModel();
JList listbox1 = new JList(model1);
public SortierenGUI()
{
button1 = new JButton("Hinzufügen");
button1.addActionListener(new Buttonhandler());
button2 = new JButton("Sortieren");
button2.addActionListener(new Buttonhandler());
button3 = new JButton("Beenden");
button3.addActionListener(new Buttonhandler());
edit1.setBounds(20, 20, 200, 20);
form1.add(edit1);
button1.setBounds(60, 70, 100, 25);
form1.add(button1);
button2.setBounds(60, 110, 100, 25);
form1.add(button2);
button3.setBounds(60, 150, 100, 25);
form1.add(button3);
listbox1.setBounds(300, 20, 250, 250);
form1.add(listbox1);
form1.setLayout(null);
form1.setSize(590,330);
form1.setVisible(true);
form1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
void hinzufuegen(String name)
{
feld1[zaehler] = name;
zaehler ++;
}
void tauschen(String par1, String par2)
{
String temp;
temp = par1;
par1 = par2;
par2 = temp;
}
void sortieren()
{
int i, j;
for (i = 1; i < (n - 1); i ++)
{
for (j = i + 1; i < n; i ++)
{
String string1 = feld1[i];
String string2 = feld1[j];
if (string1 != null && string1.compareTo(string2) > 0)
{
tauschen(feld1[i], feld1[j]);
}
}
}
}
private class Buttonhandler implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
if (e.getSource() == button1)
{
int i;
hinzufuegen(edit1.getText());
model1.clear();
for (i = 1; i < n; i ++)
{
if (feld1[i] != "")
{
model1.addElement(feld1[i]);
}
}
edit1.setText("");
}
else if (e.getSource() == button2)
{
int i;
sortieren();
model1.clear();
for (i = 1; i < n; i++)
{
if (feld1[i] != "")
{
model1.addElement(feld1[i]);
}
}
}
else if (e.getSource() == button3)
{
System.exit(0);
}
}
}
}