Advertisement
Guest User

Untitled

a guest
Apr 20th, 2018
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.73 KB | None | 0 0
  1. package PROJ_0_GUI;
  2.  
  3. import javax.swing.*;
  4. import java.util.List;
  5. import java.awt.*;
  6. import java.awt.event.ActionEvent;
  7. import java.awt.event.ActionListener;
  8.  
  9. public class ChildWindowView extends JFrame {
  10.  
  11. private final LogController ctrl;
  12. private JPanel jPanel;
  13. private JPanel jPanel2;
  14. private JPanel jPanel3;
  15. JScrollPane jsp;
  16.  
  17. public ChildWindowView(LogController ctrl) {
  18. this.ctrl = ctrl;
  19. body();
  20. }
  21.  
  22. private void body() {
  23.  
  24. setTitle("Hello, world3!");
  25.  
  26. jPanel = new JPanel(); //Tworzę wnętrzę (w srodku ScrollPane i jpanel2)
  27. jPanel2 = new JPanel(); //Tworzę wnętrzę
  28. jPanel3 = new JPanel(); //Tworzę wnętrzę // tutaj umieszczoy pole do wpisania danych i przycisk
  29.  
  30. jPanel.setLayout(new BoxLayout(jPanel, BoxLayout.Y_AXIS)); //Ustawiam układ pionowy
  31. jPanel2.setLayout(new BoxLayout(jPanel2, BoxLayout.Y_AXIS)); //Ustawiam układ pionowy
  32. jPanel3.setLayout(new BoxLayout(jPanel3, BoxLayout.X_AXIS)); //Ustawiam układ poziomy
  33.  
  34. this.add(jPanel);
  35. jsp = new JScrollPane(jPanel2); // Tworzymy opakowanie z możliwością scrolla //w srodku jpanel2 to ktorego bedzie dostawiane dane
  36. jPanel.add(jsp); // dodanie scroolpane
  37.  
  38. //Komponenty
  39. JTextField textArea = new JTextField();
  40. JButton jButton = new JButton("Add");
  41.  
  42. // Dodanie komponentów
  43.  
  44. jPanel.add(jPanel3);
  45. jPanel3.add(textArea);
  46. jPanel3.add(jButton);
  47.  
  48.  
  49. jButton.addActionListener(new ActionListener() { //Akcja przycisku
  50. @Override
  51. public void actionPerformed(ActionEvent e) {
  52. if (textArea.getText().trim().length() > 0) {
  53. ctrl.addText(textArea); //metoda dodaje napis listy danych
  54. //ctrl.updateAll(); //metoda przechodzi po liscie okienek, czysci dane, wypisuje cała liste
  55. ctrl.updateLast(); //metoda przechodzi po liscie okienek, dodaje ostatni element listy
  56. }
  57. }
  58. });
  59.  
  60. pack();
  61. setSize(512, 384);
  62. System.out.println(textArea.getHeight());
  63. // jsp.setMinimumSize(new Dimension(100, 300)); //ustawienie rozmiaru scrollpane
  64. jPanel3.setMaximumSize(new Dimension(this.getWidth(), 100)); //ustawienie rozmiaru scrollpane
  65. }
  66.  
  67. @Override
  68. public void invalidate() {
  69. super.invalidate();
  70. jsp.setMinimumSize(new Dimension(this.getWidth(), this.getHeight()-jPanel3.getHeight())); //ustawienie rozmiaru scrollpane
  71. jPanel3.setMaximumSize (new Dimension(this.getWidth(),jPanel3.getHeight()));
  72.  
  73. }
  74.  
  75.  
  76. public void showLogs(List<Log> data) { //metoda dodaje liste w postaci labeli
  77. for (Log log : data) {
  78. JLabel jLabel = new JLabel(log.toString());
  79. jPanel2.add(jLabel);
  80. }
  81. jPanel2.revalidate();
  82. }
  83.  
  84.  
  85. public void start() {
  86. setVisible(true);
  87. }
  88.  
  89.  
  90. public void clearData() { //metoda kasuje labele w widoku
  91. jPanel2.removeAll();
  92. }
  93.  
  94.  
  95. public void addLast(Log log) { //metoda dodaje labele
  96. JLabel jLabel = new JLabel(log.toString());
  97. jPanel2.add(jLabel);
  98. jPanel2.revalidate();
  99.  
  100. }
  101. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement