majkel Napisano Maj 7, 2015 Zgłoś Udostępnij Napisano Maj 7, 2015 import java.awt.BorderLayout; import java.awt.Container; import java.awt.Dimension; import java.awt.GridLayout; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.File; import java.io.FileReader; import java.io.FileWriter; import java.io.IOException; import javax.swing.AbstractButton; import javax.swing.JButton; import javax.swing.JFileChooser; import javax.swing.JFrame; import javax.swing.JScrollPane; import javax.swing.JTextArea; public class Edytor extends JFrame implements ActionListener { private JButton wczytaj=new JButton("wczytaj"); private JButton zapisz=new JButton("zapisz"); private JTextArea edytor=new JTextArea(); private JScrollPane scroll=new JScrollPane(); public Edytor() { Container c = this.getContentPane(); Container d = new Container(); d.setLayout(new GridLayout()); c.setLayout(new BorderLayout()); Container e = this.getContentPane(); Container e1 = new Container(); //c.add(przycisk,BorderLayout.SOUTH); d.add(wczytaj,BorderLayout.EAST); d.add(zapisz,BorderLayout.WEST); c.add(d,BorderLayout.SOUTH); c.add(edytor,BorderLayout.CENTER); e1.add(scroll,BorderLayout.CENTER); wczytaj.addActionListener(this); zapisz.addActionListener(this); this.setSize(new Dimension(600,600)); this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); this.setVisible(true); scroll.setBounds(5,5,300,200); } public static void main(String[] args) { new Edytor(); } @Override public void actionPerformed(ActionEvent a) { if(a.getSource()==wczytaj) { JFileChooser fc =new JFileChooser(); fc.showOpenDialog(null); String tekst = ""; File plik=fc.getSelectedFile(); try { BufferedReader br=new BufferedReader(new FileReader(plik)); String linia; do { linia=br.readLine(); if(linia!=null) tekst+=linia+"\n"; } while(linia!=null); br.close(); edytor.setText(tekst); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } { if(a.getSource()==zapisz) { String tekst; /** * @param args */ JFileChooser fc =new JFileChooser(); fc.showSaveDialog(null); tekst=""; for(int i=0;i<10;i++) { tekst+="Hello World!\n"; } File plik=fc.getSelectedFile(); try { BufferedWriter bw=new BufferedWriter(new FileWriter(plik)); bw.write(tekst); bw.flush(); bw.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } } } Cytuj Link do komentarza Udostępnij na innych stronach More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.