1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128
| package com_java_JTextEditor;
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JTextArea;
import javax.swing.JButton;
import java.awt.Color;
import javax.swing.UIManager;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.AbstractAction;
import java.awt.event.ActionEvent;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import javax.swing.Action;
import javax.swing.JTextField;
public class TextEditor extends JFrame {
public Classtransport transport=new Classtransport();
private JPanel contentPane;
private final Action action = new SwingAction();
private final Action action_1 = new SwingAction_1();
private JTextField textField;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
TextEditor frame = new TextEditor();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the frame.
*/
public TextEditor() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 450, 300);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(null);
JTextArea contenu = new JTextArea();
contenu.setBounds(6, 58, 438, 161);
contentPane.add(contenu);
contenu.getText();
transport.contenutexte=contenu.getText();
JButton btnNewButton = new JButton("Exporter");
btnNewButton.setAction(action_1);
btnNewButton.setBackground(new Color(9, 80, 208));
btnNewButton.setForeground(Color.BLACK);
btnNewButton.setBounds(327, 218, 117, 29);
contentPane.add(btnNewButton);
JButton btnNewButton_1 = new JButton("Annuler");
btnNewButton_1.setAction(action);
btnNewButton_1.setBounds(218, 218, 117, 29);
contentPane.add(btnNewButton_1);
JLabel lblNewLabel = new JLabel("JTextEditor 1.0");
lblNewLabel.setBounds(6, 256, 150, 16);
contentPane.add(lblNewLabel);
textField = new JTextField();
textField.setBounds(106, 6, 130, 26);
contentPane.add(textField);
textField.setColumns(10);
JLabel lblNewLabel_1 = new JLabel("Nom du fichier:");
lblNewLabel_1.setBounds(6, 11, 117, 16);
contentPane.add(lblNewLabel_1);
JLabel lblNewLabel_2 = new JLabel("Contenu:");
lblNewLabel_2.setBounds(6, 41, 61, 16);
contentPane.add(lblNewLabel_2);
}
private class SwingAction extends AbstractAction {
public SwingAction() {
putValue(NAME, "annuler");
putValue(SHORT_DESCRIPTION, "annuler");
}
public void actionPerformed(ActionEvent e) {
System.exit(0);
}
}
private class SwingAction_1 extends AbstractAction {
public SwingAction_1() {
putValue(NAME, "exporter");
putValue(SHORT_DESCRIPTION, "exporter le fichier en format txt");
}
public void actionPerformed(ActionEvent e) {
String texte;
texte=transport.contenutexte;
textField.getText();
Path fichier=Paths.get(textField.getText()+".txt");
try {
Files.write(fichier, texte.getBytes());
} catch (IOException e1) {
JOptionPane.showMessageDialog(contentPane,"une erreur est survenue lors de la création du fichier.");
e1.printStackTrace();
}
}
}
} |
Partager