Advertisement
Guest User

Untitled

a guest
Feb 27th, 2015
184
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 25.67 KB | None | 0 0
  1. import javax.swing.*;
  2. import java.awt.*;
  3. import java.awt.event.*;
  4. import java.math.*;
  5. import java.util.*;
  6. import java.math.BigDecimal;
  7. import java.io.*;
  8.  
  9. public class Kalkulator {
  10. JFrame frame;
  11. JPanel mainPanel;
  12. JPanel displayPanel;
  13. JPanel sidePanel;
  14. static JTextArea mainDisplay;
  15. // main display, holds only recently introduced value
  16. static JTextArea secDisplay;
  17. // secondary display, holds whole operation date, until use of "="
  18. static JTextArea sideTextArea;
  19. static JTextField sideTextField;
  20.  
  21. private static String numValue;
  22. private static ArrayList<BigDecimal> memory; // working memory of calculator
  23. private static ArrayList<String> operation;
  24. private static BigDecimal result;
  25. private BigDecimal memoryStore; // function of memory, holds one value
  26. private static Integer prec;
  27. private static boolean root;
  28. ImageIcon img;
  29.  
  30. JCheckBoxMenuItem wrap;
  31. JCheckBoxMenuItem history;
  32.  
  33. public static void main(String[] args) {
  34. Kalkulator calc = new Kalkulator();
  35. calc.go();
  36. }
  37.  
  38. public void go() {
  39.  
  40. numValue = ""; // String with currently introduced values
  41. memory = new ArrayList<BigDecimal>();
  42. // holds String with values for operations
  43. operation = new ArrayList<String>(); // holds symbols of operations
  44. prec = 15; // decimal point accuracy,
  45. memoryStore = new BigDecimal(0); // BigDecimal keeps one value in memory
  46. root = false; // controls blocking of buttons
  47.  
  48. /** GUI **/
  49. img = new ImageIcon(getClass().getResource("icon.png"));
  50. frame = new JFrame("Calculator");
  51. frame.setIconImage(img.getImage());
  52. frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
  53. mainPanel = new JPanel();
  54. frame.getContentPane().add(BorderLayout.CENTER, mainPanel);
  55.  
  56. mainPanel.setLayout(new GridLayout(6, 4));
  57. mainPanel.setBackground(Color.GRAY);
  58.  
  59. ArrayList<JButton> jButtons = new ArrayList<JButton>();
  60. // simplify a configuration of buttons
  61.  
  62. /* menu */
  63. JMenuBar menuBar = new JMenuBar();
  64. JMenu options = new JMenu("Options");
  65. JMenu help = new JMenu("Help");
  66. JMenuItem viewHelp = new JMenuItem("Help");
  67. viewHelp.addActionListener(new helpActList());
  68. JMenuItem info = new JMenuItem("Information");
  69. info.addActionListener(new infoActList());
  70. wrap = new JCheckBoxMenuItem("Wrap lines");
  71. wrap.addItemListener(new wrapList());
  72. history = new JCheckBoxMenuItem("Results history");
  73. history.addActionListener(new histList());
  74. JMenuItem precison = new JMenuItem("Accuracy");
  75. precison.addActionListener(new precList());
  76.  
  77. menuBar.add(options);
  78. menuBar.add(help);
  79. options.add(wrap);
  80. options.add(history);
  81. options.add(precison);
  82. help.add(viewHelp);
  83. help.add(info);
  84. frame.setJMenuBar(menuBar);
  85.  
  86. /* Fonts */
  87. Font mainDisplayFont = new Font("Ariala", Font.BOLD, 20);
  88. Font secDisplayFont = new Font("Ariala", Font.PLAIN, 10);
  89. Font buttonsFont = new Font("Ariala Black", Font.PLAIN, 20);
  90. /* wyświetlacze */
  91. mainDisplay = new JTextArea(2, 10);
  92. mainDisplay.setFont(mainDisplayFont);
  93. mainDisplay.setText("");
  94. mainDisplay.setEditable(false);
  95. JScrollPane scrollPane = new JScrollPane(mainDisplay);
  96.  
  97. scrollPane
  98. .setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED);
  99. scrollPane
  100. .setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_AS_NEEDED);
  101.  
  102. secDisplay = new JTextArea(1, 20);
  103. secDisplay.setFont(secDisplayFont);
  104. secDisplay.setText(numValue);
  105. secDisplay.setEditable(false);
  106.  
  107. displayPanel = new JPanel();
  108. displayPanel.setLayout(new BorderLayout());
  109. frame.getContentPane().add(BorderLayout.NORTH, displayPanel);
  110. displayPanel.add(BorderLayout.NORTH, secDisplay);
  111. displayPanel.add(BorderLayout.CENTER, scrollPane);
  112. displayPanel.setBorder(BorderFactory.createBevelBorder(1, Color.GRAY,
  113. Color.LIGHT_GRAY));
  114.  
  115. /* buttons */
  116. JButton C = new JButton("C");
  117. C.addActionListener(new CActList());
  118. jButtons.add(C);
  119.  
  120. JButton MC = new JButton("MC");
  121. MC.addActionListener(new MCActList());
  122. jButtons.add(MC);
  123.  
  124. JButton MS = new JButton("MS");
  125. MS.addActionListener(new MSActList());
  126. jButtons.add(MS);
  127.  
  128. JButton MR = new JButton("MR");
  129. MR.addActionListener(new MRActList());
  130. jButtons.add(MR);
  131.  
  132. JButton percent = new JButton("%");
  133. percent.addActionListener(new percentActList());
  134. jButtons.add(percent);
  135.  
  136. JButton rootSquere = new JButton("u221A");
  137. rootSquere.addActionListener(new rootActList());
  138. jButtons.add(rootSquere);
  139.  
  140. JButton nPower = new JButton("x" + "u207F");
  141. nPower.addActionListener(new nActList());
  142. jButtons.add(nPower);
  143.  
  144. JButton plusminus = new JButton("u00B1");
  145. plusminus.addActionListener(new pmActList());
  146. jButtons.add(plusminus);
  147.  
  148. JButton B7 = new JButton("7");
  149. B7.addActionListener(new B7ActList());
  150. jButtons.add(B7);
  151.  
  152. JButton B8 = new JButton("8");
  153. B8.addActionListener(new B8ActList());
  154. jButtons.add(B8);
  155.  
  156. JButton B9 = new JButton("9");
  157. B9.addActionListener(new B9ActList());
  158. jButtons.add(B9);
  159.  
  160. JButton Bplus = new JButton("+");
  161. Bplus.addActionListener(new BplusActList());
  162. jButtons.add(Bplus);
  163.  
  164. JButton B4 = new JButton("4");
  165. B4.addActionListener(new B4ActList());
  166. jButtons.add(B4);
  167.  
  168. JButton B5 = new JButton("5");
  169. B5.addActionListener(new B5ActList());
  170. jButtons.add(B5);
  171.  
  172. JButton B6 = new JButton("6");
  173. B6.addActionListener(new B6ActList());
  174. jButtons.add(B6);
  175.  
  176. JButton Bmulti = new JButton("u00D7");
  177. Bmulti.addActionListener(new BmultiActList());
  178. jButtons.add(Bmulti);
  179.  
  180. JButton B1 = new JButton("1");
  181. B1.addActionListener(new B1ActList());
  182. jButtons.add(B1);
  183.  
  184. JButton B2 = new JButton("2");
  185. B2.addActionListener(new B2ActList());
  186. jButtons.add(B2);
  187.  
  188. JButton B3 = new JButton("3");
  189. B3.addActionListener(new B3ActList());
  190. jButtons.add(B3);
  191.  
  192. JButton Bdiv = new JButton(":");
  193. Bdiv.addActionListener(new BdivActList());
  194. jButtons.add(Bdiv);
  195.  
  196. JButton B0 = new JButton("0");
  197. B0.addActionListener(new B0ActList());
  198. jButtons.add(B0);
  199.  
  200. JButton Bpoint = new JButton(".");
  201. Bpoint.addActionListener(new BpointActList());
  202. jButtons.add(Bpoint);
  203.  
  204. JButton Bequal = new JButton("=");
  205. Bequal.addActionListener(new BequalActList());
  206. jButtons.add(Bequal);
  207.  
  208. JButton Bminus = new JButton("-");
  209. Bminus.addActionListener(new BminusActList());
  210. jButtons.add(Bminus);
  211.  
  212. for (JButton button : jButtons) {
  213. button.setFont(buttonsFont);
  214. button.setBorder(BorderFactory.createBevelBorder(1, Color.GRAY,
  215. Color.LIGHT_GRAY));
  216. mainPanel.add(button);
  217. }
  218.  
  219. /* Results history - holds results of former operations */
  220.  
  221. sidePanel = new JPanel();
  222. JPanel sideTextPanel = new JPanel();
  223. sideTextPanel.setLayout(new BorderLayout());
  224. sidePanel.setBackground(Color.WHITE);
  225. sideTextField = new JTextField("M: ", 7);
  226. sideTextArea = new JTextArea(11, 7);
  227. sideTextField.setBorder(BorderFactory.createLineBorder(Color.GRAY));
  228. sideTextField.setEditable(false);
  229. sideTextArea.setEditable(false);
  230. sideTextArea.setLineWrap(true);
  231. JScrollPane sideScroll = new JScrollPane(sideTextArea);
  232. sideScroll.setBorder(BorderFactory.createLineBorder(Color.LIGHT_GRAY));
  233. sideTextPanel.add(BorderLayout.NORTH, sideTextField);
  234. sideTextPanel.add(BorderLayout.CENTER, sideScroll);
  235. sideScroll
  236. .setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
  237. sideScroll
  238. .setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED);
  239. sidePanel.add(sideTextPanel);
  240. frame.getContentPane().add(BorderLayout.EAST, sidePanel);
  241. sidePanel.setVisible(false);
  242.  
  243. frame.setSize(250, 325);
  244. frame.setResizable(false);
  245. frame.setLocationByPlatform(true);
  246. frame.setVisible(true);
  247. }
  248.  
  249. static void numButton(String s) {
  250. // reaction for use of button: adding number to displays and memory
  251. numValue += s;
  252. mainDisplay.setText(numValue);
  253. secDisplay.append(s);
  254. root = false; // unblocking of buttons block
  255. }
  256.  
  257. static void numOperation(String n, String s) {
  258. // controls operation on numbers, without math
  259. root = false; // n - number/symbol of operation (0=,1+,2-,3*,4/,5^,6%);
  260. // s - string, displayed symbol
  261. if (operation.isEmpty()) {
  262. memory.add(new BigDecimal(numValue));
  263. secDisplay.append(s);
  264. numValue = "";
  265. operation.add(0, n);
  266. /* if user want to continue counting on result of former operations */
  267. } else if (operation.get(0).equals("0")) {
  268. memory.add(new BigDecimal(mainDisplay.getText()));
  269. secDisplay.setText(mainDisplay.getText());
  270. numValue = "";
  271. operation.add(0, n);
  272. secDisplay.append(s);
  273. //if user do more than one operation, without usage of "=" -
  274. //displays mid-result after every operation
  275.  
  276. } else if ((!operation.isEmpty()) && (!operation.get(0).equals("0"))) {
  277. try {
  278. memory.add(new BigDecimal(numValue));
  279. numValue = "";
  280. result = opEqual(memory, operation);
  281. mainDisplay.setText(result.toString());
  282. memory.removeAll(memory);
  283. memory.add(new BigDecimal(mainDisplay.getText()));
  284. operation.add(0, n);
  285. secDisplay.setText("(" + secDisplay.getText() + ")" + s);
  286. } catch (NumberFormatException ex) {
  287. ex.printStackTrace();
  288. /* if user decide to change current operation - change symbol */
  289. if (!operation.get(0).equals(n)) {
  290. operation.add(0, n);
  291. String error = secDisplay.getText();
  292. secDisplay.setText(error.substring(0, error.length() - 1)
  293. + s);
  294. }
  295. }
  296. }
  297. }
  298.  
  299. static void block() {
  300. // if buttons are blocked(there is final value on display) use of next
  301. // button cause a display reset
  302. if (root) {
  303. secDisplay.setText("");
  304. mainDisplay.setText("");
  305. numValue = "";
  306. }
  307. }
  308.  
  309. static BigDecimal opEqual(ArrayList<BigDecimal> list, ArrayList<String> s) {
  310. // operations on numbers
  311. BigDecimal temp = new BigDecimal(0);
  312. int op = Integer.parseInt(s.get(0));
  313. switch (op) {
  314. case 1:
  315. temp = list.get(0).add(list.get(1));
  316. break;
  317. case 2:
  318. temp = list.get(0).subtract(list.get(1));
  319. break;
  320. case 3:
  321. temp = list.get(0).multiply(list.get(1));
  322. break;
  323. case 4:
  324. temp = list.get(0)
  325. .divide(list.get(1), prec, RoundingMode.HALF_DOWN)
  326. .stripTrailingZeros();
  327. break;
  328. case 5:
  329. temp = list.get(0).pow(list.get(1).intValue());
  330. break;
  331. case 6:
  332. temp = list.get(0).divide(new BigDecimal("100"))
  333. .multiply(list.get(1));
  334. break;
  335. }
  336. return temp;
  337. }
  338.  
  339. /* buttons listeners, every in separated inner class */
  340. class B9ActList implements ActionListener {
  341. public void actionPerformed(ActionEvent event) {
  342. block();
  343. Kalkulator.numButton("9");
  344. }
  345. }
  346.  
  347. class B8ActList implements ActionListener {
  348. public void actionPerformed(ActionEvent event) {
  349. block();
  350. Kalkulator.numButton("8");
  351. }
  352. }
  353.  
  354. class B7ActList implements ActionListener {
  355. public void actionPerformed(ActionEvent event) {
  356. block();
  357. Kalkulator.numButton("7");
  358. }
  359. }
  360.  
  361. class B6ActList implements ActionListener {
  362. public void actionPerformed(ActionEvent event) {
  363. block();
  364. Kalkulator.numButton("6");
  365. }
  366. }
  367.  
  368. class B5ActList implements ActionListener {
  369. public void actionPerformed(ActionEvent event) {
  370. block();
  371. Kalkulator.numButton("5");
  372. }
  373. }
  374.  
  375. class B4ActList implements ActionListener {
  376. public void actionPerformed(ActionEvent event) {
  377. block();
  378. Kalkulator.numButton("4");
  379. }
  380. }
  381.  
  382. class B3ActList implements ActionListener {
  383. public void actionPerformed(ActionEvent event) {
  384. block();
  385. Kalkulator.numButton("3");
  386. }
  387. }
  388.  
  389. class B2ActList implements ActionListener {
  390. public void actionPerformed(ActionEvent event) {
  391. block();
  392. Kalkulator.numButton("2");
  393. }
  394. }
  395.  
  396. class B1ActList implements ActionListener {
  397. public void actionPerformed(ActionEvent event) {
  398. block();
  399. Kalkulator.numButton("1");
  400. }
  401. }
  402.  
  403. class B0ActList implements ActionListener {
  404. public void actionPerformed(ActionEvent event) {
  405. block();
  406. Kalkulator.numButton("0");
  407. }
  408. }
  409.  
  410. class BpointActList implements ActionListener {
  411. public void actionPerformed(ActionEvent event) {
  412. block();
  413. boolean point = false;
  414. for (int i = numValue.length() - 1; i >= 0; i--) {
  415. if (numValue.substring(i, i + 1).equals(".")) {
  416. point = true;
  417. }
  418. }
  419. if (numValue.equals("")) {
  420. Kalkulator.numButton("0.");
  421. } else if (point) {
  422.  
  423. } else {
  424. Kalkulator.numButton(".");
  425. }
  426. }
  427. }
  428.  
  429. class BequalActList implements ActionListener { // useage of "="
  430. public void actionPerformed(ActionEvent event) {
  431. root = false;
  432. if (numValue.equals("0") && operation.get(0).equals("4")) {
  433. // attempt to divide by"O"
  434. secDisplay.setText("Error:can not divide by "0"");
  435. operation.removeAll(operation);
  436. memory.removeAll(memory);
  437. numValue = "";
  438. root = true;
  439. } else if (operation.isEmpty() || operation.get(0).equals("0")) {
  440. // unnecessary use of "=" button
  441. sideTextArea.append("u00BB" + mainDisplay.getText() + "n");
  442. // send display content to history
  443.  
  444. } else {
  445. try { // standard "=" operation
  446. memory.add(new BigDecimal(numValue));
  447. numValue = "";
  448. result = opEqual(memory, operation);
  449. mainDisplay.setText(result
  450. .setScale(prec, RoundingMode.HALF_DOWN)
  451. .stripTrailingZeros().toPlainString());
  452.  
  453. operation.add(0, "0");
  454. memory.removeAll(memory);
  455. secDisplay.setText("");
  456. sideTextArea
  457. .append("u00BB" + mainDisplay.getText() + "n");
  458.  
  459. // if user will try to get result, after unnecessary usage
  460. // of operation button(+,-,etc.
  461.  
  462. } catch (Exception ex) {
  463. sideTextArea
  464. .append("u00BB" + mainDisplay.getText() + "n");
  465. if (ex instanceof NumberFormatException) {
  466. memory.removeAll(memory);
  467. operation.removeAll(operation);
  468. numValue = mainDisplay.getText();
  469. secDisplay.setText(numValue);
  470. } else if (ex instanceof IndexOutOfBoundsException) {
  471. memory.removeAll(memory);
  472. operation.removeAll(operation);
  473. numValue = mainDisplay.getText();
  474. }
  475. }
  476. }
  477. }
  478. }
  479.  
  480. class BplusActList implements ActionListener {
  481. public void actionPerformed(ActionEvent event) {
  482. Kalkulator.numOperation("1", "+");
  483. }
  484. }
  485.  
  486. class BminusActList implements ActionListener {
  487. public void actionPerformed(ActionEvent event) {
  488. Kalkulator.numOperation("2", "-");
  489. }
  490. }
  491.  
  492. class BmultiActList implements ActionListener {
  493. public void actionPerformed(ActionEvent event) {
  494. Kalkulator.numOperation("3", "u00D7");
  495. }
  496. }
  497.  
  498. class BdivActList implements ActionListener {
  499. public void actionPerformed(ActionEvent event) {
  500. Kalkulator.numOperation("4", ":");
  501. }
  502. }
  503.  
  504. class nActList implements ActionListener {
  505. public void actionPerformed(ActionEvent event) {
  506. Kalkulator.numOperation("5", "^");
  507. }
  508. }
  509.  
  510. class percentActList implements ActionListener {
  511. public void actionPerformed(ActionEvent event) {
  512. Kalkulator.numOperation("6", "%");
  513. }
  514. }
  515.  
  516. /**
  517. * Root Listener is different form other operations, it does not use a
  518. * opEqual method, it counts by itself. The result of root operation is not
  519. * counted as normal result of operation, to send it to history, it is
  520. * necessary to additional use of "=".
  521. */
  522. class rootActList implements ActionListener {
  523. public void actionPerformed(ActionEvent event) {
  524. root = false;
  525. if ((!operation.isEmpty()) && operation.get(0).equals("0")) {
  526. // if user want to use root on result of former operations
  527. secDisplay.setText(mainDisplay.getText());
  528. }
  529. String sqrt = secDisplay.getText();
  530. try {
  531. BigDecimal squareRoot = new BigDecimal(Math.sqrt(Double
  532. .parseDouble(mainDisplay.getText()))).setScale(prec,
  533. RoundingMode.HALF_DOWN);
  534.  
  535. for (int i = sqrt.length() - 1; i >= 0; i--) {
  536. char c = sqrt.charAt(i);
  537. // root on already rooted number
  538. if ((!Character.isDigit(c) && sqrt.substring(i, i + 1)
  539. .equals("u221A"))) {
  540. // to avoid root symbol doubling
  541. sqrt = sqrt.substring(0, i + 1) + mainDisplay.getText();
  542. break;
  543. } else if (!(Character.isDigit(c) || c == '.' || sqrt
  544. .substring(i, i + 1).equals("u221A"))) {
  545. sqrt = sqrt.substring(0, i + 1) + "u221A"
  546. + sqrt.substring(i + 1);
  547. break;
  548. } else if (i == 0 && Character.isDigit(c)) {
  549. sqrt = "u221A" + sqrt;
  550. break;
  551. }
  552. }
  553. secDisplay.setText(sqrt);
  554. /*
  555. * changing a value on secondary display after use root one
  556. * before rooted number
  557. */
  558. if ((!operation.isEmpty()) && operation.get(0).equals("6")) {
  559. secDisplay.setText("u221A" + mainDisplay.getText());
  560. }
  561. mainDisplay.setText(squareRoot
  562. .setScale(prec, RoundingMode.HALF_UP)
  563. .stripTrailingZeros().toString());
  564. numValue = mainDisplay.getText();
  565. root = true;
  566. } catch (NumberFormatException ex) { // attempt to root negative
  567. // number
  568. secDisplay.setText("Error: incorrect data");
  569. operation.removeAll(operation);
  570. memory.removeAll(memory);
  571. numValue = "";
  572. root = true;
  573. }
  574. }
  575. }
  576.  
  577. class pmActList implements ActionListener {
  578. // change value sign, form + to -, and from - to +
  579. public void actionPerformed(ActionEvent event) {
  580. root = false;
  581. if (numValue.charAt(0) == '-') {
  582. numValue = numValue.substring(1);
  583. } else {
  584. numValue = "-" + numValue;
  585. }
  586. mainDisplay.setText(numValue);
  587.  
  588. String s = secDisplay.getText(); // adding sign to secondary display
  589. if (numValue.substring(0, 1).equals("-")) {
  590. secDisplay.setText(s.substring(0,
  591. s.length() - (numValue.length() - 1))
  592. + "(" + numValue + ")");
  593. } else {
  594. secDisplay.setText(s.substring(0,
  595. s.length() - (numValue.length() + 3))
  596. + numValue);
  597. }
  598. root = true;
  599. }
  600. }
  601.  
  602. class CActList implements ActionListener { // calculator reset
  603. public void actionPerformed(ActionEvent event) {
  604. memory.removeAll(memory);
  605. operation.removeAll(operation);
  606. numValue = "";
  607. secDisplay.setText("");
  608. mainDisplay.setText(numValue);
  609. root = false;
  610. }
  611. }
  612.  
  613. class MCActList implements ActionListener {
  614. // reset of memory and results history
  615. public void actionPerformed(ActionEvent event) {
  616. memoryStore = new BigDecimal(0);
  617. sideTextField.setText("M: ");
  618. sideTextArea.setText("");
  619. }
  620. }
  621.  
  622. class MSActList implements ActionListener { // sending value to memory
  623. public void actionPerformed(ActionEvent event) {
  624. memoryStore = new BigDecimal(mainDisplay.getText());
  625. sideTextField.setText("M: " + mainDisplay.getText());
  626.  
  627. }
  628. }
  629.  
  630. class MRActList implements ActionListener { // recall value from memory
  631. public void actionPerformed(ActionEvent event) {
  632. String s = secDisplay.getText();
  633. String m = memoryStore.toString();
  634. numValue = m;
  635. mainDisplay.setText(numValue);
  636.  
  637. if (s.length() == 0
  638. || ((s.length() > 0 && (!s.substring(
  639. s.length() - m.length()).equals(m))))) {
  640. secDisplay.append(numValue);
  641. }
  642. }
  643. }
  644.  
  645. // options from menu
  646. class wrapList implements ItemListener {
  647. // wrap a lines in main display if value is to big
  648. public void itemStateChanged(ItemEvent event) {
  649. if (wrap.isSelected()) {
  650. mainDisplay.setLineWrap(true);
  651. } else {
  652. mainDisplay.setLineWrap(false);
  653. }
  654. }
  655. }
  656.  
  657. class precList implements ActionListener { // decimal point accuracy
  658. public void actionPerformed(ActionEvent event) {
  659. try {
  660. prec = Integer.parseInt(JOptionPane.showInputDialog(frame,
  661. "Current decimal point accuracyn" + prec
  662. + "nChange a decimal point accuracy:"));
  663. } catch (NumberFormatException ex) {
  664. ex.printStackTrace();
  665. }
  666. }
  667. }
  668.  
  669. class infoActList implements ActionListener {
  670. public void actionPerformed(ActionEvent event) {
  671. JOptionPane
  672. .showMessageDialog(
  673. frame,
  674. "Calculatorn Cracov 27.02.2015n nSimple calculator for basic usage.",
  675. "Abount the program",
  676. JOptionPane.INFORMATION_MESSAGE);
  677. }
  678. }
  679.  
  680. class helpActList implements ActionListener { // displays help
  681. public void actionPerformed(ActionEvent event) {
  682. JPanel helpPanel = new JPanel();
  683. JTextArea helpArea = new JTextArea(10, 20);
  684. Font helpFont = new Font("New Times Roman", Font.PLAIN, 10);
  685. helpArea.setFont(helpFont);
  686. helpArea.setLineWrap(true);
  687. helpArea.setWrapStyleWord(true);
  688. helpArea.setBorder(BorderFactory.createBevelBorder(4,
  689. Color.LIGHT_GRAY, Color.GRAY));
  690. helpArea.setSize(400, 400);
  691. helpArea.setEditable(false);
  692. InputStream is = getClass().getResourceAsStream("help.txt");
  693. InputStreamReader isr = new InputStreamReader(is);
  694.  
  695. try {
  696. Reader reader = new InputStreamReader(is, "UTF-8");
  697. while (true) {
  698. char c = (char) reader.read();
  699. if (c == '`') {
  700. break;
  701. }
  702. helpArea.append(Character.toString(c));
  703. }
  704. reader.close();
  705. isr.close();
  706. is.close();
  707.  
  708. } catch (Exception ex) {
  709. secDisplay.setText("Error: file not found");
  710. }
  711.  
  712. root = true;
  713. helpPanel.add(helpArea);
  714. JOptionPane.showMessageDialog(frame, helpArea, "Help",
  715. JOptionPane.INFORMATION_MESSAGE);
  716. }
  717. }
  718.  
  719. class histList implements ActionListener {
  720. public void actionPerformed(ActionEvent event) {
  721. // adding side panel with result history
  722. if (history.isSelected()) {
  723. sidePanel.setVisible(true);
  724. frame.setSize(340, 325);
  725. } else {
  726. sidePanel.setVisible(false);
  727. frame.setSize(250, 325);
  728. }
  729. }
  730. }
  731. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement