Advertisement
Guest User

GUI

a guest
Feb 23rd, 2017
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.03 KB | None | 0 0
  1. import java.awt.BorderLayout;
  2. import java.awt.Color;
  3. import java.awt.Container;
  4. import java.awt.Event;
  5. import java.awt.FlowLayout;
  6. import java.awt.GridBagLayout;
  7. import java.awt.event.ActionEvent;
  8. import java.awt.event.ActionListener;
  9. import java.awt.event.ActionListener.*;
  10. import java.io.BufferedReader;
  11. import java.io.FileNotFoundException;
  12. import java.io.FileReader;
  13. import java.io.IOException;
  14. import java.io.PrintWriter;
  15.  
  16. import javax.swing.JButton;
  17. import javax.swing.JFrame;
  18. import javax.swing.JLabel;
  19. import javax.swing.JOptionPane;
  20. import javax.swing.JPanel;
  21. import javax.swing.JScrollPane;
  22. import javax.swing.JTextArea;
  23. import javax.swing.JTextField;
  24.  
  25. //import TextStats.ButtonListener;
  26.  
  27. public class JavaPad extends JFrame
  28. {
  29. public final static int WIDTH = 600;
  30. public final static int HEIGHT = 500;
  31.  
  32. private Container contentPane;
  33. private JScrollPane scroll;
  34. private JLabel label;
  35. private JButton newButton, saveButton, loadButton, quitButton;
  36. private ButtonListener buttonListener;
  37. private JTextArea display;
  38. private JTextArea fieldOne;
  39. private JPanel topPanel, centerPanel, bottomPanel;
  40. private JFrame frame;
  41. private Event event;
  42.  
  43. public JavaPad()
  44. {
  45. setTitle("Macrosoft JavaPad XP");// sets title
  46. this.setSize(WIDTH, HEIGHT);
  47. this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);// choices default
  48. // close
  49. this.setLayout(new BorderLayout());// sets layout
  50.  
  51. contentPane = this.getContentPane();// instantiates container
  52. contentPane.setLayout(new BorderLayout());
  53.  
  54. createTopPanel();
  55. contentPane.add(topPanel, BorderLayout.NORTH);
  56. createCenterPanel();
  57. contentPane.add(centerPanel, BorderLayout.CENTER);
  58. createBottomPanel();
  59. contentPane.add(bottomPanel, BorderLayout.SOUTH);
  60.  
  61. setVisible(true);
  62. }
  63.  
  64. private void createTopPanel()
  65. {
  66. buttonListener = new ButtonListener();
  67. topPanel = new JPanel();
  68. topPanel.setLayout(new FlowLayout());
  69.  
  70. newButton = new JButton("new");
  71. saveButton = new JButton("save");
  72. loadButton = new JButton("load");
  73. quitButton = new JButton("quit");
  74.  
  75. newButton.addActionListener(buttonListener);
  76. saveButton.addActionListener(buttonListener);
  77. loadButton.addActionListener(buttonListener);
  78. quitButton.addActionListener(buttonListener);
  79.  
  80. topPanel.add(newButton);
  81. topPanel.add(saveButton);
  82. topPanel.add(loadButton);
  83. topPanel.add(quitButton);
  84.  
  85. }
  86.  
  87. private void createCenterPanel()
  88. {
  89.  
  90. centerPanel = new JPanel();
  91. centerPanel.setLayout(new FlowLayout());
  92. fieldOne = new JTextArea(" ", 50, 50);
  93. fieldOne.setSize(100, 200);
  94. scroll = new JScrollPane(fieldOne);
  95. centerPanel.add(scroll);
  96. }
  97.  
  98. private void createBottomPanel()
  99. {
  100. bottomPanel = new JPanel();
  101. bottomPanel.setLayout(new FlowLayout());
  102. label = new JLabel("Macrosoft: Resistance is futile.");
  103. bottomPanel.add(label);
  104. }
  105.  
  106. private void saveToFile()
  107. {
  108. try
  109. {
  110. PrintWriter save = new PrintWriter("HardCode.txt");
  111. save.print(fieldOne.getText());
  112. save.close();
  113. } catch (FileNotFoundException exception)
  114. {
  115. System.out.print(exception.getMessage());
  116.  
  117. }
  118.  
  119. }
  120.  
  121. private void quit()
  122. {
  123. int dialogButton = JOptionPane.YES_NO_OPTION;
  124. dialogButton = JOptionPane.showConfirmDialog(null, "Quitting: Save?", "Quit", dialogButton);
  125. if (JOptionPane.showConfirmDialog(null, "Quitting: Save?", "Quit",
  126. JOptionPane.YES_NO_OPTION) == JOptionPane.YES_OPTION)
  127. {
  128. System.exit(0);
  129. // yes option
  130. } else
  131. {
  132. remove(dialogButton);
  133. System.exit(0);
  134. // no option
  135. }
  136. }
  137.  
  138. private class ButtonListener implements ActionListener
  139. {
  140. public void actionPerformed(ActionEvent event)
  141. {
  142.  
  143. String actionCommand = event.getActionCommand();
  144.  
  145. if (actionCommand.equals("new"))
  146. {
  147. fieldOne.setText(null);
  148. } else if (actionCommand.equals("save"))
  149. {
  150. saveToFile();
  151. } else if (actionCommand.equals("load"))
  152. {
  153. try
  154. {
  155. BufferedReader reader = new BufferedReader(new FileReader("HardCode.txt"));
  156. fieldOne.setText("");
  157. String text = reader.readLine();
  158. System.out.println("try block");
  159. while (text != null)
  160. {
  161. fieldOne.append(text + "\n");
  162. text = reader.readLine();
  163. }
  164. reader.close();
  165. } catch (IOException exception)
  166. {
  167. System.out.println(exception.getMessage());
  168. }
  169.  
  170. } else if (actionCommand.equals("quit"))
  171. {
  172. quit();
  173. }
  174.  
  175. setVisible(true);
  176.  
  177. }
  178. }
  179.  
  180. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement