[code]
import java.awt.BorderLayout;
import javax.swing.JFileChooser;
import javax.swing.JPanel;
import javax.swing.JApplet;
import java.awt.Dimension;
import javax.swing.JScrollPane;
import java.awt.Rectangle;
import javax.swing.JTextArea;
import javax.swing.JButton;
import java.awt.Point;
import java.awt.Color;
import java.awt.Font;
import java.io.*;
import javax.swing.JDialog;
import javax.swing.JLabel;
public class AppletAva extends JApplet {
private JPanel jContentPane = null;
private JScrollPane ScrollPane1 = null;
private JTextArea TextAva = null;
private JButton ButaoNovo = null;
private JButton ButaoSalvar = null;
private JButton ButaoAbrir = null;
private JDialog DialogSN = null; // @jve:decl-index=0:visual-constraint="75,-3"
private JPanel jContPn = null;
private JLabel jLabel = null;
private JButton Sim = null;
private JButton Nao = null;
/**
* This is the xxx default constructor
*/
public AppletAva() {
super();
}
/**
* This method initializes this
*
* @return void
*/
public void init() {
this.setSize(502, 299);
this.setBackground(new Color(94, 189, 214));
this.setContentPane(getJContentPane());
}
/**
* This method initializes jContentPane
*
* @return javax.swing.JPanel
*/
private JPanel getJContentPane() {
if (jContentPane == null) {
jContentPane = new JPanel();
jContentPane.setLayout(null);
jContentPane.setBackground(new Color(67, 169, 230));
jContentPane.add(getScrollPane1(), null);
jContentPane.add(getButaoComp(), null);
jContentPane.add(getButaoExec(), null);
jContentPane.add(getButaoParar(), null);
jContentPane.add(getButaoNovo(), null);
jContentPane.add(getButaoSalvar(), null);
jContentPane.add(getButaoAbrir(), null);
}
return jContentPane;
}
/**
* This method initializes ScrollPane1
*
* @return javax.swing.JScrollPane
*/
private JScrollPane getScrollPane1() {
if (ScrollPane1 == null) {
ScrollPane1 = new JScrollPane();
ScrollPane1.setBounds(new Rectangle(20, 46, 360, 161));
ScrollPane1.setViewportView(getTextAva());
}
return ScrollPane1;
}
/**
* This method initializes TextAva
*
* @return javax.swing.JTextArea
*/
private JTextArea getTextAva() {
if (TextAva == null) {
TextAva = new JTextArea();
TextAva.setFont(new Font("Arial", Font.PLAIN, 12));
TextAva.setLineWrap(false);
TextAva.setWrapStyleWord(false);
}
return TextAva;
}
private JButton getButaoNovo() {
if (ButaoNovo == null) {
ButaoNovo = new JButton();
ButaoNovo.setText(“Novo”);
ButaoNovo.setLocation(new Point(414, 60));
ButaoNovo.setSize(new Dimension(69, 26));
ButaoNovo.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent e) {
System.out.println(“actionPerformed()”); // TODO Auto-generated Event stub actionPerformed()
if(TextAva != null){
// DialogSN x= new DialogSN();
// x.show(true);
e.getActionCommand();
JDialog DialogSN = getDialogSN();
DialogSN.setVisible(true);
}
// TextAva.setText(" ");
// TextAva.repaint();
// System.exit(0);
}
});
}
return ButaoNovo;
}
/**
* This method initializes ButaoSalvar
*
* @return javax.swing.JButton
*/
private JButton getButaoSalvar() {
if (ButaoSalvar == null) {
ButaoSalvar = new JButton();
ButaoSalvar.setText("Salvar");
ButaoSalvar.setLocation(new Point(414, 100));
ButaoSalvar.setSize(new Dimension(70, 26));
ButaoSalvar.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent e) {
System.out.println("actionPerformed()"); // TODO Auto-generated Event stub actionPerformed()
JFileChooser fc = new JFileChooser();
if(fc.showSaveDialog(AppletAva.this) != JFileChooser.APPROVE_OPTION)
return;
File arquivo = fc.getSelectedFile();
if(arquivo == null)
return;
FileWriter writer = null;
try{
writer = new FileWriter(arquivo);
writer.write(TextAva.getText());
}
catch(IOException ex){
// Possiveis erros aqui
}
finally{
if(writer != null){
try{
writer.close();
}
catch (IOException x){
//
}
}
}
}
}
);
}
return ButaoSalvar;
}
/**
* This method initializes ButaoAbrir
*
* @return javax.swing.JButton
*/
private JButton getButaoAbrir() {
if (ButaoAbrir == null) {
ButaoAbrir = new JButton();
ButaoAbrir.setText("Abrir");
ButaoAbrir.setLocation(new Point(414, 140));
ButaoAbrir.setSize(new Dimension(69, 26));
ButaoAbrir.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent e) {
System.out.println("actionPerformed()"); // TODO Auto-generated Event stub actionPerformed()
JFileChooser op = new JFileChooser();
int res = op.showOpenDialog(null);
if(res == JFileChooser.APPROVE_OPTION){
File arquivo = op.getSelectedFile();
TextAva.setText(" ");
try{
BufferedReader in = new BufferedReader(new FileReader(arquivo));
String str, texto = " ";
while((str = in.readLine()) != null){
texto += str;
}
TextAva.setText(texto);
in.close();
}
catch (IOException ioe){
// possiveis erros são tratatos aqui
}
}
}
});
}
return ButaoAbrir;
}
/**
* This method initializes DialogSN
*
* @return javax.swing.JDialog
*/
private JDialog getDialogSN() {
if (DialogSN == null) {
DialogSN = new JDialog();
DialogSN.setSize(new Dimension(286, 141));
DialogSN.setLocation(new Point(130, 70));
DialogSN.setContentPane(getJContPn());
DialogSN.setVisible(true);
}
return DialogSN;
}
/**
* This method initializes jContPn
*
* @return javax.swing.JPanel
*/
private JPanel getJContPn() {
if (jContPn == null) {
jLabel = new JLabel();
jLabel.setText(" Você deseja salvar este arquivo ?");
jLabel.setBackground(Color.white);
jLabel.setFont(new Font("Dialog", Font.BOLD, 12));
jLabel.setForeground(new Color(13, 12, 12));
jLabel.setBounds(new Rectangle(-1, 21, 278, 16));
jContPn = new JPanel();
jContPn.setLayout(null);
jContPn.setBackground(new Color(74, 187, 255));
jContPn.add(jLabel, null);
jContPn.add(getSim(), null);
jContPn.add(getNao(), null);
}
return jContPn;
}
/**
* This method initializes Sim
*
* @return javax.swing.JButton
*/
private JButton getSim() {
if (Sim == null) {
Sim = new JButton();
Sim.setBounds(new Rectangle(42, 75, 61, 21));
Sim.setText("Sim");
Sim.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent e) {
System.out.println("actionPerformed()"); // TODO Auto-generated Event stub actionPerformed()
JFileChooser fc = new JFileChooser();
if(fc.showSaveDialog(AppletAva.this) != JFileChooser.APPROVE_OPTION)
return;
File arquivo = fc.getSelectedFile();
if(arquivo == null)
return;
FileWriter writer = null;
try{
writer = new FileWriter(arquivo);
writer.write(TextAva.getText());
}
catch(IOException ex){
// Possiveis erros aqui
}
finally{
if(writer != null){
try{
writer.close();
}
catch (IOException x){
//
}
}
}
TextAva.setText(" ");
TextAva.repaint();
}
});
}
return Sim;
}
/**
* This method initializes Nao
*
* @return javax.swing.JButton
*/
private JButton getNao() {
if (Nao == null) {
Nao = new JButton();
Nao.setBounds(new Rectangle(166, 77, 59, 19));
Nao.setText("Não");
Nao.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent e) {
System.out.println("actionPerformed()"); // TODO Auto-generated Event stub actionPerformed()
TextAva.setText(" ");
TextAva.repaint();
DialogSN.dispose();
}
});
}
return Nao;
}
}[/code]