Advertisement
tombenko

UserInterface.java

Jun 15th, 2014
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.84 KB | None | 0 0
  1. import javax.swing.JFrame;
  2. import javax.swing.JPanel;
  3. import javax.swing.JTextField;
  4. import javax.swing.JLabel;
  5. import javax.swing.JButton;
  6. import java.awt.GridLayout;
  7. import java.awt.FlowLayout;
  8. import java.awt.event.*;
  9.  
  10.  
  11. public class UserInterface extends JFrame{
  12.    
  13.     /*
  14.      * The main interface for the users. Yet it contains a textfield for
  15.      * the input, a label for the output and two buttons. One button to
  16.      * make calculation and one to exit the program. In the input field
  17.      * pressing the enter also do calculate.
  18.      * */
  19.    
  20.     private JPanel workingPanel = new JPanel( new GridLayout(2,1));
  21.     private JPanel buttonPanel = new JPanel( new GridLayout(2,1));
  22.     private JTextField inputField = new JTextField(20);
  23.     private JLabel outputField = new JLabel("0");
  24.     private JButton calcButton = new JButton("Calculate");
  25.     private JButton exitButton = new JButton("Exit");
  26.    
  27.     public UserInterface(String title){
  28.         super(title);
  29.         workingPanel.add(inputField);
  30.         workingPanel.add(outputField);
  31.         inputField.addKeyListener(new KeyListener(){
  32.             public void keyPressed(KeyEvent e){
  33.                 if(e.getKeyCode() == KeyEvent.VK_ENTER){ // Only the
  34.                     //enter have some special meaning...
  35.                     doCalculate(inputField.getText());
  36.                 }
  37.             }
  38.             public void keyReleased(KeyEvent e){
  39.                
  40.             }
  41.             public void keyTyped(KeyEvent e){
  42.                
  43.             }
  44.         });
  45.         workingPanel.setVisible(true);
  46.         buttonPanel.add(calcButton);
  47.         calcButton.addMouseListener(new MouseListener(){
  48.             public void mouseClicked(MouseEvent e){
  49.                
  50.             }
  51.             public void mouseEntered(MouseEvent e){
  52.                
  53.             }
  54.             public void mouseExited(MouseEvent e){
  55.                
  56.             }
  57.             public void mousePressed(MouseEvent e){
  58.                 doCalculate(inputField.getText());//It's enough to press
  59.                 //the button.
  60.             }
  61.             public void mouseReleased(MouseEvent e){
  62.                
  63.             }
  64.         });
  65.         exitButton.addMouseListener(new MouseListener(){
  66.             public void mouseClicked(MouseEvent e){
  67.                 dispose(); // Just to let the user change his mind.
  68.                
  69.             }
  70.             public void mouseEntered(MouseEvent e){
  71.                
  72.             }
  73.             public void mouseExited(MouseEvent e){
  74.                
  75.             }
  76.             public void mousePressed(MouseEvent e){
  77.                
  78.             }
  79.             public void mouseReleased(MouseEvent e){
  80.                
  81.             }
  82.         });
  83.         buttonPanel.add(exitButton);
  84.         buttonPanel.setVisible(true);
  85.         super.setLayout(new FlowLayout());
  86.         super.add(workingPanel);
  87.         super.add(buttonPanel);
  88.         super.setDefaultCloseOperation(EXIT_ON_CLOSE);
  89.         super.pack();
  90.         super.setResizable(false);
  91.         super.setVisible(true);
  92.     }
  93.    
  94.     private void doCalculate(String forward){
  95.         try{
  96.             outputField.setText( Calculator.getResult( forward ).toString() );
  97.         } catch(BraNotEnough brn){
  98.             outputField.setText(brn.getMessage());
  99.         } catch(OperandsNotEnough one){
  100.             outputField.setText(one.getMessage());
  101.         } catch(NumberFormatException nan){
  102.             outputField.setText("Something is wrong..");
  103.         }
  104.     }
  105.  
  106. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement