Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.awt.BorderLayout;
- import java.awt.Color;
- import java.awt.Container;
- import java.awt.Event;
- import java.awt.FlowLayout;
- import java.awt.GridBagLayout;
- import java.awt.event.ActionEvent;
- import java.awt.event.ActionListener;
- import java.awt.event.ActionListener.*;
- import java.io.BufferedReader;
- import java.io.FileNotFoundException;
- import java.io.FileReader;
- import java.io.IOException;
- import java.io.PrintWriter;
- import javax.swing.JButton;
- import javax.swing.JFrame;
- import javax.swing.JLabel;
- import javax.swing.JOptionPane;
- import javax.swing.JPanel;
- import javax.swing.JScrollPane;
- import javax.swing.JTextArea;
- import javax.swing.JTextField;
- //import TextStats.ButtonListener;
- public class JavaPad extends JFrame
- {
- public final static int WIDTH = 600;
- public final static int HEIGHT = 500;
- private Container contentPane;
- private JScrollPane scroll;
- private JLabel label;
- private JButton newButton, saveButton, loadButton, quitButton;
- private ButtonListener buttonListener;
- private JTextArea display;
- private JTextArea fieldOne;
- private JPanel topPanel, centerPanel, bottomPanel;
- private JFrame frame;
- private Event event;
- public JavaPad()
- {
- setTitle("Macrosoft JavaPad XP");// sets title
- this.setSize(WIDTH, HEIGHT);
- this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);// choices default
- // close
- this.setLayout(new BorderLayout());// sets layout
- contentPane = this.getContentPane();// instantiates container
- contentPane.setLayout(new BorderLayout());
- createTopPanel();
- contentPane.add(topPanel, BorderLayout.NORTH);
- createCenterPanel();
- contentPane.add(centerPanel, BorderLayout.CENTER);
- createBottomPanel();
- contentPane.add(bottomPanel, BorderLayout.SOUTH);
- setVisible(true);
- }
- private void createTopPanel()
- {
- buttonListener = new ButtonListener();
- topPanel = new JPanel();
- topPanel.setLayout(new FlowLayout());
- newButton = new JButton("new");
- saveButton = new JButton("save");
- loadButton = new JButton("load");
- quitButton = new JButton("quit");
- newButton.addActionListener(buttonListener);
- saveButton.addActionListener(buttonListener);
- loadButton.addActionListener(buttonListener);
- quitButton.addActionListener(buttonListener);
- topPanel.add(newButton);
- topPanel.add(saveButton);
- topPanel.add(loadButton);
- topPanel.add(quitButton);
- }
- private void createCenterPanel()
- {
- centerPanel = new JPanel();
- centerPanel.setLayout(new FlowLayout());
- fieldOne = new JTextArea(" ", 50, 50);
- fieldOne.setSize(100, 200);
- scroll = new JScrollPane(fieldOne);
- centerPanel.add(scroll);
- }
- private void createBottomPanel()
- {
- bottomPanel = new JPanel();
- bottomPanel.setLayout(new FlowLayout());
- label = new JLabel("Macrosoft: Resistance is futile.");
- bottomPanel.add(label);
- }
- private void saveToFile()
- {
- try
- {
- PrintWriter save = new PrintWriter("HardCode.txt");
- save.print(fieldOne.getText());
- save.close();
- } catch (FileNotFoundException exception)
- {
- System.out.print(exception.getMessage());
- }
- }
- private void quit()
- {
- int dialogButton = JOptionPane.YES_NO_OPTION;
- dialogButton = JOptionPane.showConfirmDialog(null, "Quitting: Save?", "Quit", dialogButton);
- if (JOptionPane.showConfirmDialog(null, "Quitting: Save?", "Quit",
- JOptionPane.YES_NO_OPTION) == JOptionPane.YES_OPTION)
- {
- System.exit(0);
- // yes option
- } else
- {
- remove(dialogButton);
- System.exit(0);
- // no option
- }
- }
- private class ButtonListener implements ActionListener
- {
- public void actionPerformed(ActionEvent event)
- {
- String actionCommand = event.getActionCommand();
- if (actionCommand.equals("new"))
- {
- fieldOne.setText(null);
- } else if (actionCommand.equals("save"))
- {
- saveToFile();
- } else if (actionCommand.equals("load"))
- {
- try
- {
- BufferedReader reader = new BufferedReader(new FileReader("HardCode.txt"));
- fieldOne.setText("");
- String text = reader.readLine();
- System.out.println("try block");
- while (text != null)
- {
- fieldOne.append(text + "\n");
- text = reader.readLine();
- }
- reader.close();
- } catch (IOException exception)
- {
- System.out.println(exception.getMessage());
- }
- } else if (actionCommand.equals("quit"))
- {
- quit();
- }
- setVisible(true);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement