@author maq02
/
import javax.swing.JOptionPane;
import java.io.;
import java.awt.FileDialog;
public class Editor extends javax.swing.JFrame {
/** Creates new form Editor /
public Editor() {
initComponents();
}
FileDialog fabrir, fsalvarcomo;
String nomearquivo, texto;
int neg, ita;
/* This method is called from within the constructor to
-
initialize the form.
-
WARNING: Do NOT modify this code. The content of this method is
-
always regenerated by the Form Editor.
*/
// <editor-fold defaultstate=“collapsed” desc=" Generated Code ">//GEN-BEGIN:initComponents
private void initComponents() {
jScrollPane2 = new javax.swing.JScrollPane();
jtArea = new javax.swing.JEditorPane();
jMenuBar1 = new javax.swing.JMenuBar();
jMenu1 = new javax.swing.JMenu();
Novo = new javax.swing.JMenuItem();
Abrir = new javax.swing.JMenuItem();
jSeparator1 = new javax.swing.JSeparator();
Salvar = new javax.swing.JMenuItem();
SalvarComo = new javax.swing.JMenuItem();
jSeparator2 = new javax.swing.JSeparator();
Sair = new javax.swing.JMenuItem();
jMenu2 = new javax.swing.JMenu();
Copiar = new javax.swing.JMenuItem();
Recortar = new javax.swing.JMenuItem();
Colar = new javax.swing.JMenuItem();
jSeparator3 = new javax.swing.JSeparator();
Negrito = new javax.swing.JCheckBoxMenuItem();
Italico = new javax.swing.JCheckBoxMenuItem();
SelecionarTudo = new javax.swing.JMenuItem();
setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
jScrollPane2.setViewportView(jtArea);
jMenu1.setMnemonic(‘a’);
jMenu1.setText(“Arquivo”);
Novo.setMnemonic(‘n’);
Novo.setText(“Novo”);
Novo.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
NovoActionPerformed(evt);
}
});
jMenu1.add(Novo);
Abrir.setMnemonic(‘a’);
Abrir.setText(“Abrir”);
Abrir.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
AbrirActionPerformed(evt);
}
});
jMenu1.add(Abrir);
jMenu1.add(jSeparator1);
Salvar.setMnemonic(‘s’);
Salvar.setText(“Salvar”);
Salvar.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
SalvarActionPerformed(evt);
}
});
jMenu1.add(Salvar);
SalvarComo.setMnemonic(‘c’);
SalvarComo.setText(“Salvar Como”);
SalvarComo.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
SalvarComoActionPerformed(evt);
}
});
jMenu1.add(SalvarComo);
jMenu1.add(jSeparator2);
Sair.setMnemonic(‘r’);
Sair.setText(“Sair”);
Sair.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
SairActionPerformed(evt);
}
});
jMenu1.add(Sair);
jMenuBar1.add(jMenu1);
jMenu2.setMnemonic(‘e’);
jMenu2.setText(“Editar”);
Copiar.setMnemonic(‘c’);
Copiar.setText(“Copiar”);
Copiar.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
CopiarActionPerformed(evt);
}
});
jMenu2.add(Copiar);
Recortar.setMnemonic(‘r’);
Recortar.setText(“Recortar”);
Recortar.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
RecortarActionPerformed(evt);
}
});
jMenu2.add(Recortar);
Colar.setMnemonic(‘o’);
Colar.setText(“Colar”);
Colar.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
ColarActionPerformed(evt);
}
});
jMenu2.add(Colar);
jMenu2.add(jSeparator3);
Negrito.setMnemonic(‘N’);
Negrito.setText(“Negrito”);
Negrito.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
NegritoActionPerformed(evt);
}
});
jMenu2.add(Negrito);
Italico.setMnemonic(‘I’);
Italico.setText(“Italico”);
Italico.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
ItalicoActionPerformed(evt);
}
});
jMenu2.add(Italico);
SelecionarTudo.setMnemonic(‘S’);
SelecionarTudo.setText(“Selecionar Tudo”);
SelecionarTudo.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
SelecionarTudoActionPerformed(evt);
}
});
jMenu2.add(SelecionarTudo);
jMenuBar1.add(jMenu2);
setJMenuBar(jMenuBar1);
org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
.add(jScrollPane2, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 399, Short.MAX_VALUE)
);
layout.setVerticalGroup(
layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
.add(jScrollPane2, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 260, Short.MAX_VALUE)
);
pack();
java.awt.Dimension screenSize = java.awt.Toolkit.getDefaultToolkit().getScreenSize();
java.awt.Dimension dialogSize = getSize();
setLocation((screenSize.width-dialogSize.width)/2,(screenSize.height-dialogSize.height)/2);
}// </editor-fold>//GEN-END:initComponents
private void ItalicoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_ItalicoActionPerformed
// TODO add your handling code here:
if (Italico.getState()==true){
ita = 2;
} else{
ita=0;
}
jtArea.setFont(new java.awt.Font(“Arial”, neg+ita, 20));
}//GEN-LAST:event_ItalicoActionPerformed
private void NegritoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_NegritoActionPerformed
// TODO add your handling code here:
if (Negrito.getState()==true){
neg = 1;
} else{
neg = 0;
}
jtArea.setFont(new java.awt.Font(“Arial”, neg+ita, 20));
}//GEN-LAST:event_NegritoActionPerformed
private void SelecionarTudoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_SelecionarTudoActionPerformed
// TODO add your handling code here:
jtArea.selectAll();
}//GEN-LAST:event_SelecionarTudoActionPerformed
private void ColarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_ColarActionPerformed
// TODO add your handling code here:
jtArea.paste();
}//GEN-LAST:event_ColarActionPerformed
private void RecortarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_RecortarActionPerformed
// TODO add your handling code here:
jtArea.cut();
}//GEN-LAST:event_RecortarActionPerformed
private void CopiarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_CopiarActionPerformed
// TODO add your handling code here:
jtArea.copy();
}//GEN-LAST:event_CopiarActionPerformed
private void SairActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_SairActionPerformed
// TODO add your handling code here:
this.dispose();
}//GEN-LAST:event_SairActionPerformed
private void SalvarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_SalvarActionPerformed
// TODO add your handling code here:
if (nomearquivo.length()!=0){
try{
FileWriter out = new FileWriter(nomearquivo);
out.write(jtArea.getText());
out.close();
} catch (java.io.IOException e){
JOptionPane.showMessageDialog(null, “Erro ao salvar o arquivo”);
}
}
}//GEN-LAST:event_SalvarActionPerformed
private void AbrirActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_AbrirActionPerformed
// TODO add your handling code here:
try{
fabrir = new FileDialog(this, “Abrir Arquivo”, FileDialog.LOAD);
fabrir.show();
if(fabrir.getFile()==null);
{
return;
}
nomearquivo = fabrir.getDirectory()+fabrir.getFile();
FileReader in=new FileReader(nomearquivo);
String texto;
texto="";
int I;
I = in.read();
while (I!=1){
texto=texto+(char)I;
I=in.read();
}
jtArea.setText(texto);
in.close();
} catch (java.io.IOException e){
JOptionPane.showMessageDialog(null, “Erro ao carregar o arquivo”);
}
}//GEN-LAST:event_AbrirActionPerformed
private void SalvarComoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_SalvarComoActionPerformed
// TODO add your handling code here:
fsalvarcomo = new FileDialog(this, “Salvar Como”, FileDialog.SAVE);
//configura o tipo de caixa e o título da mesma
try{
fsalvarcomo.show();
//exibe a caixa
if (fsalvarcomo.getFile()==null);
{
return;
}
// caso o usuário não digite o nome do arquivo ou clique no botão
// cancelar será returnado com a gravação do arquivo
nomearquivo = fsalvarcomo.getDirectory()+fabrir.getFile();
FileWriter out = new FileWriter(nomearquivo);
out.write(jtArea.getText());
out.close();
} catch (java.io.IOException e){
JOptionPane.showMessageDialog(null, “Erro ao carregar o arquivo”);
}
}//GEN-LAST:event_SalvarComoActionPerformed
private void NovoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_NovoActionPerformed
// TODO add your handling code here:
String nomearquivo;
nomearquivo = “”;
jtArea.setText("");
}//GEN-LAST:event_NovoActionPerformed
/**
-
@param args the command line arguments
*/
public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new Editor().setVisible(true);
}
});
}
// Variables declaration - do not modify//GEN-BEGIN:variables
private javax.swing.JMenuItem Abrir;
private javax.swing.JMenuItem Colar;
private javax.swing.JMenuItem Copiar;
private javax.swing.JCheckBoxMenuItem Italico;
private javax.swing.JCheckBoxMenuItem Negrito;
private javax.swing.JMenuItem Novo;
private javax.swing.JMenuItem Recortar;
private javax.swing.JMenuItem Sair;
private javax.swing.JMenuItem Salvar;
private javax.swing.JMenuItem SalvarComo;
private javax.swing.JMenuItem SelecionarTudo;
private javax.swing.JMenu jMenu1;
private javax.swing.JMenu jMenu2;
private javax.swing.JMenuBar jMenuBar1;
private javax.swing.JScrollPane jScrollPane2;
private javax.swing.JSeparator jSeparator1;
private javax.swing.JSeparator jSeparator2;
private javax.swing.JSeparator jSeparator3;
private javax.swing.JEditorPane jtArea;
// End of variables declaration//GEN-END:variables