Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import javax.swing.JFrame;
- import javax.swing.JPanel;
- import javax.swing.JTextField;
- import javax.swing.JLabel;
- import javax.swing.JButton;
- import java.awt.GridLayout;
- import java.awt.FlowLayout;
- import java.awt.event.*;
- public class UserInterface extends JFrame{
- /*
- * The main interface for the users. Yet it contains a textfield for
- * the input, a label for the output and two buttons. One button to
- * make calculation and one to exit the program. In the input field
- * pressing the enter also do calculate.
- * */
- private JPanel workingPanel = new JPanel( new GridLayout(2,1));
- private JPanel buttonPanel = new JPanel( new GridLayout(2,1));
- private JTextField inputField = new JTextField(20);
- private JLabel outputField = new JLabel("0");
- private JButton calcButton = new JButton("Calculate");
- private JButton exitButton = new JButton("Exit");
- public UserInterface(String title){
- super(title);
- workingPanel.add(inputField);
- workingPanel.add(outputField);
- inputField.addKeyListener(new KeyListener(){
- public void keyPressed(KeyEvent e){
- if(e.getKeyCode() == KeyEvent.VK_ENTER){ // Only the
- //enter have some special meaning...
- doCalculate(inputField.getText());
- }
- }
- public void keyReleased(KeyEvent e){
- }
- public void keyTyped(KeyEvent e){
- }
- });
- workingPanel.setVisible(true);
- buttonPanel.add(calcButton);
- calcButton.addMouseListener(new MouseListener(){
- public void mouseClicked(MouseEvent e){
- }
- public void mouseEntered(MouseEvent e){
- }
- public void mouseExited(MouseEvent e){
- }
- public void mousePressed(MouseEvent e){
- doCalculate(inputField.getText());//It's enough to press
- //the button.
- }
- public void mouseReleased(MouseEvent e){
- }
- });
- exitButton.addMouseListener(new MouseListener(){
- public void mouseClicked(MouseEvent e){
- dispose(); // Just to let the user change his mind.
- }
- public void mouseEntered(MouseEvent e){
- }
- public void mouseExited(MouseEvent e){
- }
- public void mousePressed(MouseEvent e){
- }
- public void mouseReleased(MouseEvent e){
- }
- });
- buttonPanel.add(exitButton);
- buttonPanel.setVisible(true);
- super.setLayout(new FlowLayout());
- super.add(workingPanel);
- super.add(buttonPanel);
- super.setDefaultCloseOperation(EXIT_ON_CLOSE);
- super.pack();
- super.setResizable(false);
- super.setVisible(true);
- }
- private void doCalculate(String forward){
- try{
- outputField.setText( Calculator.getResult( forward ).toString() );
- } catch(BraNotEnough brn){
- outputField.setText(brn.getMessage());
- } catch(OperandsNotEnough one){
- outputField.setText(one.getMessage());
- } catch(NumberFormatException nan){
- outputField.setText("Something is wrong..");
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement