Download Apertura de Applet con Swing
Document related concepts
no text concepts found
Transcript
Alumna: Keren Monge Cid Materia: Web Application Development Ticher: Alejandro S. Álvarez Cifuentes Grupo: 3CM2 Apertura de Applet con Swing Código en Java: import java.awt.Container; import java.awt.GridLayout; import java.awt.event.*; import java.io.*; import javax.swing.*; public class Tarea2conSwing extends JApplet implements ActionListener { JPanel p, p2; JLabel l1, l2, l3; JButton b, g, cls; JTextArea ta; BufferedReader br; JFileChooser fd; String arch, ruta, texto; BufferedWriter bw; Container c; File f; public void init() { p = new JPanel(); l1 = new JLabel("Archivo:",2); l2 = new JLabel("Guardar:",2); l3 = new JLabel("Limpiar:",2); b = new JButton("Abrir"); b.addActionListener(this); g = new JButton("Guardar"); g.addActionListener(this); cls = new JButton("Limpiar"); cls.addActionListener(this); ta = new JTextArea(); c = getContentPane(); c.setLayout(new GridLayout(1,1)); p.add(l1); p.add(b); p.add(l2); p.add(g); p.add(l3); p.add(cls); c.add("North",p); c.add("South",ta); setSize(600, 300); } public void actionPerformed(ActionEvent e) { String s = e.getActionCommand(); if(s.equals("Abrir")){ fd = new JFileChooser(); fd.setVisible(true); System.out.println(ruta); if(fd.showOpenDialog(this)== JFileChooser.APPROVE_OPTION){ f = fd.getSelectedFile(); ruta = f.getPath(); try{ br = new BufferedReader(new FileReader(ruta)); while((texto = br.readLine()) != null) ta.append(texto + "\n"); br.close(); }catch(IOException ioe){} } } if(s.equals("Guardar")){ fd = new JFileChooser(); fd.setVisible(true); if(fd.showSaveDialog(this)== JFileChooser.APPROVE_OPTION){ f = fd.getSelectedFile(); ruta = f.getPath(); try{ bw = new BufferedWriter(new FileWriter(ruta +".txt")); bw.write(ta.getText()); bw.close(); }catch(IOException ioe){} } } if(s.equals("Limpiar")) ta.setText(""); } } Código en HTML: <HTML> <BODY> <APPLET code="Tarea2conSwing.class" archive= "Tarea2conSwing.jar" width=600 height=400> </APPLET> </BODY> </HTML>
Related documents