Advertisement
Guest User

Untitled

a guest
Jun 26th, 2019
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.42 KB | None | 0 0
  1. import java.awt.BorderLayout;
  2. import java.awt.EventQueue;
  3. import javax.swing.JFrame;
  4. import javax.swing.JPanel;
  5. import javax.swing.border.EmptyBorder;
  6. import javax.swing.JTextField;
  7. import java.awt.FlowLayout;
  8. import javax.swing.JButton;
  9. import java.awt.event.ActionListener;
  10. import java.awt.event.ActionEvent;
  11. import javax.swing.JTextPane;
  12.  
  13. public class CalFrame extends JFrame {
  14.  
  15. private JPanel contentPane;
  16. private JTextField calText;
  17. String dis = "";
  18. Boolean addbol = false;
  19. Boolean subbol = false;
  20. Boolean eqlbol = false;
  21. private int TEMP;
  22. private int TEMP2;
  23. /**
  24. * Launch the application.
  25. */
  26. public static void main(String[] args) {
  27. EventQueue.invokeLater(new Runnable() {
  28. public void run() {
  29. try {
  30. CalFrame frame = new CalFrame();
  31. frame.setVisible(true);
  32. } catch (Exception e) {
  33. e.printStackTrace();
  34. }
  35. }
  36. });
  37. }
  38.  
  39. /**
  40. * Create the frame.
  41. */
  42. public CalFrame() {
  43. setTitle("Calculator");
  44. setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  45. setBounds(100, 100, 400, 527);
  46. contentPane = new JPanel();
  47. contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
  48. setContentPane(contentPane);
  49. contentPane.setLayout(null);
  50.  
  51. calText = new JTextField();
  52. calText.setBounds(10, 11, 364, 80);
  53. contentPane.add(calText);
  54. calText.setColumns(10);
  55.  
  56. JButton button1 = new JButton("1");
  57. button1.addActionListener(new ActionListener() {
  58. public void actionPerformed(ActionEvent arg0) {
  59.  
  60.  
  61. }
  62. });
  63. button1.setBounds(10, 140, 100, 50);
  64. contentPane.add(button1);
  65.  
  66. JButton button2 = new JButton("2");
  67. button2.addActionListener(new ActionListener() {
  68. public void actionPerformed(ActionEvent e) {
  69. }
  70. });
  71. button2.setBounds(140, 140, 100, 50);
  72. contentPane.add(button2);
  73.  
  74. JButton button3 = new JButton("3");
  75. button3.setBounds(274, 140, 100, 50);
  76. contentPane.add(button3);
  77.  
  78. JButton button4 = new JButton("4");
  79. button4.addActionListener(new ActionListener() {
  80. public void actionPerformed(ActionEvent e) {
  81. }
  82. });
  83. button4.setBounds(10, 210, 100, 50);
  84. contentPane.add(button4);
  85.  
  86. JButton button5 = new JButton("5");
  87. button5.setBounds(140, 210, 100, 50);
  88. contentPane.add(button5);
  89.  
  90. JButton button6 = new JButton("6");
  91. button6.setBounds(274, 210, 100, 50);
  92. contentPane.add(button6);
  93.  
  94. JButton button7 = new JButton("7");
  95. button7.setBounds(10, 280, 100, 50);
  96. contentPane.add(button7);
  97.  
  98. JButton button8 = new JButton("8");
  99. button8.setBounds(140, 280, 100, 50);
  100. contentPane.add(button8);
  101.  
  102. JButton button9 = new JButton("9");
  103. button9.addActionListener(new ActionListener() {
  104. public void actionPerformed(ActionEvent e) {
  105. }
  106. });
  107. button9.setBounds(274, 280, 100, 50);
  108. contentPane.add(button9);
  109.  
  110. JButton buttonPls = new JButton("+");
  111. buttonPls.setBounds(10, 350, 100, 50);
  112. contentPane.add(buttonPls);
  113.  
  114. JButton button0 = new JButton("0");
  115. button0.setBounds(140, 350, 100, 50);
  116. contentPane.add(button0);
  117.  
  118. JButton buttonMin = new JButton("-");
  119. buttonMin.setBounds(274, 350, 100, 50);
  120. contentPane.add(buttonMin);
  121.  
  122. JButton buttonEql = new JButton("=");
  123. buttonEql.setBounds(10, 420, 364, 50);
  124. contentPane.add(buttonEql);
  125.  
  126. button1.addActionListener(new Listen1());
  127. button2.addActionListener(new Listen2());
  128. button3.addActionListener(new Listen3());
  129. button4.addActionListener(new Listen4());
  130. button5.addActionListener(new Listen5());
  131. button6.addActionListener(new Listen6());
  132. button7.addActionListener(new Listen7());
  133. button8.addActionListener(new Listen8());
  134. button9.addActionListener(new Listen9());
  135. button0.addActionListener(new Listen0());
  136. buttonPls.addActionListener(new ListenPls());
  137. buttonMin.addActionListener(new ListenMin());
  138. buttonEql.addActionListener(new ListenEql());
  139. }
  140. class Clearr implements ActionListener {
  141. public void actionPerformed(ActionEvent e) {
  142.  
  143. calText.setText("");
  144. addbol = false;
  145. subbol = false;
  146. TEMP = 0;
  147. TEMP2 = 0;
  148. }
  149. }
  150.  
  151. class Listen1 implements ActionListener{
  152.  
  153.  
  154. @Override
  155. public void actionPerformed(ActionEvent e) {
  156. // TODO Auto-generated method stub
  157. dis = calText.getText();
  158. calText.setText(dis + "1");
  159.  
  160. }}
  161. class Listen2 implements ActionListener{
  162.  
  163.  
  164. @Override
  165. public void actionPerformed(ActionEvent e) {
  166. // TODO Auto-generated method stub
  167. dis = calText.getText();
  168. calText.setText(dis + "2");
  169.  
  170. }}
  171. class Listen3 implements ActionListener{
  172.  
  173.  
  174. @Override
  175. public void actionPerformed(ActionEvent e) {
  176. // TODO Auto-generated method stub
  177. dis = calText.getText();
  178. calText.setText(dis + "3");
  179.  
  180. }}
  181. class Listen4 implements ActionListener{
  182.  
  183.  
  184. @Override
  185. public void actionPerformed(ActionEvent e) {
  186. // TODO Auto-generated method stub
  187.  
  188. dis = calText.getText();
  189. calText.setText(dis + "4");
  190. }}
  191. class Listen5 implements ActionListener{
  192.  
  193.  
  194. @Override
  195. public void actionPerformed(ActionEvent e) {
  196. // TODO Auto-generated method stub
  197.  
  198. dis = calText.getText();
  199. calText.setText(dis + "5");
  200. }}
  201. class Listen6 implements ActionListener{
  202.  
  203.  
  204. @Override
  205. public void actionPerformed(ActionEvent e) {
  206. // TODO Auto-generated method stub
  207. dis = calText.getText();
  208. calText.setText(dis + "6");
  209.  
  210. }}
  211. class Listen7 implements ActionListener{
  212.  
  213.  
  214. @Override
  215. public void actionPerformed(ActionEvent e) {
  216. // TODO Auto-generated method stub
  217. dis = calText.getText();
  218. calText.setText(dis + "7");
  219.  
  220. }}
  221. class Listen8 implements ActionListener{
  222.  
  223.  
  224. @Override
  225. public void actionPerformed(ActionEvent e) {
  226. // TODO Auto-generated method stub
  227. dis = calText.getText();
  228. calText.setText(dis + "8");
  229.  
  230. }}
  231. class Listen9 implements ActionListener{
  232.  
  233.  
  234. @Override
  235. public void actionPerformed(ActionEvent e) {
  236. // TODO Auto-generated method stub
  237.  
  238. dis = calText.getText();
  239. calText.setText(dis + "9");
  240. }}
  241. class Listen0 implements ActionListener{
  242.  
  243.  
  244. @Override
  245. public void actionPerformed(ActionEvent e) {
  246. // TODO Auto-generated method stub
  247. dis = calText.getText();
  248. calText.setText(dis + "0");
  249.  
  250. }}
  251. class ListenPls implements ActionListener{
  252.  
  253.  
  254. @Override
  255. public void actionPerformed(ActionEvent e) {
  256. // TODO Auto-generated method stub
  257.  
  258. TEMP = Integer.parseInt(calText.getText());
  259. dis = calText.getText();
  260. calText.setText(dis + "+");
  261. addbol = true;
  262.  
  263.  
  264. }}
  265. class ListenMin implements ActionListener{
  266.  
  267.  
  268. @Override
  269. public void actionPerformed(ActionEvent e) {
  270. // TODO Auto-generated method stub
  271.  
  272. TEMP = Integer.parseInt(calText.getText());
  273. dis = calText.getText();
  274. calText.setText(dis + "-");
  275. subbol = true;
  276.  
  277. }}
  278. class ListenEql implements ActionListener{
  279.  
  280.  
  281. @Override
  282. public void actionPerformed(ActionEvent e) {
  283. // TODO Auto-generated method stub
  284. TEMP2 = Integer.parseInt(calText.getText());
  285. if (subbol == true)
  286. {
  287. if(calText.getText().contains("-"))
  288. {
  289. String[] splt = calText.getText().split("-");
  290. String prt1 = splt[0];
  291. String prt2 = splt[1];
  292.  
  293.  
  294. TEMP2=Integer.parseInt(prt1)-Integer.parseInt(prt2);
  295. TEMP2=TEMP2+TEMP;
  296.  
  297. calText.setText(dis+Integer.toString(TEMP2));
  298. }
  299. }
  300.  
  301.  
  302. else if ( addbol == true)
  303. {
  304.  
  305. if(calText.getText().contains("+"))
  306. {
  307. String[] splt = calText.getText().split("+");
  308. String prt1 = splt[0];
  309. String prt2 = splt[1];
  310.  
  311. TEMP2=Integer.parseInt(prt1)+Integer.parseInt(prt2);
  312. TEMP2=TEMP2+TEMP;
  313. calText.setText(dis+Integer.toString(TEMP2));
  314.  
  315. }
  316. }
  317.  
  318. /*TEMP2= Integer.parseInt(calText.getText());
  319. if (addbol == true)
  320. {
  321. TEMP2 = TEMP2 + TEMP;
  322. }
  323. else if ( subbol == true)
  324. {
  325. TEMP2 = TEMP2 - TEMP;
  326. }
  327. calText.setText(Integer.toString(TEMP2));
  328. addbol = false;
  329. subbol = false;
  330. */
  331.  
  332. subbol = false;
  333. addbol = false;
  334.  
  335. }}
  336.  
  337.  
  338. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement