Advertisement
homzode

curso_java

Jun 18th, 2023 (edited)
169
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 11.04 KB | Source Code | 0 0
  1. directorio pkgdir/CargarClase.java
  2.  
  3. package pkgdir.graficos;
  4.  
  5. import javax.swing.*;
  6. import java.awt.Color;
  7. import java.awt.Image;
  8. import java.awt.image.BufferedImage;
  9. import java.awt.Font;
  10. import java.awt.Dimension;
  11. import javax.imageio.ImageIO;
  12. import java.net.URL;
  13.  
  14. /**
  15.   * Hereda de la clase JFrame
  16. **/
  17. public class GuiUser extends JFrame{
  18.     private JPanel panel;
  19.     private JButton butWrite;
  20.     private JButton butRead;
  21.     private JButton butDel;
  22.     private JLabel labWrite;
  23.     private JTextField texfWrite;
  24.     private JTextArea areaRead;
  25.     private JScrollPane scrollRead;
  26.     private String namel;
  27.     private Long docl;
  28.     private int edadl;
  29.  
  30.     /**
  31.     *Metodo constructor de la clase    
  32.     */
  33.     public GuiUser() {
  34.     }
  35.  
  36.     /**
  37.     *Metodo que pinta el JFrame    
  38.     *Retorna vacio
  39.     */
  40.     public void showWin(String name, Long documento, int edad){
  41.         try{
  42.             this.namel = name;
  43.             this.docl = documento;
  44.             this.edadl = edad;
  45.             System.out.println("Pintando ventana namel= "+namel+" docl= "+docl+" edadl= "+edadl);
  46.             this.setSize(550, 350);
  47.             this.setTitle("HV: "+namel+" doc: "+docl+" edad: "+edadl);
  48.             this.setLocationRelativeTo(null);
  49.             this.setResizable(true);
  50.             this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  51.             this.getContentPane().add(drawPanel());
  52.             this.setVisible(true);
  53.         }catch(Exception e){
  54.             e.printStackTrace();
  55.         }
  56.  
  57.     }
  58.  
  59.     /**
  60.     *Metodo que pinta el JButton butWrite
  61.     *Retorna JButton
  62.     */
  63.     private JButton drawButtonWrite(){
  64.         try{
  65.              URL url = GuiUser.class.getResource("../../res/write_1.png");
  66.              BufferedImage img = ImageIO.read(url);
  67.             butWrite = new JButton("Escribir",new ImageIcon(img));
  68.             butWrite.setBackground(Color.WHITE);
  69.             butWrite.setFont(butWrite.getFont().deriveFont(Font.BOLD | Font.ITALIC));
  70.             butWrite.setAlignmentX(panel.CENTER_ALIGNMENT);
  71.             butWrite.setFocusPainted(true);
  72.         }catch(Exception e){
  73.             e.printStackTrace();
  74.         }
  75.         return butWrite;
  76.     }
  77.  
  78.     /**
  79.     *Metodo que pinta el JButton butRead
  80.     *Retorna JButton
  81.     */
  82.     private JButton drawButtonRead(){
  83.         try{
  84.              URL url = GuiUser.class.getResource("../../res/read_1.png");
  85.              BufferedImage img = ImageIO.read(url);
  86.             butRead = new JButton("Leer",new ImageIcon(img));
  87.             butRead.setBackground(Color.WHITE);
  88.             butRead.setFont(butWrite.getFont().deriveFont(Font.BOLD | Font.ITALIC));
  89.             butRead.setAlignmentX(panel.CENTER_ALIGNMENT);
  90.             butRead.setFocusPainted(true);
  91.         }catch(Exception e){
  92.             e.printStackTrace();
  93.         }
  94.         return butRead;
  95.     }
  96.  
  97.     /**
  98.     *Metodo que pinta el JButton butDel
  99.     *Retorna JButton
  100.     */
  101.     private JButton drawButtonDel(){
  102.         try{
  103.              URL url = GuiUser.class.getResource("../../res/read_1.png");
  104.              BufferedImage img = ImageIO.read(url);
  105.             butDel = new JButton("Borrar",new ImageIcon(img));
  106.             butDel.setBackground(Color.WHITE);
  107.             butDel.setFont(butDel.getFont().deriveFont(Font.BOLD | Font.ITALIC));
  108.             butDel.setAlignmentX(panel.CENTER_ALIGNMENT);
  109.             butDel.setFocusPainted(true);
  110.         }catch(Exception e){
  111.             e.printStackTrace();
  112.         }
  113.         return butDel;
  114.     }
  115.  
  116.     /**
  117.     *Metodo que pinta el JTextField texffWrite Jlabel dentro de un JPanel
  118.     *Retorna JTextField
  119.     */
  120.     private JPanel drawTextfWrite(){
  121.         JPanel jtmp = new JPanel();
  122.         try{
  123.              jtmp.setLayout(new BoxLayout(jtmp,BoxLayout.X_AXIS));         
  124.             labWrite = new JLabel("Texto:  ");
  125.             texfWrite = new JTextField();
  126.             labWrite.setLabelFor(texfWrite);
  127.             texfWrite.setFont(new Font("Serif", Font.PLAIN, 16));
  128.             texfWrite.setPreferredSize(new Dimension(350, 30));
  129.              texfWrite.setMaximumSize(new Dimension(350, 30));
  130.             jtmp.add(labWrite);
  131.             jtmp.add(texfWrite);
  132.         }catch(Exception e){
  133.             e.printStackTrace();
  134.         }
  135.         return jtmp;
  136.     }
  137.  
  138.     /**
  139.     *Metodo que pinta el JTextArea dentro de un JScrollPane
  140.     *Retorna JScrollPane
  141.     */
  142.     private JScrollPane drawAreaRead(){
  143.         try{
  144.             areaRead = new JTextArea();
  145.             areaRead.setEditable(false);
  146.             scrollRead = new JScrollPane(areaRead);
  147.             areaRead.setFont(new Font("Serif", Font.PLAIN, 16));
  148.             scrollRead.setPreferredSize(new Dimension(350, 120));
  149.             scrollRead.setMaximumSize(new Dimension(350, 120));
  150.         }catch(Exception e){
  151.             e.printStackTrace();
  152.         }
  153.         return scrollRead;
  154.     }
  155.  
  156.     /**
  157.     *Metodo que pinta el JPanel
  158.     *Retorna JPanel
  159.     */
  160.     private JPanel drawPanel(){
  161.         try{
  162.             panel = new JPanel();
  163.              panel.setLayout(new BoxLayout(panel,BoxLayout.Y_AXIS));
  164.             panel.add(Box.createVerticalStrut(10));
  165.             panel.setBackground(Color.CYAN);
  166.             panel.add(drawTextfWrite());
  167.             panel.add(Box.createVerticalStrut(5));
  168.             panel.add(drawButtonWrite());
  169.             panel.add(Box.createVerticalStrut(5));
  170.             panel.add(drawButtonRead());
  171.             panel.add(Box.createVerticalStrut(5));
  172.             panel.add(drawAreaRead());
  173.             panel.add(Box.createVerticalStrut(5));
  174.             panel.add(drawButtonDel());
  175.         }catch(Exception e){
  176.             e.printStackTrace();
  177.         }
  178.         return panel;
  179.     }  
  180.  
  181.  
  182.     /**
  183.      * Devuelve el boton escribir
  184.      * @return
  185.      */
  186.     public JButton getBotonWrite() {
  187.         return butWrite;
  188.     }
  189.  
  190.     /**
  191.      * Devuelve el boton leer
  192.      * @return
  193.      */
  194.     public JButton getBotonRead() {
  195.         return butRead;
  196.     }
  197.  
  198.     /**
  199.      * Devuelve el boton borrar
  200.      * @return
  201.      */
  202.     public JButton getBotonDel() {
  203.         return butDel;
  204.     }
  205.  
  206.     /**
  207.      * Devuelve el textarea read
  208.      * @return
  209.      */
  210.     public JTextArea gettextAreaRead() {
  211.         return areaRead;
  212.     }
  213.    
  214.     /**
  215.      * Devuelve el texField write
  216.      * @return
  217.      */
  218.     public JTextField getTextField() {
  219.         return texfWrite;
  220.     }
  221.  
  222.  
  223. }
  224.  
  225. directorio pkgdir/control/Controller.java
  226.  
  227. package pkgdir.control;
  228.  
  229. import pkgdir.modelo.FileServices;
  230. import pkgdir.graficos.GuiUser;
  231. import java.awt.event.ActionEvent;
  232. import java.awt.event.ActionListener;
  233. import javax.swing.event.CaretEvent;
  234. import javax.swing.event.CaretListener;
  235.  
  236.  
  237. public class Controller implements ActionListener{
  238.  
  239.     private GuiUser guiUserl;
  240.     private FileServices fileServices;
  241.     private CaretListener listener;
  242.     private String stmpg;
  243.  
  244.     /**
  245.      * Constructor sin parametros
  246.      * @see empty
  247.      */
  248.     public Controller() {
  249.         super();
  250.     }
  251.  
  252.     /**
  253.     * Constructor GuiUser como parametros
  254.     * @param GuiUser
  255.     */
  256.     public Controller(GuiUser guiUser) {
  257.         super();
  258.         this.guiUserl = guiUser;
  259.         agregarEventos();      
  260.     }
  261.  
  262.     /**
  263.      * Metodo que administra los eventos sobre los botones
  264.     * pertenece a la clase ActionListener
  265.      * @param ae
  266.      */
  267.     @Override
  268.     public void actionPerformed(ActionEvent ae) {
  269.         if( ae.getSource() == guiUserl.getBotonWrite()){
  270.             fileServices = new FileServices();
  271.             String stmp = guiUserl.getTextField().getText();
  272.             fileServices.writeFile( stmp, "historial.txt" );
  273.             guiUserl.getTextField().setText("");
  274.         }
  275.         if( ae.getSource() == guiUserl.getBotonRead()){
  276.             fileServices = new FileServices();
  277.             String stmp = fileServices.readFile( "historial.txt" );
  278.             guiUserl.gettextAreaRead().setText(stmp);
  279.         }
  280.         if( ae.getSource() == guiUserl.getBotonDel()){
  281.             fileServices = new FileServices();
  282.             fileServices.delText( "historial.txt", stmpg);
  283.         }
  284.     }
  285.  
  286.    
  287.     /**
  288.      * Metodo que agrega eventos sobre los componentes de GuiUser
  289.      */
  290.     private void agregarEventos(){
  291.         guiUserl.getBotonWrite().addActionListener(this);
  292.         guiUserl.getBotonRead().addActionListener(this);
  293.         guiUserl.getBotonDel().addActionListener(this);
  294.         listener = new CaretListener() {
  295.             public void caretUpdate(CaretEvent caretEvent) {
  296.                 stmpg = "";
  297.                 int posM = caretEvent.getMark();
  298.                 int posD = caretEvent.getDot();
  299.                 if( posD > posM){
  300.                     stmpg = (guiUserl.gettextAreaRead().getText()).substring( posM, posD );                
  301.                 }else{
  302.                     stmpg = (guiUserl.gettextAreaRead().getText()).substring( posD, posM );                
  303.                 }  
  304.                 System.out.println("stmpg: "+stmpg);   
  305.             }
  306.          };
  307.         guiUserl.gettextAreaRead().addCaretListener(listener);
  308.     }
  309.  
  310. }
  311.  
  312.  
  313. directorio pkgdir/modelo/FileServices.java
  314. package pkgdir.modelo;
  315.  
  316. import java.io.FileWriter;
  317. import java.io.File;
  318. import java.io.BufferedReader;
  319. import java.io.FileReader;
  320. import java.io.PrintWriter;
  321. import java.util.Scanner;
  322.  
  323.  
  324. public class FileServices{
  325.  
  326.     private File textfile;
  327.     private FileWriter writer;
  328.     private Scanner txtReader;
  329.  
  330.     /**
  331.      * Constructor sin parametros
  332.      * @see empty
  333.      */
  334.     public FileServices(){
  335.         super();
  336.     }
  337.  
  338.     /**
  339.      * Metodo que escribe texto en archivo plano
  340.      * @param String mess
  341.      * @param String fileName
  342.      */
  343.     public void writeFile(String mess, String fileName){
  344.         try {
  345.         textfile = new File(fileName);
  346.         writer = new FileWriter(textfile, true);
  347.         if (textfile.length() == 0){
  348.             writer.append(mess);
  349.         } else {
  350.             writer.append("\n"+mess);
  351.         }
  352.         writer.flush();
  353.         writer.close();
  354.         }catch (Exception e){
  355.             e.printStackTrace();
  356.         }
  357.     }
  358.  
  359.     /**
  360.      * Metodo que lee texto desde archivo plano
  361.      * @param String fileName
  362.      */
  363.  
  364.     public String readFile(String fileName) {
  365.         String data = new String("");
  366.         try {
  367.             textfile = new File(fileName);
  368.             if(textfile.exists() && !textfile.isDirectory()) {
  369.                 txtReader = new Scanner(textfile);
  370.                 while (txtReader.hasNextLine()) {
  371.                     data += txtReader.nextLine()+"\n";
  372.                 }
  373.                 txtReader.close();
  374.             }else{
  375.                 data = "No se encontro archivo: "+textfile.getName();
  376.             }
  377.            
  378.         } catch (Exception e) {
  379.             e.printStackTrace();
  380.         }
  381.         return data;
  382.     }
  383.  
  384.     /**
  385.      * Metodo que borra texto en archivo plano
  386.      * @param String fileName
  387.      */
  388.  
  389.     public void delText(String fileName, String dataModel) {
  390.         try {
  391.             System.out.println("dataModel: "+dataModel);   
  392.             textfile = new File(fileName);
  393.             //tmpPath guarda nombre para nuevo archivo temporal
  394.             String tmpPath = (fileName.substring( 0, fileName.indexOf( "." ))) + ".tmp";       
  395.             System.out.println("tmpPath: "+tmpPath);   
  396.             //Se crea elobjeto File para el archivo temporal
  397.             File tmpfile = new File( tmpPath );
  398.             if(textfile.exists() && !textfile.isDirectory()) {
  399.                 //Lee sobre archivo original
  400.                 BufferedReader br = new BufferedReader(new FileReader(fileName));
  401.                 //Escribe sobre archivo temporal
  402.                 PrintWriter pw = new PrintWriter(new FileWriter(tmpfile));
  403.                 String line = null;
  404.                 while ((line = br.readLine()) != null) {
  405.                     if ( !dataModel.trim().contains(line)) {
  406.                         System.out.println("line: "+line.trim());          
  407.                         pw.println(line.trim());
  408.                         pw.flush();
  409.                     }
  410.                 }
  411.                 pw.close();
  412.                 br.close();
  413.                 if (tmpfile.renameTo( new File(fileName) )) {
  414.                     System.out.println("archivo renombrado");
  415.                 } else {
  416.                     System.out.println("error");
  417.                 }
  418.             }else{
  419.                 System.out.println( "No se encontro archivo: "+textfile.getName() );           
  420.             }
  421.            
  422.         } catch (Exception e) {
  423.             e.printStackTrace();
  424.         }
  425.     }
  426.        
  427. }
  428.  
  429.  
  430. directorio pkgdir/java_build.sh
  431.  
  432. #!/bin/bash
  433. # -*- ENCODING: UTF-8 -*-
  434. echo "Compilando paquetes"
  435. javac CargarClase.java graficos/GuiUser.java control/Controller.java modelo/FileServices.java -d ./dist/
  436. echo "Generando JAR"
  437. cd dist/
  438. jar -cmf META-INF/MANIFEST.MF java_clases.jar pkgdir/CargarClase.class pkgdir res/*.png
  439. echo "Ejecutando aplicacion"
  440. java -jar java_clases.jar
  441. cd ..
  442. exit
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement