Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.gmail.dimitrischeibe.xti.gui;
- import java.awt.BorderLayout;
- import java.awt.EventQueue;
- import javax.swing.JFileChooser;
- import javax.swing.JFrame;
- import javax.swing.JOptionPane;
- import javax.swing.JPanel;
- import javax.swing.JTextArea;
- import javax.swing.border.EmptyBorder;
- import java.awt.GridLayout;
- import javax.swing.JButton;
- import javax.swing.JScrollPane;
- import java.awt.Color;
- import java.awt.Font;
- import java.awt.event.ActionEvent;
- import java.awt.event.ActionListener;
- import java.io.File;
- import java.io.FileWriter;
- import java.io.IOException;
- import java.io.PrintWriter;
- import java.nio.file.Files;
- import java.nio.file.Path;
- import java.nio.file.Paths;
- public class PTXT extends JFrame {
- JTextArea txt;
- private JPanel contentPane;
- /**
- * Launch the application.
- */
- public static void main(String[] args) {
- EventQueue.invokeLater(new Runnable() {
- public void run() {
- try {
- PTXT frame = new PTXT();
- frame.setVisible(true);
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- });
- }
- /**
- * Create the frame.
- */
- public PTXT() {
- setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
- setBounds(100, 100, 965, 729);
- contentPane = new JPanel();
- contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
- setContentPane(contentPane);
- contentPane.setLayout(new BorderLayout());
- JPanel panel = new JPanel();
- contentPane.add(BorderLayout.SOUTH, panel);
- panel.setLayout(new GridLayout(1, 2, 0, 0));
- txt = new JTextArea();
- txt.setFont(new Font("Arial", Font.PLAIN, 26));
- txt.setForeground(Color.LIGHT_GRAY);
- txt.setBackground(Color.DARK_GRAY);
- JScrollPane scrollPane = new JScrollPane(txt);
- contentPane.add(scrollPane, BorderLayout.CENTER);
- JButton btnNewButton = new JButton("Abrir");
- panel.add(btnNewButton);
- btnNewButton.addActionListener(new ActionListener() {
- public void actionPerformed(ActionEvent e) {
- JFileChooser jfc = new JFileChooser();
- jfc.showOpenDialog(null);
- File file = jfc.getSelectedFile();
- try {
- Path path = Paths.get(file.getAbsolutePath());
- String retorno = new String(Files.readAllBytes(path));
- txt.setText(retorno);
- } catch (Exception erro) {
- JOptionPane.showMessageDialog(null, "Vish, deu erro D:");
- }
- }
- });
- JButton btnNew = new JButton("Salvar");
- panel.add(btnNew);
- btnNew.addActionListener(new ActionListener() {
- public void actionPerformed(ActionEvent e) {
- JFileChooser salvar = new JFileChooser();
- File salvarArquivoEscolhido = salvar.getSelectedFile();
- try {
- PrintWriter pw = new PrintWriter(new FileWriter(salvarArquivoEscolhido));
- pw.print(txt.toString());
- pw.close();
- } catch (IOException e1) {
- e1.printStackTrace();
- }
- }
- });
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment