Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package javaapplication35;
- import javax.swing.*;
- import java.awt.*;
- import java.awt.event.*;
- import java.io.BufferedReader;
- import java.io.BufferedWriter;
- import java.io.File;
- import java.io.FileReader;
- import java.io.FileWriter;
- public class MySuperFrame extends JFrame implements ActionListener {
- private JPanel panel;
- private JButton open;
- private JButton save;
- private JTextArea textArea;
- static final String OPEN_ACTION = "OPEN";
- static final String SAVE_ACTION = "SAVE";
- public MySuperFrame() {
- //super("My super frame!!!");
- this.setTitle("My super frame!!!");
- this.setDefaultCloseOperation(EXIT_ON_CLOSE);
- this.panel = new JPanel();
- this.panel.setLayout(new BorderLayout());
- JPanel grid = new JPanel();
- grid.setLayout(new GridLayout(1, 2));
- this.open = new JButton();
- this.save = new JButton();
- this.open.setActionCommand("OPEN");
- this.save.setActionCommand("SAVE");
- //this.open.addActionListener(this);
- //this.open.addActionListener(new Observer(open));
- //this.save.addActionListener(this);
- final String file = "tmp.txt";
- this.open.addActionListener(new ActionListener() {
- @Override
- public void actionPerformed(ActionEvent e) {
- File f = new File(file);
- 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";
- }
- br.close();
- textArea.setText(lines);
- } catch (Exception ex) {
- JOptionPane.showMessageDialog(null, ex.toString(), "ERROR!", JOptionPane.ERROR_MESSAGE);
- }
- } else {
- JOptionPane.showMessageDialog(null, "File not present");
- }
- }
- });
- this.save.addActionListener(new ActionListener() {
- @Override
- public void actionPerformed(ActionEvent e) {
- try {
- FileWriter fw = new FileWriter(file);
- BufferedWriter bw = new BufferedWriter(fw);
- bw.write(textArea.getText());
- bw.flush();
- bw.close();
- } catch (Exception ex) {
- JOptionPane.showMessageDialog(null, ex.toString(), "ERROR!", JOptionPane.ERROR_MESSAGE);
- }
- }
- });
- this.open.setText("OPEN");
- this.save.setText("SAVE");
- this.textArea = new JTextArea();
- this.setSize(640, 480);
- grid.add(this.open);
- grid.add(this.save);
- this.panel.add(grid, BorderLayout.NORTH);
- this.panel.add(this.textArea, BorderLayout.CENTER);
- this.add(this.panel);
- }
- @Override
- public void actionPerformed(ActionEvent evt) {
- switch (evt.getActionCommand()) {
- case MySuperFrame.SAVE_ACTION: {
- JButton btn = (JButton) evt.getSource();
- btn.setText("You clicked save");
- break;
- }
- case MySuperFrame.OPEN_ACTION: {
- JButton btn = (JButton) evt.getSource();
- btn.setText("You clicked open");
- break;
- }
- }
- }
- public static void main(String[] args) {
- MySuperFrame frame = new MySuperFrame();
- frame.setVisible(true);
- MySuperFrame frame2 = new MySuperFrame();
- frame2.setVisible(true);
- }
- }
- /*
- * To change this template, choose Tools | Templates
- * and open the template in the editor.
- */
- package javaapplication35;
- import java.util.ArrayList;
- public class Observable {
- private ArrayList<IObserver> observers;
- public Observable(){
- this.observers = new ArrayList<>();
- }
- public void subscribeObserver(IObserver observer){
- this.observers.add(observer);
- }
- public void onAction(String action){
- for(IObserver observer: this.observers){
- observer.onEvent(new Event(this,action));
- }
- }
- }
- /*
- * To change this template, choose Tools | Templates
- * and open the template in the editor.
- */
- package javaapplication35;
- import java.awt.event.ActionEvent;
- import java.awt.event.ActionListener;
- import javax.swing.JButton;
- /**
- *
- * @author didiamantis
- */
- public class Observer implements ActionListener{
- private JButton btn;
- public Observer(JButton btn){
- this.btn = btn;
- }
- @Override
- public void actionPerformed(ActionEvent e) {
- btn.setText("Hello");
- System.out.println(e.getActionCommand());
- }
- }
- /*
- * To change this template, choose Tools | Templates
- * and open the template in the editor.
- */
- package javaapplication35;
- public interface IObserver {
- public void onEvent(Event event);
- }
- /*
- * To change this template, choose Tools | Templates
- * and open the template in the editor.
- */
- package javaapplication35;
- public class Event {
- private Object source;
- private String action;
- public Event(Object source,String action){
- this.action = action;
- this.source = source;
- }
- public Object getSource() {
- return source;
- }
- public void setSource(Object source) {
- this.source = source;
- }
- public String getAction() {
- return action;
- }
- public void setAction(String action) {
- this.action = action;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement