Advertisement
dc5553

Calculator GUI for java

Sep 2nd, 2012
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.69 KB | None | 0 0
  1. import java.awt.*;
  2. import javax.swing.BorderFactory;
  3. import javax.swing.JButton;
  4. import javax.swing.JPanel;
  5. import javax.swing.JTextField;
  6.  
  7. /*Just add this to a JFrame for an instant Calculator GUI*/
  8.  
  9. public class GUIPanel extends JPanel {
  10.    
  11.     public GUIPanel(){  
  12.        
  13.         addDisplay();
  14.        
  15.         addPanels();
  16.    
  17.     }
  18.    
  19.     private void addDisplay(){
  20.        
  21.         JTextField numberDisplay = new JTextField("0");
  22.        
  23.         numberDisplay.setHorizontalAlignment(JTextField.RIGHT);
  24.        
  25.         numberDisplay.setBorder(BorderFactory.createLoweredBevelBorder());
  26.        
  27.         numberDisplay.setPreferredSize(new Dimension(200,50));
  28.        
  29.         numberDisplay.setBackground(new Color(204,255,204));
  30.        
  31.         this.add(numberDisplay);        
  32.        
  33.     }
  34.    
  35.     private void addPanels(){
  36.        
  37.         String [] inputButtons = {"MC","M+","M-", "MR","C","±","÷","×","1","2","3","-","4","5","6","+","7","8","9","0","="};      
  38.        
  39.         JPanel topPanel = new JPanel();      
  40.              
  41.         JPanel bottomPanel = new JPanel(new GridBagLayout());
  42.        
  43.         GridBagConstraints gbc = new GridBagConstraints();
  44.        
  45.         topPanel.setLayout(new GridLayout(4,4));      
  46.                
  47.         for(String x:inputButtons){
  48.            
  49.             if(x.equals("0")){
  50.                
  51.                 addWideButton(x,bottomPanel,gbc);
  52.                
  53.             }
  54.            
  55.             else if(x.equals("=")){            
  56.                                
  57.                 addTallButton(x,bottomPanel,gbc);              
  58.                
  59.             }
  60.            
  61.             else if(x.equals("7") || x.equals("8")|| x.equals("9")){
  62.                
  63.                 gbc.gridwidth = 1;
  64.                
  65.                 gbc.gridheight = 1;
  66.                
  67.                 if(x.equals("7")){
  68.                    
  69.                     gbc.gridx = 0;
  70.                    
  71.                     gbc.gridy = 0;
  72.                    
  73.                 }
  74.                
  75.                 else if(x.equals("8")){
  76.                    
  77.                     gbc.gridx = 0;
  78.                    
  79.                     gbc.gridy = 1;                    
  80.                    
  81.                 }
  82.                
  83.                 else if(x.equals("9")){
  84.                    
  85.                     gbc.gridx = 0;
  86.                    
  87.                     gbc.gridy = 2;                    
  88.                    
  89.                 }              
  90.                
  91.                 addButton(x,bottomPanel,gbc);              
  92.                
  93.             }
  94.            
  95.             else{
  96.                
  97.                 addButton(x,topPanel,gbc);
  98.                            
  99.             }
  100.            
  101.         }
  102.        
  103.         // this button would not go tot he right place for some reason!!
  104.        
  105.         gbc.gridx = 2;
  106.        
  107.         gbc.gridy = 1;
  108.        
  109.         JButton button = new JButton(".");
  110.        
  111.         button.setPreferredSize(new Dimension(55,30));
  112.        
  113.         bottomPanel.add(button, gbc);
  114.        
  115.         // end of the "." button fiasco!
  116.        
  117.         topPanel.setOpaque(false);
  118.        
  119.         bottomPanel.setOpaque(false);
  120.        
  121.         setPreferredSize(new Dimension(225,250));
  122.        
  123.         this.add(topPanel);
  124.        
  125.         this.add(bottomPanel);
  126.        
  127.     }
  128.    
  129.     private void addButton(String text, Container container, GridBagConstraints gbc){
  130.        
  131.         JButton button = new JButton(text);
  132.        
  133.         button.setPreferredSize(new Dimension(55,30));
  134.        
  135.         button.setOpaque(false);
  136.        
  137.         container.add(button);        
  138.        
  139.     }
  140.    
  141.     private void addWideButton(String text, Container container, GridBagConstraints gbc){
  142.        
  143.         JButton button = new JButton(text);      
  144.        
  145.         button.setOpaque(false);
  146.        
  147.         button.setPreferredSize(new Dimension(110,30));
  148.        
  149.         gbc.gridx = 0;
  150.        
  151.         gbc.gridy = 1;
  152.        
  153.         gbc.gridheight = 1;
  154.        
  155.         gbc.gridwidth = 2;
  156.        
  157.         container.add(button, gbc);        
  158.        
  159.     }
  160.    
  161.     private void addTallButton(String text, Container container, GridBagConstraints gbc){
  162.        
  163.         JButton button = new JButton(text);        
  164.        
  165.         button.setOpaque(false);
  166.        
  167.         button.setPreferredSize(new Dimension(55,60));
  168.        
  169.         gbc.gridx = 3;
  170.        
  171.         gbc.gridy = 0;
  172.        
  173.         gbc.gridheight = 2;
  174.        
  175.         gbc.gridwidth = 1;
  176.        
  177.         container.add(button, gbc);      
  178.        
  179.     }
  180.    
  181. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement