Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import javax.swing.*;
- import java.awt.*;
- import java.awt.event.ActionEvent;
- import java.awt.event.ActionListener;
- public class Gui extends JFrame implements ActionListener {
- // Definitionen
- JFrame gui = new JFrame();
- Sort sort = new Sort();
- String strAnzahlZahlen = new String();
- int iAnzahlZahlen;
- int[] arrayShow;
- int[] arraySortShow;
- JPanel mainpanel = new JPanel();
- JSplitPane centerpanel = new JSplitPane(JSplitPane.VERTICAL_SPLIT);
- JSplitPane centerbottompanel = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT);
- JPanel centertoppanel = new JPanel();
- JPanel centerleftpanel = new JPanel();
- JPanel centerrightpanel = new JPanel();
- JPanel leftpanel = new JPanel();
- JPanel toppanel = new JPanel();
- JPanel topcenterpanel = new JPanel();
- JPanel centercenterpanel = new JPanel();
- JButton btnSortieren = new JButton("Sortieren");
- JButton btnBeenden = new JButton("Beenden");
- JButton btnArrayErzeugen = new JButton("Ziehen");
- JTextField tfAnzahlZahlen = new JTextField();
- JLabel lblEinagbe = new JLabel("Anzahl Zahlen");
- JCheckBox checkRandomNumbers = new JCheckBox("Zufallszahlen ?");
- JTextArea taArrayAnzeige = new JTextArea();
- JTextArea taArraySortAnzeige = new JTextArea();
- // Erstellung Oberfläche (Konstruktor)
- public Gui() {
- gui.setSize(800, 600);
- buildKonstrust();
- gui.setVisible(true);
- gui.setResizable(false);
- }
- private void buildKonstrust() {
- // Aufbereitung Anzeige
- mainpanel.setLayout(new BoxLayout(mainpanel,1));
- gui.getContentPane().add(BorderLayout.CENTER,mainpanel);
- gui.getContentPane().add(BorderLayout.WEST, leftpanel);
- gui.getContentPane().add(BorderLayout.NORTH, toppanel);
- // Erstellung BoxLayouts
- BoxLayout leftboxlayout = new BoxLayout(leftpanel, BoxLayout.Y_AXIS);
- leftpanel.setLayout(leftboxlayout);
- BoxLayout topboxlayout = new BoxLayout(toppanel, BoxLayout.X_AXIS);
- toppanel.setLayout(topboxlayout);
- BoxLayout centertoplayout = new BoxLayout(centertoppanel, BoxLayout.PAGE_AXIS);
- centertoppanel.setLayout(centertoplayout);
- // Toppanel
- toppanel.add(BorderLayout.CENTER, lblEinagbe);
- toppanel.add(BorderLayout.CENTER, tfAnzahlZahlen);
- toppanel.add(BorderLayout.CENTER, checkRandomNumbers);
- // Leftpanel
- leftpanel.add(btnBeenden);
- leftpanel.add(btnArrayErzeugen);
- leftpanel.add(btnSortieren);
- // Center Top panel
- centertoppanel.setMinimumSize(new Dimension(200,200));
- //Centerpanel
- centerpanel.setTopComponent(centertoppanel);
- centerpanel.setBottomComponent(centerbottompanel);
- mainpanel.add(centerpanel);
- //Center Bottom Split
- centerleftpanel.add(taArrayAnzeige);
- centerrightpanel.add(taArraySortAnzeige);
- centerleftpanel.setMinimumSize(new Dimension(320,200));
- centerrightpanel.setMinimumSize(new Dimension(320,200));
- centerbottompanel.setLeftComponent(centerleftpanel);
- centerbottompanel.setRightComponent(centerrightpanel);
- // Buttons
- btnBeenden.setPreferredSize(new Dimension(100, 40));
- btnBeenden.setBounds(200, 500, 120, 30);
- btnSortieren.setPreferredSize(new Dimension(100, 40));
- btnSortieren.setBounds(200, 500, 120, 30);
- btnArrayErzeugen.setPreferredSize(new Dimension(100, 40));
- btnArrayErzeugen.setBounds(200, 500, 120, 30);
- //Colours
- centerrightpanel.setBackground(Color.white);
- centerleftpanel.setBackground(Color.white);
- centertoppanel.setBackground(Color.white);
- leftpanel.setBackground(Color.white);
- toppanel.setBackground(Color.white);
- centerpanel.setBackground(Color.white);
- toppanel.setBackground(Color.white);
- // Add Events
- btnBeenden.addActionListener(this);
- btnSortieren.addActionListener(this);
- btnArrayErzeugen.addActionListener(this);
- // Event Anzahl Zahlen
- tfAnzahlZahlen.addActionListener(new ActionListener() {
- @Override
- public void actionPerformed(ActionEvent e) {
- strAnzahlZahlen = tfAnzahlZahlen.getText();
- iAnzahlZahlen = Integer.parseInt(strAnzahlZahlen);
- }
- });
- }
- // Event Buttons
- @Override
- public void actionPerformed(ActionEvent buttons) {
- switch (buttons.getActionCommand()) {
- case "Beenden":
- gui.setVisible(false);
- break;
- case "Sortieren":
- arraySortShow = sort.sortieren(arrayShow);
- showArraySort();
- break;
- case "Ziehen":
- clear();
- createGuiArray();
- break;
- }
- }
- // Show Array
- protected void showArray() {
- for (int i = 0; i < arrayShow.length; i++) {
- taArrayAnzeige.append(arrayShow[i] + "\n");
- }
- }
- protected void showArraySort() {
- for (int i = 0; i < arraySortShow.length; i++) {
- taArraySortAnzeige.append(arraySortShow[i] + "\n");
- }
- }
- public void clear() {
- arrayShow = null;
- arraySortShow = null;
- taArrayAnzeige.setText("");
- taArraySortAnzeige.setText("");
- }
- // Rückgabe/Übergabe iAnzahlZahlen
- public int getiAnzahlZahlen() {
- return iAnzahlZahlen;
- }
- protected void createGuiArray() {
- //Abfrage - Checkbox angeklickt ja/nein
- if (checkRandomNumbers.isSelected()) { //Checkbox Ja
- if (iAnzahlZahlen != 0) {
- arrayShow = sort.createArray(true, iAnzahlZahlen);
- showArray();
- }
- else { // Error Anzahl Zahlen
- JOptionPane jopError = new JOptionPane();
- jopError.showMessageDialog(null, "Error ");
- }
- }
- else { //Checkbox Nein
- //JTextFelder erstellen nach iAnzahlZahlen
- int i = 0;
- while(i <= iAnzahlZahlen){
- JTextField tfvariable = new JTextField();
- tfvariable.setBackground(Color.red);
- centertoppanel.add(BorderLayout.CENTER, tfvariable);
- tfvariable.setVisible(true);
- i++;
- }
- //Ausgabe Array
- arrayShow = sort.createArray(false, iAnzahlZahlen);
- showArray();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement