Advertisement
Guest User

interfataGrafica

a guest
Dec 12th, 2018
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.91 KB | None | 0 0
  1. package invatatlab61;
  2.  
  3. import java.awt.Color;
  4. import java.awt.Dimension;
  5. import java.awt.GridLayout;
  6. import java.awt.Toolkit;
  7. import java.awt.event.ActionEvent;
  8. import java.awt.event.ActionListener;
  9. import java.util.Collections;
  10. import java.util.Comparator;
  11. import java.util.Iterator;
  12. import java.util.Vector;
  13.  
  14. import javax.swing.DefaultListModel;
  15. import javax.swing.JButton;
  16. import javax.swing.JFrame;
  17. import javax.swing.JLabel;
  18. import javax.swing.JList;
  19. import javax.swing.JPanel;
  20. import javax.swing.JTextField;
  21.  
  22. class Carte {
  23. private String Titlu;
  24. private String Autor;
  25. private int An;
  26.  
  27.  
  28. public Carte(String Titlu, String Autor, int An ){
  29. this.Titlu = Titlu;
  30. this.Autor = Autor;
  31. this.An = An;
  32. }
  33.  
  34. public String GetTitlu(){
  35. return Titlu;
  36. }
  37.  
  38. public String toString(){
  39. return Titlu + " " + Autor + " " + An;
  40. }
  41.  
  42. }
  43.  
  44. class TitluComparator implements Comparator<Carte> {
  45.  
  46. @Override
  47. public int compare(Carte o1, Carte o2) {
  48. String name1 = o1.GetTitlu();
  49. String name2 = o2.GetTitlu();
  50. // ascending order (descending order would be: name2.compareTo(name1))
  51. return name1.compareTo(name2);
  52. }
  53. }
  54.  
  55.  
  56. class Fereastra extends JFrame{
  57.  
  58. public Fereastra(String titlu){
  59. super(titlu);
  60. Toolkit t=Toolkit.getDefaultToolkit();
  61. Dimension d=t.getScreenSize();
  62. int h=d.height;
  63. int w=d.width;
  64. setSize(w/4,h/2);
  65. setLocation(w/4,h/4);
  66. setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  67.  
  68. }
  69. }
  70.  
  71. public class invatatlab61 {
  72.  
  73. static Vector<Carte> listaCarti = new Vector<Carte>();
  74.  
  75. public static void main(String[] args) {
  76. // TODO Auto-generated method stub
  77.  
  78.  
  79. JFrame f = new Fereastra("Carti");
  80. f.getContentPane().setLayout(new GridLayout(8,1));
  81.  
  82. JPanel p1 = new JPanel();
  83. JLabel lblTitlu = new JLabel("Titlu");
  84. JTextField txtTitlu = new JTextField(10);
  85. p1.add(lblTitlu);
  86. p1.add(txtTitlu);
  87.  
  88. JPanel p2 = new JPanel();
  89. JLabel lblAutor = new JLabel("Autor");
  90. JTextField txtAutor = new JTextField(10);
  91. p2.add(lblAutor);
  92. p2.add(txtAutor);
  93.  
  94. JPanel p5 = new JPanel();
  95. JLabel lblAn = new JLabel("An");
  96. JTextField txtAn = new JTextField(10);
  97. p5.add(lblAn);
  98. p5.add(txtAn);
  99.  
  100. JPanel p3 = new JPanel();
  101. JButton butonAdaugare = new JButton("Adaugare");
  102. JButton butonAfisare = new JButton("Afisare");
  103. JButton butonAfisareOrdonata = new JButton("Afisare ordonata");
  104. p3.add(butonAdaugare);
  105. p3.add(butonAfisare);
  106. p3.add(butonAfisareOrdonata);
  107.  
  108. JPanel p4 = new JPanel();
  109. DefaultListModel<String> model = new DefaultListModel<>();
  110. JList<String> txtRez = new JList<>(model);
  111. p4.add(txtRez);
  112.  
  113.  
  114.  
  115.  
  116. txtRez.setPreferredSize(new Dimension(120,90));
  117.  
  118.  
  119. butonAdaugare.addActionListener(new ActionListener(){
  120.  
  121. public void actionPerformed(ActionEvent e) {
  122.  
  123. String titlu = txtTitlu.getText();
  124. String autor = txtAutor.getText();
  125. int an = Integer.parseInt(txtAn.getText());
  126.  
  127. Carte nouaCarte = new Carte(titlu, autor, an);
  128.  
  129. listaCarti.add(nouaCarte);
  130. }
  131. });
  132.  
  133. butonAfisare.addActionListener(new ActionListener(){
  134.  
  135. public void actionPerformed(ActionEvent e) {
  136. model.clear();
  137. for (Iterator<Carte> i = listaCarti.iterator(); i.hasNext();) {
  138. Carte item = i.next();
  139. model.addElement(item.toString());
  140. }
  141. }
  142. });
  143.  
  144. butonAfisareOrdonata.addActionListener(new ActionListener(){
  145.  
  146. public void actionPerformed(ActionEvent e) {
  147.  
  148. Collections.sort(listaCarti, new TitluComparator());
  149. model.clear();
  150. for (Iterator<Carte> i = listaCarti.iterator(); i.hasNext();) {
  151. Carte item = i.next();
  152. model.addElement(item.toString());
  153. }
  154. }
  155. });
  156.  
  157.  
  158.  
  159.  
  160. f.getContentPane().add(p1);
  161. f.getContentPane().add(p2);
  162. f.getContentPane().add(p5);
  163. f.getContentPane().add(p3);
  164. f.getContentPane().add(p4);
  165.  
  166.  
  167. f.setVisible(true);
  168.  
  169.  
  170. }
  171.  
  172. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement