Advertisement
Guest User

Untitled

a guest
Dec 12th, 2019
106
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.55 KB | None | 0 0
  1. import javax.swing.*;
  2. import java.awt.*;
  3. import java.awt.event.ActionEvent;
  4. import java.awt.event.ActionListener;
  5.  
  6. public class Gui extends JFrame implements ActionListener {
  7. // Definitionen
  8. JFrame gui = new JFrame();
  9. Sort sort = new Sort();
  10. String strAnzahlZahlen = new String();
  11. int iAnzahlZahlen;
  12. int[] arrayShow;
  13. int[] arraySortShow;
  14. JPanel mainpanel = new JPanel();
  15. JSplitPane centerpanel = new JSplitPane(JSplitPane.VERTICAL_SPLIT);
  16. JSplitPane centerbottompanel = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT);
  17. JPanel centertoppanel = new JPanel();
  18. JPanel centerleftpanel = new JPanel();
  19. JPanel centerrightpanel = new JPanel();
  20. JPanel leftpanel = new JPanel();
  21. JPanel toppanel = new JPanel();
  22. JPanel topcenterpanel = new JPanel();
  23. JPanel centercenterpanel = new JPanel();
  24. JButton btnSortieren = new JButton("Sortieren");
  25. JButton btnBeenden = new JButton("Beenden");
  26. JButton btnArrayErzeugen = new JButton("Ziehen");
  27. JTextField tfAnzahlZahlen = new JTextField();
  28. JLabel lblEinagbe = new JLabel("Anzahl Zahlen");
  29. JCheckBox checkRandomNumbers = new JCheckBox("Zufallszahlen ?");
  30. JTextArea taArrayAnzeige = new JTextArea();
  31. JTextArea taArraySortAnzeige = new JTextArea();
  32.  
  33. // Erstellung Oberfläche (Konstruktor)
  34. public Gui() {
  35. gui.setSize(800, 600);
  36. buildKonstrust();
  37. gui.setVisible(true);
  38. gui.setResizable(false);
  39. }
  40.  
  41. private void buildKonstrust() {
  42. // Aufbereitung Anzeige
  43. mainpanel.setLayout(new BoxLayout(mainpanel,1));
  44. gui.getContentPane().add(BorderLayout.CENTER,mainpanel);
  45. gui.getContentPane().add(BorderLayout.WEST, leftpanel);
  46. gui.getContentPane().add(BorderLayout.NORTH, toppanel);
  47.  
  48. // Erstellung BoxLayouts
  49. BoxLayout leftboxlayout = new BoxLayout(leftpanel, BoxLayout.Y_AXIS);
  50. leftpanel.setLayout(leftboxlayout);
  51. BoxLayout topboxlayout = new BoxLayout(toppanel, BoxLayout.X_AXIS);
  52. toppanel.setLayout(topboxlayout);
  53. BoxLayout centertoplayout = new BoxLayout(centertoppanel, BoxLayout.PAGE_AXIS);
  54. centertoppanel.setLayout(centertoplayout);
  55. // Toppanel
  56. toppanel.add(BorderLayout.CENTER, lblEinagbe);
  57. toppanel.add(BorderLayout.CENTER, tfAnzahlZahlen);
  58. toppanel.add(BorderLayout.CENTER, checkRandomNumbers);
  59. // Leftpanel
  60. leftpanel.add(btnBeenden);
  61. leftpanel.add(btnArrayErzeugen);
  62. leftpanel.add(btnSortieren);
  63. // Center Top panel
  64. centertoppanel.setMinimumSize(new Dimension(200,200));
  65. //Centerpanel
  66. centerpanel.setTopComponent(centertoppanel);
  67. centerpanel.setBottomComponent(centerbottompanel);
  68. mainpanel.add(centerpanel);
  69. //Center Bottom Split
  70. centerleftpanel.add(taArrayAnzeige);
  71. centerrightpanel.add(taArraySortAnzeige);
  72. centerleftpanel.setMinimumSize(new Dimension(320,200));
  73. centerrightpanel.setMinimumSize(new Dimension(320,200));
  74. centerbottompanel.setLeftComponent(centerleftpanel);
  75. centerbottompanel.setRightComponent(centerrightpanel);
  76. // Buttons
  77. btnBeenden.setPreferredSize(new Dimension(100, 40));
  78. btnBeenden.setBounds(200, 500, 120, 30);
  79. btnSortieren.setPreferredSize(new Dimension(100, 40));
  80. btnSortieren.setBounds(200, 500, 120, 30);
  81. btnArrayErzeugen.setPreferredSize(new Dimension(100, 40));
  82. btnArrayErzeugen.setBounds(200, 500, 120, 30);
  83. //Colours
  84. centerrightpanel.setBackground(Color.white);
  85. centerleftpanel.setBackground(Color.white);
  86. centertoppanel.setBackground(Color.white);
  87. leftpanel.setBackground(Color.white);
  88. toppanel.setBackground(Color.white);
  89. centerpanel.setBackground(Color.white);
  90. toppanel.setBackground(Color.white);
  91. // Add Events
  92. btnBeenden.addActionListener(this);
  93. btnSortieren.addActionListener(this);
  94. btnArrayErzeugen.addActionListener(this);
  95. // Event Anzahl Zahlen
  96. tfAnzahlZahlen.addActionListener(new ActionListener() {
  97. @Override
  98. public void actionPerformed(ActionEvent e) {
  99. strAnzahlZahlen = tfAnzahlZahlen.getText();
  100. iAnzahlZahlen = Integer.parseInt(strAnzahlZahlen);
  101. }
  102. });
  103. }
  104.  
  105. // Event Buttons
  106. @Override
  107. public void actionPerformed(ActionEvent buttons) {
  108. switch (buttons.getActionCommand()) {
  109. case "Beenden":
  110. gui.setVisible(false);
  111. break;
  112. case "Sortieren":
  113. arraySortShow = sort.sortieren(arrayShow);
  114. showArraySort();
  115. break;
  116. case "Ziehen":
  117. clear();
  118. createGuiArray();
  119. break;
  120. }
  121. }
  122.  
  123. // Show Array
  124. protected void showArray() {
  125. for (int i = 0; i < arrayShow.length; i++) {
  126. taArrayAnzeige.append(arrayShow[i] + "\n");
  127. }
  128. }
  129.  
  130. protected void showArraySort() {
  131. for (int i = 0; i < arraySortShow.length; i++) {
  132. taArraySortAnzeige.append(arraySortShow[i] + "\n");
  133. }
  134. }
  135.  
  136. public void clear() {
  137. arrayShow = null;
  138. arraySortShow = null;
  139. taArrayAnzeige.setText("");
  140. taArraySortAnzeige.setText("");
  141. }
  142.  
  143. // Rückgabe/Übergabe iAnzahlZahlen
  144. public int getiAnzahlZahlen() {
  145. return iAnzahlZahlen;
  146. }
  147.  
  148.  
  149. protected void createGuiArray() {
  150. //Abfrage - Checkbox angeklickt ja/nein
  151. if (checkRandomNumbers.isSelected()) { //Checkbox Ja
  152. if (iAnzahlZahlen != 0) {
  153. arrayShow = sort.createArray(true, iAnzahlZahlen);
  154. showArray();
  155. }
  156. else { // Error Anzahl Zahlen
  157. JOptionPane jopError = new JOptionPane();
  158. jopError.showMessageDialog(null, "Error ");
  159. }
  160. }
  161. else { //Checkbox Nein
  162. //JTextFelder erstellen nach iAnzahlZahlen
  163. int i = 0;
  164. while(i <= iAnzahlZahlen){
  165. JTextField tfvariable = new JTextField();
  166. tfvariable.setBackground(Color.red);
  167. centertoppanel.add(BorderLayout.CENTER, tfvariable);
  168. tfvariable.setVisible(true);
  169. i++;
  170. }
  171.  
  172. //Ausgabe Array
  173. arrayShow = sort.createArray(false, iAnzahlZahlen);
  174. showArray();
  175. }
  176. }
  177. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement