Bom dia… Alguem poderia me ajudar… pois estou desenvolvendo um compilador e não consigo colocar as ações de abrir e fechar nos botões. Quem souber por favor poderia me passar o código. Estou utilizando o programa IDE NetBeans 6.1.
Obrigada!!
Eis aí o codigo:
package com.compilador.view;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import javax.swing.JOptionPane;
/**
*
-
@author aluno
*/
public class Editor extends javax.swing.JFrame {/** Creates new form JanelaPrincipal */
public Editor() {
initComponents();
init();
}private void init() {
editor.setTabSize(1);
mensagem.setTabSize(1);
}/** 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.
*/
//
private void initComponents() {jButton1 = new javax.swing.JButton();
jButton2 = new javax.swing.JButton();
jButton3 = new javax.swing.JButton();
jMenuBar1 = new javax.swing.JMenuBar();
jMenu1 = new javax.swing.JMenu();
jMenu2 = new javax.swing.JMenu();
jScrollPane1 = new javax.swing.JScrollPane();
editor = new javax.swing.JTextArea();
jScrollPane2 = new javax.swing.JScrollPane();
mensagem = new javax.swing.JTextArea();
jButton4 = new javax.swing.JButton();
jButton5 = new javax.swing.JButton();
jMenuBar2 = new javax.swing.JMenuBar();
jMenu3 = new javax.swing.JMenu();
jMenuItem1 = new javax.swing.JMenuItem();
jMenuItem2 = new javax.swing.JMenuItem();
jMenuItem3 = new javax.swing.JMenuItem();
jMenuItem8 = new javax.swing.JMenuItem();
jMenuItem4 = new javax.swing.JMenuItem();
jMenu4 = new javax.swing.JMenu();
jMenuItem5 = new javax.swing.JMenuItem();
jMenuItem6 = new javax.swing.JMenuItem();
jMenuItem7 = new javax.swing.JMenuItem();setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
jButton1.setText(“Compilar”);
jButton1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton1ActionPerformed(evt);
}
});jButton2.setText(“Salvar”);
jButton2.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton2ActionPerformed(evt);
}
});jButton3.setText(“Abrir”);
jMenu1.setText(“File”);
jMenuBar1.add(jMenu1);jMenu2.setText(“Edit”);
jMenuBar1.add(jMenu2);editor.setColumns(20);
editor.setRows(5);
jScrollPane1.setViewportView(editor);mensagem.setColumns(20);
mensagem.setRows(5);
jScrollPane2.setViewportView(mensagem);jButton4.setText(“Novo”);
jButton5.setAction(jButton5.getAction());
jButton5.setText(“Fechar”);jMenu3.setText(“Arquivo”);
jMenuItem1.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_O, java.awt.event.InputEvent.CTRL_MASK));
jMenuItem1.setIcon(new javax.swing.ImageIcon(“C:\WINDOWS\Icones\ark_selectall.png”)); // NOI18N
jMenuItem1.setText(“Novo”);
jMenu3.add(jMenuItem1);jMenuItem2.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_A, java.awt.event.InputEvent.CTRL_MASK));
jMenuItem2.setIcon(new javax.swing.ImageIcon(“C:\WINDOWS\Icones\fileopen.png”)); // NOI18N
jMenuItem2.setText(“Abrir”);
jMenu3.add(jMenuItem2);jMenuItem3.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_B, java.awt.event.InputEvent.CTRL_MASK));
jMenuItem3.setIcon(new javax.swing.ImageIcon(“C:\WINDOWS\Icones\filesave.png”)); // NOI18N
jMenuItem3.setText(“Salvar”);
jMenu3.add(jMenuItem3);jMenuItem8.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_F9, 0));
jMenuItem8.setIcon(new javax.swing.ImageIcon(“C:\WINDOWS\Icones\forward.png”)); // NOI18N
jMenuItem8.setText(“Compilar”);
jMenu3.add(jMenuItem8);jMenuItem4.setIcon(new javax.swing.ImageIcon(“C:\WINDOWS\Icones\fileclose.png”)); // NOI18N
jMenuItem4.setText(“Fechar”);
jMenu3.add(jMenuItem4);jMenuBar2.add(jMenu3);
jMenu4.setText(“Editar”);
jMenuItem5.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_X, java.awt.event.InputEvent.CTRL_MASK));
jMenuItem5.setText(“Recortar”);
jMenu4.add(jMenuItem5);jMenuItem6.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_C, java.awt.event.InputEvent.CTRL_MASK));
jMenuItem6.setText(“Copiar”);
jMenu4.add(jMenuItem6);jMenuItem7.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_V, java.awt.event.InputEvent.CTRL_MASK));
jMenuItem7.setText(“Colar”);
jMenu4.add(jMenuItem7);jMenuBar2.add(jMenu4);
setJMenuBar(jMenuBar2);
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jScrollPane2, javax.swing.GroupLayout.DEFAULT_SIZE, 533, Short.MAX_VALUE)
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
.addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 452, Short.MAX_VALUE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
.addComponent(jButton5, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(jButton4, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(jButton3, javax.swing.GroupLayout.DEFAULT_SIZE, 75, Short.MAX_VALUE)
.addComponent(jButton2, javax.swing.GroupLayout.DEFAULT_SIZE, 75, Short.MAX_VALUE)
.addComponent(jButton1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))))
.addContainerGap())
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addComponent(jButton1)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jButton2)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jButton3)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jButton4)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jButton5))
.addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 288, Short.MAX_VALUE))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 89, javax.swing.GroupLayout.PREFERRED_SIZE)
.addContainerGap())
);pack();
}//
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
tokens = editor.getText().split("$");
mensagem.setText("");
for (String token : tokens) {
mensagem.setText(mensagem.getText() + “\n” + tokens);
}
}private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {
BufferedWriter buff = null;
try {
// TODO add your handling code here:String nomeArquivo = JOptionPane.showInputDialog("Nome do arquivo?"); File arquivo = new File("C:\\IVANETE\\UNOESC\\10 Semestre 2007\\Compiladores\\" + nomeArquivo + ".cmp"); buff = new BufferedWriter(new FileWriter(arquivo)); tokens = editor.getText().split("$"); for (int i = 0; i < tokens.length; i++) { buff.write(tokens[i]); buff.newLine(); } } catch (IOException ex) { ex.printStackTrace(); } finally { try { buff.close(); } catch (IOException ex) { ex.printStackTrace(); } }
}
/**
-
@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
private javax.swing.JTextArea editor;
private javax.swing.JButton jButton1;
private javax.swing.JButton jButton2;
private javax.swing.JButton jButton3;
private javax.swing.JButton jButton4;
private javax.swing.JButton jButton5;
private javax.swing.JMenu jMenu1;
private javax.swing.JMenu jMenu2;
private javax.swing.JMenu jMenu3;
private javax.swing.JMenu jMenu4;
private javax.swing.JMenuBar jMenuBar1;
private javax.swing.JMenuBar jMenuBar2;
private javax.swing.JMenuItem jMenuItem1;
private javax.swing.JMenuItem jMenuItem2;
private javax.swing.JMenuItem jMenuItem3;
private javax.swing.JMenuItem jMenuItem4;
private javax.swing.JMenuItem jMenuItem5;
private javax.swing.JMenuItem jMenuItem6;
private javax.swing.JMenuItem jMenuItem7;
private javax.swing.JMenuItem jMenuItem8;
private javax.swing.JScrollPane jScrollPane1;
private javax.swing.JScrollPane jScrollPane2;
private javax.swing.JTextArea mensagem;
// End of variables declaration
private String[] tokens;
} -