Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * To change this template, choose Tools | Templates
- * and open the template in the editor.
- */
- package javaapplication34;
- import java.awt.BorderLayout;
- import java.awt.Color;
- 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 javax.swing.JButton;
- import javax.swing.JFrame;
- import javax.swing.JOptionPane;
- import javax.swing.JPanel;
- import javax.swing.JTextArea;
- public class MySuperFrame extends JFrame implements ActionListener {
- final String SAVE_JBUTTON = "save";
- final String OPEN_JBUTTON = "open";
- private JPanel panel;
- private JButton save;
- private JButton open;
- private JTextArea textArea;
- public MySuperFrame() {
- super("Super Frame!!!");
- this.setSize(620, 480);
- this.panel = new JPanel();
- this.save = new JButton("Save");
- this.open = new JButton("Open");
- this.save.setActionCommand("save");
- this.textArea = new JTextArea();
- this.panel.setBackground(Color.black);
- this.panel.setLayout(new BorderLayout());
- JPanel buttons = new JPanel();
- buttons.setLayout(new GridLayout(1, 2));
- buttons.add(save);
- buttons.add(open);
- final String fileName = "tmp.txt";
- //this.save.addActionListener(this);
- //this.open.addActionListener(this);
- this.open.addActionListener(new ActionListener() {
- @Override
- public void actionPerformed(ActionEvent e) {
- //open.setBackground(Color.red);
- File f = new File(fileName);
- if (f.exists()) {
- try {
- FileReader fr = new FileReader(f);
- BufferedReader br = new BufferedReader(fr);
- String lines = "";
- String line = "";
- while ((line = br.readLine()) != null) {
- lines += line + "\n";
- }
- textArea.setText(lines);
- br.close();
- } catch (Exception ex) {
- JOptionPane.showMessageDialog(null, "File not found");
- }
- } else {
- JOptionPane.showMessageDialog(null, "File not found");
- }
- }
- });
- this.save.addActionListener(new ActionListener() {
- @Override
- public void actionPerformed(ActionEvent e) {
- File f = new File(fileName);
- try {
- FileWriter fw = new FileWriter(f);
- BufferedWriter bw = new BufferedWriter(fw);
- bw.write(textArea.getText());
- bw.flush();
- bw.close();
- } catch (Exception ex) {
- JOptionPane.showMessageDialog(null, ex.toString());
- }
- }
- });
- this.open.setActionCommand("open");
- //Observer ob = new Observer();
- //this.save.addActionListener(ob);
- this.panel.add(this.textArea, BorderLayout.CENTER);
- this.panel.add(buttons, BorderLayout.NORTH);
- this.setDefaultCloseOperation(EXIT_ON_CLOSE);
- this.add(this.panel);
- }
- public static void main(String[] args) {
- MySuperFrame frame = new MySuperFrame();
- frame.setVisible(true);
- }
- @Override
- public void actionPerformed(ActionEvent e) {
- switch (e.getActionCommand()) {
- case SAVE_JBUTTON: {
- JButton btn = (JButton) e.getSource();
- btn.setText("You clicked me");
- this.textArea.setText("Hello world!");
- break;
- }
- case OPEN_JBUTTON: {
- JButton btn = (JButton) e.getSource();
- btn.setText("Hello world!");
- this.textArea.setText("You clicked me");
- break;
- }
- default:
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement