silentkiler029

Azhar_Question-01_Calculator_GUI_Design

Sep 29th, 2021 (edited)
222
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 13.53 KB | None | 0 0
  1. import javax.swing.*;
  2. import java.awt.*;
  3. import javax.swing.border.Border;
  4.  
  5.  
  6. public class problem1 extends JFrame {
  7.  
  8.     private int l = 20;
  9.     private int w = 70;
  10.     private int h = 40;
  11.     private int g = 5;
  12.    
  13.     private Container c;
  14.     private JPanel jp;
  15.     private Font font, font2, font3;
  16.     private JMenuBar menubar;
  17.     private JMenu view, edit, help;
  18.     private JTextField tf;
  19.     private Border border;
  20.  
  21.     private JRadioButton deg, rad, grad;
  22.     private ButtonGroup bg;
  23.     private JPanel panel1;
  24.  
  25.     private JButton empty, lnt, dms, pi, F_E;
  26.     private JButton lnv, sinh, cosh, tanh, exp;
  27.     private JButton ln, sin, cos, tan, mod;
  28.     private JButton ob, xp2, xpy, xp3, log;
  29.     private JButton cb, nf, xry, xr3, tenpx;
  30.     private JButton mc, la, seven, four, one, zero;
  31.     private JButton mr, ce, eight, five, two;
  32.     private JButton ms, C, nine, six, three, dot;
  33.     private JButton mp, pm, div, mul, min, plus;
  34.     private JButton mm, sq, per, inv, eq;
  35.    
  36.     problem1()
  37.     {
  38.         this.setSize(10*w+2*l, 9*h+l);
  39.         this.setLocationRelativeTo(null);
  40.         this.setTitle("Calculator");
  41.         this.setResizable(true);
  42.  
  43.         init();
  44.     }
  45.  
  46.     void init()
  47.     {
  48.         // creating contentpane
  49.         c = this.getContentPane();
  50.         c.setLayout(null);
  51.  
  52.         jp = new JPanel();
  53.         jp.setLayout(null);
  54.         c.add(jp);
  55.         jp.setBounds(0, 0, 10*w+2*l, 9*h+l);
  56.  
  57.         // setting font
  58.         font = new Font("Consolas", Font.BOLD, 32);
  59.         font2 = new Font("Consolas", Font.BOLD, 16);
  60.         font3 = new Font("Consolas", Font.BOLD, 12);
  61.         // setting border
  62.         border = BorderFactory.createLineBorder(Color.black);
  63.  
  64.         // adding components
  65.         createMenu();
  66.         addTextField();
  67.         addRadioButtons();
  68.         column1Buttons();
  69.         column2Buttons();
  70.         column3Buttons();
  71.         column4Buttons();
  72.         column5Buttons();
  73.         column6Buttons();
  74.         column7Buttons();
  75.         column8Buttons();
  76.         column9Buttons();
  77.         column10Buttons();
  78.     }
  79.  
  80.     public static void main(String[] args) throws Exception {
  81.         problem1 frame = new problem1();
  82.         frame.setVisible(true);
  83.         frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  84.     }
  85.  
  86.    
  87.     void createMenu()
  88.     {
  89.         menubar = new JMenuBar();
  90.         view = new JMenu("View");
  91.         edit = new JMenu("Edit");
  92.         help = new JMenu("Help");
  93.         menubar.add(view);
  94.         menubar.add(edit);
  95.         menubar.add(help);
  96.         setJMenuBar(menubar);
  97.     }
  98.  
  99.     void addTextField()
  100.     {
  101.         tf = new JTextField();
  102.         tf.setText("0");
  103.         tf.setBounds(l+g, g, 10*w - 2*g, 2*h - 2*g);
  104.         tf.setFont(font);
  105.         tf.setHorizontalAlignment(JTextField.RIGHT);
  106.         tf.setEditable(false);
  107.         tf.setBorder(border);
  108.         jp.add(tf);
  109.     }
  110.  
  111.     void addRadioButtons()
  112.     {
  113.         bg = new ButtonGroup();
  114.         panel1 = new JPanel();
  115.         panel1.setBounds(l+g, 2*h + g, 5*w - 2*g, h - 2*g);
  116.         panel1.setBorder(border);
  117.         panel1.setLayout(null);
  118.        
  119.         deg = new JRadioButton("Degrees");
  120.         rad = new JRadioButton("Radians");
  121.         grad = new JRadioButton("Grads");
  122.  
  123.         int x = (int) ((5*w - 8*g) / (3.0));
  124.  
  125.         deg.setBounds(g, 1, x, h - 2*g - 2);
  126.         deg.setFont(font2);
  127.         deg.setVerticalAlignment(JRadioButton.CENTER);
  128.         deg.setFocusable(false);
  129.         bg.add(deg);
  130.  
  131.         rad.setBounds(x + 3*g, 1, x, h - 2*g - 2);
  132.         rad.setFont(font2);
  133.         rad.setVerticalAlignment(JRadioButton.CENTER);
  134.         rad.setFocusable(false);
  135.         bg.add(rad);
  136.  
  137.         grad.setBounds(2*x + 5*g, 1, x, h - 2*g - 2);
  138.         grad.setFont(font2);
  139.         grad.setVerticalAlignment(JRadioButton.CENTER);
  140.         grad.setFocusable(false);
  141.         bg.add(grad);
  142.  
  143.         panel1.add(deg);
  144.         panel1.add(rad);
  145.         panel1.add(grad);
  146.  
  147.         jp.add(panel1);
  148.     }
  149.  
  150.     void column1Buttons()
  151.     {
  152.         empty = new JButton();
  153.         empty.setBounds(l+g, 3*h+g, w-2*g, h-2*g);
  154.         empty.setFocusable(false);
  155.         empty.setFont(font2);
  156.         jp.add(empty);
  157.  
  158.         lnt = new JButton("lnt");
  159.         lnt.setBounds(l+g, 4*h+g, w-2*g, h-2*g);
  160.         lnt.setFont(font2);
  161.         lnt.setFocusable(false);
  162.         jp.add(lnt);
  163.  
  164.         dms = new JButton("dms");
  165.         dms.setBounds(l+g, 5*h+g, w-2*g, h-2*g);
  166.         dms.setFont(font2);
  167.         dms.setFocusable(false);
  168.         jp.add(dms);
  169.  
  170.         pi = new JButton("\u03C0");
  171.         pi.setBounds(l+g, 6*h+g, w-2*g, h-2*g);
  172.         pi.setFocusable(false);
  173.         pi.setFont(font2);
  174.         jp.add(pi);
  175.  
  176.         F_E = new JButton("F-E");
  177.         F_E.setBounds(l+g, 7*h+g, w-2*g, h-2*g);
  178.         F_E.setFocusable(false);
  179.         F_E.setFont(font2);
  180.         jp.add(F_E);
  181.     }
  182.  
  183.     void column2Buttons()
  184.     {
  185.         lnv = new JButton("lnv");
  186.         lnv.setBounds(l+w+g, 3*h+g, w-2*g, h-2*g);
  187.         lnv.setFocusable(false);
  188.         lnv.setFont(font2);
  189.         jp.add(lnv);
  190.  
  191.         sinh = new JButton("sinh");
  192.         sinh.setBounds(l+w+g, 4*h+g, w-2*g, h-2*g);
  193.         sinh.setFont(font3);
  194.         sinh.setFocusable(false);
  195.         jp.add(sinh);
  196.  
  197.         cosh = new JButton("cosh");
  198.         cosh.setBounds(l+w+g, 5*h+g, w-2*g, h-2*g);
  199.         cosh.setFont(font3);
  200.         cosh.setFocusable(false);
  201.         jp.add(cosh);
  202.  
  203.         tanh = new JButton("tanh");
  204.         tanh.setBounds(l+w+g, 6*h+g, w-2*g, h-2*g);
  205.         tanh.setFocusable(false);
  206.         tanh.setFont(font3);
  207.         jp.add(tanh);
  208.  
  209.         exp = new JButton("Exp");
  210.         exp.setBounds(l+w+g, 7*h+g, w-2*g, h-2*g);
  211.         exp.setFocusable(false);
  212.         exp.setFont(font2);
  213.         jp.add(exp);
  214.     }
  215.  
  216.     void column3Buttons()
  217.     {
  218.         ln = new JButton("ln");
  219.         ln.setBounds(l+2*w+g, 3*h+g, w-2*g, h-2*g);
  220.         ln.setFocusable(false);
  221.         ln.setFont(font2);
  222.         jp.add(ln);
  223.  
  224.         sin = new JButton("sin");
  225.         sin.setBounds(l+2*w+g, 4*h+g, w-2*g, h-2*g);
  226.         sin.setFont(font2);
  227.         sin.setFocusable(false);
  228.         jp.add(sin);
  229.  
  230.         cos = new JButton("cos");
  231.         cos.setBounds(l+2*w+g, 5*h+g, w-2*g, h-2*g);
  232.         cos.setFont(font2);
  233.         cos.setFocusable(false);
  234.         jp.add(cos);
  235.  
  236.         tan = new JButton("tan");
  237.         tan.setBounds(l+2*w+g, 6*h+g, w-2*g, h-2*g);
  238.         tan.setFocusable(false);
  239.         tan.setFont(font2);
  240.         jp.add(tan);
  241.  
  242.         mod = new JButton("Mod");
  243.         mod.setBounds(l+2*w+g, 7*h+g, w-2*g, h-2*g);
  244.         mod.setFocusable(false);
  245.         mod.setFont(font2);
  246.         jp.add(mod);
  247.     }
  248.  
  249.     void column4Buttons()
  250.     {
  251.         ob = new JButton("(");
  252.         ob.setBounds(l+3*w+g, 3*h+g, w-2*g, h-2*g);
  253.         ob.setFocusable(false);
  254.         ob.setFont(font2);
  255.         jp.add(ob);
  256.  
  257.         xp2 = new JButton("x\u00B2");
  258.         xp2.setBounds(l+3*w+g, 4*h+g, w-2*g, h-2*g);
  259.         xp2.setFont(font2);
  260.         xp2.setFocusable(false);
  261.         jp.add(xp2);
  262.  
  263.         xpy = new JButton("x\u02B8");
  264.         xpy.setBounds(l+3*w+g, 5*h+g, w-2*g, h-2*g);
  265.         xpy.setFont(font2);
  266.         xpy.setFocusable(false);
  267.         jp.add(xpy);
  268.  
  269.         xp3 = new JButton("x\u00B3");
  270.         xp3.setBounds(l+3*w+g, 6*h+g, w-2*g, h-2*g);
  271.         xp3.setFocusable(false);
  272.         xp3.setFont(font2);
  273.         jp.add(xp3);
  274.  
  275.         log = new JButton("log");
  276.         log.setBounds(l+3*w+g, 7*h+g, w-2*g, h-2*g);
  277.         log.setFocusable(false);
  278.         log.setFont(font2);
  279.         jp.add(log);
  280.     }
  281.  
  282.     void column5Buttons()
  283.     {
  284.         cb = new JButton(")");
  285.         cb.setBounds(l+4*w+g, 3*h+g, w-2*g, h-2*g);
  286.         cb.setFocusable(false);
  287.         cb.setFont(font2);
  288.         jp.add(cb);
  289.  
  290.         nf = new JButton("n!");
  291.         nf.setBounds(l+4*w+g, 4*h+g, w-2*g, h-2*g);
  292.         nf.setFont(font2);
  293.         nf.setFocusable(false);
  294.         jp.add(nf);
  295.  
  296.         xry = new JButton("\u02B8\u221Ax");
  297.         xry.setBounds(l+4*w+g, 5*h+g, w-2*g, h-2*g);
  298.         xry.setFont(font2);
  299.         xry.setFocusable(false);
  300.         jp.add(xry);
  301.  
  302.         xr3 = new JButton("\u00B3\u221Ax");
  303.         xr3.setBounds(l+4*w+g, 6*h+g, w-2*g, h-2*g);
  304.         xr3.setFocusable(false);
  305.         xr3.setFont(font2);
  306.         jp.add(xr3);
  307.  
  308.         tenpx = new JButton("10\u00B2");
  309.         tenpx.setBounds(l+4*w+g, 7*h+g, w-2*g, h-2*g);
  310.         tenpx.setFocusable(false);
  311.         tenpx.setFont(font2);
  312.         jp.add(tenpx);
  313.     }
  314.  
  315.     void column6Buttons()
  316.     {
  317.         mc = new JButton("MC");
  318.         mc.setBounds(l+5*w+g, 2*h+g, w-2*g, h-2*g);
  319.         mc.setFocusable(false);
  320.         mc.setFont(font2);
  321.         jp.add(mc);
  322.        
  323.         la = new JButton("\u2190");
  324.         la.setBounds(l+5*w+g, 3*h+g, w-2*g, h-2*g);
  325.         la.setFocusable(false);
  326.         la.setFont(font2);
  327.         jp.add(la);
  328.  
  329.         seven = new JButton("7");
  330.         seven.setBounds(l+5*w+g, 4*h+g, w-2*g, h-2*g);
  331.         seven.setFont(font2);
  332.         seven.setFocusable(false);
  333.         jp.add(seven);
  334.  
  335.         four = new JButton("4");
  336.         four.setBounds(l+5*w+g, 5*h+g, w-2*g, h-2*g);
  337.         four.setFont(font2);
  338.         four.setFocusable(false);
  339.         jp.add(four);
  340.  
  341.         one = new JButton("1");
  342.         one.setBounds(l+5*w+g, 6*h+g, w-2*g, h-2*g);
  343.         one.setFocusable(false);
  344.         one.setFont(font2);
  345.         jp.add(one);
  346.  
  347.         zero = new JButton("0");
  348.         zero.setBounds(l+5*w+g, 7*h+g, 2*w-2*g, h-2*g);
  349.         zero.setFocusable(false);
  350.         zero.setFont(font2);
  351.         jp.add(zero);
  352.     }
  353.  
  354.     void column7Buttons()
  355.     {
  356.         mr = new JButton("MR");
  357.         mr.setBounds(l+6*w+g, 2*h+g, w-2*g, h-2*g);
  358.         mr.setFocusable(false);
  359.         mr.setFont(font2);
  360.         jp.add(mr);
  361.        
  362.         ce = new JButton("CE");
  363.         ce.setBounds(l+6*w+g, 3*h+g, w-2*g, h-2*g);
  364.         ce.setFocusable(false);
  365.         ce.setFont(font2);
  366.         jp.add(ce);
  367.  
  368.         eight = new JButton("8");
  369.         eight.setBounds(l+6*w+g, 4*h+g, w-2*g, h-2*g);
  370.         eight.setFont(font2);
  371.         eight.setFocusable(false);
  372.         jp.add(eight);
  373.  
  374.         five = new JButton("5");
  375.         five.setBounds(l+6*w+g, 5*h+g, w-2*g, h-2*g);
  376.         five.setFont(font2);
  377.         five.setFocusable(false);
  378.         jp.add(five);
  379.  
  380.         two = new JButton("2");
  381.         two.setBounds(l+6*w+g, 6*h+g, w-2*g, h-2*g);
  382.         two.setFocusable(false);
  383.         two.setFont(font2);
  384.         jp.add(two);
  385.     }
  386.  
  387.     void column8Buttons()
  388.     {
  389.         ms = new JButton("MS");
  390.         ms.setBounds(l+7*w+g, 2*h+g, w-2*g, h-2*g);
  391.         ms.setFocusable(false);
  392.         ms.setFont(font2);
  393.         jp.add(ms);
  394.        
  395.         C = new JButton("C");
  396.         C.setBounds(l+7*w+g, 3*h+g, w-2*g, h-2*g);
  397.         C.setFocusable(false);
  398.         C.setFont(font2);
  399.         jp.add(C);
  400.  
  401.         nine = new JButton("9");
  402.         nine.setBounds(l+7*w+g, 4*h+g, w-2*g, h-2*g);
  403.         nine.setFont(font2);
  404.         nine.setFocusable(false);
  405.         jp.add(nine);
  406.  
  407.         six = new JButton("6");
  408.         six.setBounds(l+7*w+g, 5*h+g, w-2*g, h-2*g);
  409.         six.setFont(font2);
  410.         six.setFocusable(false);
  411.         jp.add(six);
  412.  
  413.         three = new JButton("3");
  414.         three.setBounds(l+7*w+g, 6*h+g, w-2*g, h-2*g);
  415.         three.setFocusable(false);
  416.         three.setFont(font2);
  417.         jp.add(three);
  418.  
  419.         dot = new JButton(".");
  420.         dot.setBounds(l+7*w+g, 7*h+g, w-2*g, h-2*g);
  421.         dot.setFocusable(false);
  422.         dot.setFont(font2);
  423.         jp.add(dot);
  424.     }
  425.  
  426.     void column9Buttons()
  427.     {
  428.         mp = new JButton("M+");
  429.         mp.setBounds(l+8*w+g, 2*h+g, w-2*g, h-2*g);
  430.         mp.setFocusable(false);
  431.         ms.setFont(font2);
  432.         jp.add(mp);
  433.        
  434.         pm = new JButton("\u00B1");
  435.         pm.setBounds(l+8*w+g, 3*h+g, w-2*g, h-2*g);
  436.         pm.setFocusable(false);
  437.         pm.setFont(font2);
  438.         jp.add(pm);
  439.  
  440.         div = new JButton("/");
  441.         div.setBounds(l+8*w+g, 4*h+g, w-2*g, h-2*g);
  442.         div.setFont(font2);
  443.         div.setFocusable(false);
  444.         jp.add(div);
  445.  
  446.         mul = new JButton("*");
  447.         mul.setBounds(l+8*w+g, 5*h+g, w-2*g, h-2*g);
  448.         mul.setFont(font2);
  449.         mul.setFocusable(false);
  450.         jp.add(mul);
  451.  
  452.         min = new JButton("-");
  453.         min.setBounds(l+8*w+g, 6*h+g, w-2*g, h-2*g);
  454.         min.setFocusable(false);
  455.         min.setFont(font2);
  456.         jp.add(min);
  457.  
  458.         plus = new JButton("+");
  459.         plus.setBounds(l+8*w+g, 7*h+g, w-2*g, h-2*g);
  460.         plus.setFocusable(false);
  461.         plus.setFont(font2);
  462.         jp.add(plus);
  463.     }
  464.  
  465.     void column10Buttons()
  466.     {
  467.         mm = new JButton("M-");
  468.         mm.setBounds(l+9*w+g, 2*h+g, w-2*g, h-2*g);
  469.         mm.setFocusable(false);
  470.         mm.setFont(font2);
  471.         jp.add(mm);
  472.        
  473.         sq = new JButton("\u221A");
  474.         sq.setBounds(l+9*w+g, 3*h+g, w-2*g, h-2*g);
  475.         sq.setFocusable(false);
  476.         sq.setFont(font2);
  477.         jp.add(sq);
  478.  
  479.         per = new JButton("%");
  480.         per.setBounds(l+9*w+g, 4*h+g, w-2*g, h-2*g);
  481.         per.setFont(font2);
  482.         per.setFocusable(false);
  483.         jp.add(per);
  484.  
  485.         inv = new JButton("1/x");
  486.         inv.setBounds(l+9*w+g, 5*h+g, w-2*g, h-2*g);
  487.         inv.setFont(font2);
  488.         inv.setFocusable(false);
  489.         jp.add(inv);
  490.  
  491.         eq = new JButton("=");
  492.         eq.setBounds(l+9*w+g, 6*h+g, w-2*g, 2*h-2*g);
  493.         eq.setFocusable(false);
  494.         eq.setFont(font);
  495.         jp.add(eq);
  496.     }
  497. }
  498.  
Add Comment
Please, Sign In to add comment