DimitriTheReader

PTXT

Jun 15th, 2015
282
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.87 KB | None | 0 0
  1. package com.gmail.dimitrischeibe.xti.gui;
  2.  
  3. import java.awt.BorderLayout;
  4. import java.awt.EventQueue;
  5.  
  6. import javax.swing.JFileChooser;
  7. import javax.swing.JFrame;
  8. import javax.swing.JOptionPane;
  9. import javax.swing.JPanel;
  10. import javax.swing.JTextArea;
  11. import javax.swing.border.EmptyBorder;
  12.  
  13. import java.awt.GridLayout;
  14.  
  15. import javax.swing.JButton;
  16. import javax.swing.JScrollPane;
  17.  
  18. import java.awt.Color;
  19. import java.awt.Font;
  20. import java.awt.event.ActionEvent;
  21. import java.awt.event.ActionListener;
  22. import java.io.File;
  23. import java.io.FileWriter;
  24. import java.io.IOException;
  25. import java.io.PrintWriter;
  26. import java.nio.file.Files;
  27. import java.nio.file.Path;
  28. import java.nio.file.Paths;
  29.  
  30. public class PTXT extends JFrame {
  31.  
  32.     JTextArea txt;
  33.     private JPanel contentPane;
  34.  
  35.     /**
  36.      * Launch the application.
  37.      */
  38.     public static void main(String[] args) {
  39.         EventQueue.invokeLater(new Runnable() {
  40.             public void run() {
  41.                 try {
  42.                     PTXT frame = new PTXT();
  43.                     frame.setVisible(true);
  44.                 } catch (Exception e) {
  45.                     e.printStackTrace();
  46.                 }
  47.             }
  48.         });
  49.     }
  50.  
  51.     /**
  52.      * Create the frame.
  53.      */
  54.     public PTXT() {
  55.         setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  56.         setBounds(100, 100, 965, 729);
  57.         contentPane = new JPanel();
  58.         contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
  59.         setContentPane(contentPane);
  60.         contentPane.setLayout(new BorderLayout());
  61.        
  62.         JPanel panel = new JPanel();
  63.         contentPane.add(BorderLayout.SOUTH, panel);
  64.         panel.setLayout(new GridLayout(1, 2, 0, 0));
  65.        
  66.        
  67.        
  68.        
  69.         txt = new JTextArea();
  70.         txt.setFont(new Font("Arial", Font.PLAIN, 26));
  71.         txt.setForeground(Color.LIGHT_GRAY);
  72.         txt.setBackground(Color.DARK_GRAY);
  73.        
  74.         JScrollPane scrollPane = new JScrollPane(txt);
  75.         contentPane.add(scrollPane, BorderLayout.CENTER);
  76.        
  77.         JButton btnNewButton = new JButton("Abrir");
  78.         panel.add(btnNewButton);
  79.         btnNewButton.addActionListener(new ActionListener() {
  80.             public void actionPerformed(ActionEvent e) {
  81.                 JFileChooser jfc = new JFileChooser();
  82.                 jfc.showOpenDialog(null);
  83.                 File file = jfc.getSelectedFile();
  84.                 try {
  85.                     Path path = Paths.get(file.getAbsolutePath());
  86.                     String retorno = new String(Files.readAllBytes(path));
  87.                     txt.setText(retorno);
  88.                    
  89.                 } catch (Exception erro) {
  90.                     JOptionPane.showMessageDialog(null, "Vish, deu erro D:");
  91.                 }
  92.             }
  93.         });
  94.         JButton btnNew = new JButton("Salvar");
  95.         panel.add(btnNew);
  96.         btnNew.addActionListener(new ActionListener() {
  97.             public void actionPerformed(ActionEvent e) {
  98.                JFileChooser salvar = new JFileChooser();
  99.                
  100.                File salvarArquivoEscolhido = salvar.getSelectedFile();  
  101.                try {
  102.                 PrintWriter pw = new PrintWriter(new FileWriter(salvarArquivoEscolhido));
  103.                 pw.print(txt.toString());
  104.                 pw.close();
  105.               } catch (IOException e1) {
  106.                 e1.printStackTrace();
  107.             }
  108.        
  109.             }
  110.         });
  111.    
  112.        
  113.        
  114.     }
  115. }
Advertisement
Add Comment
Please, Sign In to add comment