Advertisement
Guest User

Untitled

a guest
Nov 20th, 2018
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.82 KB | None | 0 0
  1. This class is the stand-alone class .it does all the work
  2. N. Gattuso -- 01/13/2014
  3. ****************************************************************** */
  4. import java.awt.*;
  5. import java.awt.event.*;
  6. import javax.swing.*;
  7.  
  8. public class MyWindowFrame extends JFrame {
  9.  
  10. private Container pane;
  11. private JTextField textField;
  12.  
  13. //The constructor
  14. public MyWindowFrame() {
  15.  
  16. //create a pane to work in..
  17. pane=getContentPane();
  18. //set the layout of the pane to null
  19. pane.setLayout(null);
  20.  
  21. JMenuBar menuBar = new JMenuBar();
  22. JMenu menuFile = new JMenu();
  23. JMenuItem menuFileExit = new JMenuItem();
  24.  
  25. menuFile.setText("File");
  26. menuFileExit.setText("Exit");
  27.  
  28. // Add action listener.for the menu button
  29. menuFileExit.addActionListener
  30.  
  31. (
  32. new ActionListener() {
  33. public void actionPerformed(ActionEvent e) {
  34. MyWindowFrame.this.windowClosed();
  35. }
  36. }
  37. );
  38.  
  39.  
  40.  
  41. menuFile.add(menuFileExit);
  42. menuBar.add(menuFile);
  43.  
  44. setTitle("Calculator");
  45. setJMenuBar(menuBar);
  46. setSize(new Dimension(600, 1000));
  47.  
  48.  
  49. JButton zero = new JButton("0");
  50. pane.add(zero);
  51. zero.setLocation(50, 850);
  52. zero.setSize(300, 100);
  53. zero.addActionListener
  54. (
  55. new ActionListener() {
  56. public void actionPerformed(ActionEvent e) {
  57. String text = textField.getText();
  58. zero.setText(text + "0");
  59. }
  60. }
  61. );
  62.  
  63.  
  64.  
  65. JButton one= new JButton("1");
  66. pane.add(one);
  67. one.setLocation(50, 750);
  68. one.setSize(100, 100);
  69. one.addActionListener
  70. (
  71. new ActionListener() {
  72. public void actionPerformed(ActionEvent e) {
  73.  
  74. String text = textField.getText();
  75. one.setText(text + "1");
  76.  
  77. }
  78. }
  79. );
  80.  
  81.  
  82.  
  83.  
  84.  
  85. JButton two= new JButton("2");
  86. pane.add(two);
  87. two.setLocation(150, 750);
  88. two.setSize(100, 100);
  89. two.addActionListener
  90. (
  91. new ActionListener() {
  92. public void actionPerformed(ActionEvent e) {
  93. String text = textField.getText();
  94. two.setText(text + "1");
  95.  
  96. }
  97. }
  98. );
  99.  
  100.  
  101.  
  102.  
  103. JButton four = new JButton("4");
  104. pane.add(four);
  105. four.setLocation(50, 650);
  106. four.setSize(100, 100);
  107. four.addActionListener
  108. (
  109. new ActionListener() {
  110. public void actionPerformed(ActionEvent e) {
  111. String text = textField.getText();
  112. four.setText(text + "1");
  113. }
  114. }
  115. );
  116.  
  117.  
  118.  
  119.  
  120.  
  121. JButton five = new JButton("5");
  122. pane.add(five);
  123. five.setLocation(150, 650);
  124. five.setSize(100, 100);
  125. five.addActionListener
  126. (
  127. new ActionListener() {
  128. public void actionPerformed(ActionEvent e) {
  129. String text = textField.getText();
  130. five.setText(text + "1");
  131. }
  132. }
  133. );
  134.  
  135.  
  136.  
  137.  
  138.  
  139. JButton six = new JButton("6");
  140. pane.add(six);
  141. six.setLocation(250, 650);
  142. six.setSize(100, 100);
  143. six.addActionListener
  144. (
  145. new ActionListener() {
  146. public void actionPerformed(ActionEvent e) {
  147. String text = textField.getText();
  148. six.setText(text + "6");
  149. }
  150. }
  151. );
  152.  
  153.  
  154.  
  155.  
  156.  
  157.  
  158. JButton seven = new JButton("7");
  159. pane.add(seven);
  160. seven.setLocation(50, 550);
  161. seven.setSize(100, 100);
  162. seven.addActionListener
  163. (
  164. new ActionListener() {
  165. public void actionPerformed(ActionEvent e) {
  166. String text = textField.getText();
  167. seven.setText(text + "7");
  168. }
  169. }
  170. );
  171.  
  172.  
  173.  
  174.  
  175. JButton eight = new JButton("8");
  176. pane.add(eight);
  177. eight.setLocation(150, 550);
  178. eight.setSize(100, 100);
  179. eight.addActionListener
  180. (
  181. new ActionListener() {
  182. public void actionPerformed(ActionEvent e) {
  183. String text = textField.getText();
  184. eight.setText(text + "8");
  185. }
  186. }
  187. );
  188.  
  189.  
  190.  
  191.  
  192.  
  193.  
  194.  
  195.  
  196.  
  197.  
  198.  
  199. JButton nine= new JButton("9");
  200. pane.add(nine);
  201. nine.setLocation(250, 550);
  202. nine.setSize(100, 100);
  203. nine.addActionListener
  204. (
  205. new ActionListener() {
  206. public void actionPerformed(ActionEvent e) {
  207. String text = textField.getText();
  208. nine.setText(text + "9");
  209. }
  210. }
  211. );
  212.  
  213.  
  214.  
  215.  
  216.  
  217.  
  218.  
  219. JButton minus= new JButton("--");
  220. pane.add(minus);
  221. minus.setLocation(450, 650);
  222. minus.setSize(100, 100);
  223. minus.addActionListener
  224. (
  225. new ActionListener() {
  226. public void actionPerformed(ActionEvent e) {
  227. String text = textField.getText();
  228. minus.setText(text + "--");
  229. }
  230. }
  231. );
  232.  
  233.  
  234.  
  235.  
  236.  
  237. JButton divide= new JButton("÷");
  238. pane.add(divide);
  239. divide.setLocation(450, 450);
  240. divide.setSize(100, 100);
  241. divide.addActionListener
  242. (
  243. new ActionListener() {
  244. public void actionPerformed(ActionEvent e) {
  245. String text = textField.getText();
  246. divide.setText(text + "÷");
  247. }
  248. }
  249. );
  250.  
  251.  
  252.  
  253.  
  254.  
  255. JButton times= new JButton("x");
  256. pane.add(times);
  257. times.setLocation(450, 550);
  258. times.setSize(100, 100);
  259. times.addActionListener
  260. (
  261. new ActionListener() {
  262. public void actionPerformed(ActionEvent e) {
  263. String text = textField.getText();
  264. times.setText(text + "x");
  265. }
  266. }
  267. );
  268.  
  269.  
  270.  
  271.  
  272.  
  273.  
  274.  
  275.  
  276. JButton three= new JButton("3");
  277. pane.add(three);
  278. three.setLocation(250, 750);
  279. three.setSize(100, 100);
  280. three.addActionListener
  281. (
  282. new ActionListener() {
  283. public void actionPerformed(ActionEvent e) {
  284. String text = textField.getText();
  285. three.setText(text + "3");
  286. }
  287. }
  288. );
  289.  
  290.  
  291.  
  292.  
  293.  
  294.  
  295.  
  296.  
  297.  
  298. JButton equals = new JButton("=");
  299. pane.add(equals);
  300. equals.setLocation(450, 850);
  301. equals.setSize(100, 100);
  302. equals.addActionListener
  303. (
  304. new ActionListener() {
  305. public void actionPerformed(ActionEvent e) {
  306. String text = textField.getText();
  307. equals.setText(text + "=");
  308. }
  309. }
  310. );
  311.  
  312.  
  313.  
  314.  
  315.  
  316. JButton button1 = new JButton("+");
  317. pane.add(button1);
  318. button1.setLocation(450, 750);
  319. button1.setSize(100, 100);
  320. button1.addActionListener
  321. (
  322. new ActionListener() {
  323. public void actionPerformed(ActionEvent e) {
  324. String text = textField.getText();
  325. button1.setText(text + "+");
  326. }
  327. }
  328. );
  329.  
  330.  
  331.  
  332.  
  333.  
  334.  
  335. // Add window listener. to window
  336. this.addWindowListener
  337. (
  338. new WindowAdapter() {
  339. public void windowClosing(WindowEvent e) {
  340. MyWindowFrame.this.windowClosed();
  341. }
  342. }
  343. );
  344.  
  345.  
  346. }
  347.  
  348.  
  349. /**
  350. * Shutdown procedure when run as an application.
  351. */
  352. protected void windowClosed() {
  353.  
  354. // TODO: Check if it is safe to close the application
  355.  
  356. // Exit application.
  357. System.exit(0);
  358. }
  359. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement