Results 1 to 5 of 5
  1. #1
    fernandoasac is offline Member
    Join Date
    Apr 2013
    Posts
    2
    Rep Power
    0

    Question calculatorGui.java need help

    hi , i need help in my project, i have to do a gui calculator, this is all i have done so far, and now e need help to add the action listener.
    thank

    import javax.swing.*;
    import javax.swing.event.*;
    import java.awt.*;
    import java.awt.event.*;




    public class CalculatorGui{

    JButton b;
    JTextField text;

    public static void main(String[] args){

    //Create an objet gui and call the method go() to run the program
    CalculatorGui gui = new CalculatorGui();
    gui.go();

    }

    public void go(){

    //Create the Window and set up some functions
    //JFrame.setDefaultLookAndFeelDecorated(true);
    JFrame frame = new JFrame("Calculator Project");
    frame.setResizable(false);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOS E);
    frame.setLocationRelativeTo(null);

    //Creating the main panel
    JPanel content = new JPanel();
    //content.setBackground(Color.MAGENTA);
    frame.add(content);

    //Create a specific font to used in this program
    Font bigFont = new Font("arial", Font.BOLD, 20);

    //Create and display text field
    JPanel display = new JPanel();
    //display.setBackground(Color.YELLOW);
    display.setLayout(new FlowLayout());
    text = new JTextField("0",14);
    text.setBackground(Color.WHITE);
    text.setEditable(false);
    text.setHorizontalAlignment(JTextField.RIGHT);
    text.setBorder(BorderFactory.createMatteBorder(2,2 ,2,2,Color.BLACK));
    text.addActionListener(new TextListener());
    text.setFont(bigFont);
    display.add(text);

    //Create a panel to hold the button for the numbers
    JPanel keypad = new JPanel();
    keypad.setLayout(new GridLayout(5,3,3,3));
    //keypad.setBackground(Color.BLUE);
    keypad.setFont(bigFont);

    //Create an array to hold the values numbers, dot and clear button

    ActionListener buttonAction = new KeysListener();
    String[] keysArray = {"7","8","9","4","5","6","1","2","3",".","0","CR"} ;
    for(int i = 0; i < keysArray.length; i++){
    b = new JButton(keysArray[i]);
    b.addActionListener(buttonAction);
    b.setFont(bigFont);
    keypad.add(b);
    }


    //Create another panel to hold the operational buttons
    JPanel operator = new JPanel();
    operator.setFont(bigFont);
    operator.setLayout(new GridLayout(5,1,3,3));
    //operator.setBackground(Color.GREEN);
    //Create an array to hold the operator values
    ActionListener tListener = new TextListener();
    String[] operatorArray = {"*","/","-","+","="};
    for(int index= 0; index < operatorArray.length; index++){
    JButton bOperator = new JButton(operatorArray[index]);
    bOperator.addActionListener(tListener);
    bOperator.setFont(bigFont);
    operator.add(bOperator);
    }

    //Set the main panel to the right positions on a frame and create an empty border around it
    content.setBorder(BorderFactory.createEmptyBorder( 10,10,10,10));
    content.add(display, BorderLayout.NORTH);
    content.add(keypad, BorderLayout.CENTER);
    content.add(operator, BorderLayout.EAST);
    frame.setSize(290, 300);
    frame.setVisible(true);

    }

    /////////////////////////////////////////////////////////////////////////
    //
    //ActionListener for all buttons and the textfield


    class KeysListener implements ActionListener{
    boolean firstNumber = true;
    public void actionPerformed(ActionEvent event){

    String key = event.getActionCommand();

    if(key == "CR"){
    text.setText("0");
    }
    else{
    if (firstNumber) {
    text.setText("");
    firstNumber = false;
    }
    text.setText(text.getText() + key);
    }
    }
    }

    class TextListener implements ActionListener{

    public void actionPerformed(ActionEvent event){

    String OpPanel = event.getActionCommand();
    text.setText(OpPanel);
    }
    }

    /////////////////////////////////////////////////////////////////////////
    //
    //Logic methods for all calculation


    double num;
    double num2;
    double total;

    public void sum(double n){

    num = num2 + n;
    }

    public void sub(double n){

    num = num2 - n;
    }

    public void div(double n){

    num = num2 / n;
    }

    public void mul(double n){

    num = num2 * n;
    }
    }

  2. #2
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    4,038
    Rep Power
    10

    Default Re: calculatorGui.java need help

    When posting code, please use the code tags to preserve formatting.

    What part of this is giving you trouble? What exactly are you confused about?

    Recommended reading: How to Write an Action Listener (The Java™ Tutorials > Creating a GUI With JFC/Swing > Writing Event Listeners)
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    fernandoasac is offline Member
    Join Date
    Apr 2013
    Posts
    2
    Rep Power
    0

    Default Re: calculatorGui.java need help

    thanks , that link help me alot, i have another question , how do i do to use the keyboard
    on my calculator , i meant instead of click on the calculator button i can type de number on the keyboard and the number display on the display field ?
    thanks again

    import javax.swing.*;
    import javax.swing.event.*;
    import java.awt.*;
    import java.awt.event.*;




    public class CalculatorGui{

    JButton b;
    JTextField text;

    public static void main(String[] args){

    //Create an objet gui and call the method go() to run the program
    CalculatorGui gui = new CalculatorGui();
    gui.go();

    }

    public void go(){

    //Create the Window and set up some functions
    //JFrame.setDefaultLookAndFeelDecorated(true);
    JFrame frame = new JFrame("Calculator Project");
    frame.setResizable(false);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOS E);
    frame.setLocationRelativeTo(null);

    //Creating the main panel
    JPanel content = new JPanel();
    //content.setBackground(Color.MAGENTA);
    frame.add(content);

    //Create a specific font to used in this program
    Font bigFont = new Font("arial", Font.BOLD, 20);

    //Create and display text field
    JPanel display = new JPanel();
    //display.setBackground(Color.YELLOW);
    display.setLayout(new FlowLayout());
    text = new JTextField("0",14);
    text.setBackground(Color.WHITE);
    text.setEditable(false);
    text.setHorizontalAlignment(JTextField.RIGHT);
    text.setBorder(BorderFactory.createMatteBorder(2,2 ,2,2,Color.BLACK));
    text.addActionListener(new TextListener());
    text.setFont(bigFont);
    display.add(text);

    //Create a panel to hold the button for the numbers
    JPanel keypad = new JPanel();
    keypad.setLayout(new GridLayout(5,3,3,3));
    //keypad.setBackground(Color.BLUE);
    keypad.setFont(bigFont);

    //Create an array to hold the values numbers, dot and clear button

    ActionListener buttonAction = new KeysListener();
    String[] keysArray = {"7","8","9","4","5","6","1","2","3",".","0","CR"} ;
    for(int i = 0; i < keysArray.length; i++){
    b = new JButton(keysArray[i]);
    b.addActionListener(buttonAction);
    b.setFont(bigFont);
    keypad.add(b);
    }


    //Create another panel to hold the operational buttons
    JPanel operator = new JPanel();
    operator.setFont(bigFont);
    operator.setLayout(new GridLayout(5,1,3,3));
    //operator.setBackground(Color.GREEN);
    //Create an array to hold the operator values
    ActionListener tListener = new TextListener();
    String[] operatorArray = {"*","/","-","+","="};
    for(int index= 0; index < operatorArray.length; index++){
    JButton bOperator = new JButton(operatorArray[index]);
    bOperator.addActionListener(tListener);
    bOperator.setFont(bigFont);
    operator.add(bOperator);
    }

    //Set the main panel to the right positions on a frame and create an empty border around it
    content.setBorder(BorderFactory.createEmptyBorder( 10,10,10,10));
    content.add(display, BorderLayout.NORTH);
    content.add(keypad, BorderLayout.CENTER);
    content.add(operator, BorderLayout.EAST);
    frame.setSize(290, 300);
    frame.setVisible(true);

    }

    /////////////////////////////////////////////////////////////////////////
    //
    //ActionListener for all buttons and the textfield


    class KeysListener implements ActionListener{
    boolean firstNumber = true;
    public void actionPerformed(ActionEvent event){

    String key = event.getActionCommand();

    if(key == "CR"){
    text.setText("0");
    }
    else{
    if (firstNumber) {
    text.setText("");
    firstNumber = false;
    }
    text.setText(text.getText() + key);
    }
    }
    }

    class TextListener implements ActionListener{

    public void actionPerformed(ActionEvent event){

    String OpPanel = event.getActionCommand();
    text.setText(OpPanel);
    }
    }

    /////////////////////////////////////////////////////////////////////////
    //
    //Logic methods for all calculation


    double num;
    double num2;
    double total;

    public void sum(double n){

    num = num2 + n;
    }

    public void sub(double n){

    num = num2 - n;
    }

    public void div(double n){

    num = num2 / n;
    }

    public void mul(double n){

    num = num2 * n;
    }
    }

  4. #4
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,224
    Rep Power
    20

    Default Re: calculatorGui.java need help

    [code] tags [/code]
    not
    [quote] tags [/quote]
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  5. #5
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    4,038
    Rep Power
    10

    Default Re: calculatorGui.java need help

    What have you tried? What research have you done on google?
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •