Home Upgrade Search Memberlist Extras Hacker Tools Award Goals Help Wiki Contact

Java 

Calculator GUI problem

Hello Java section,
I am making a calculator GUI for learning purposes I have ran into two problems, I can't seem to know how to resize the JTextField area or to have the buttons when clicked display the number on the JTextField area. By the way I am still a java noob not claiming to be an expert in Java so don't hate and tell me how to improve instead.
Here is my code
PHP Code

<?php 
import java
.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;


public class 
Calculator extends JFrame implements ActionListener
{
    private static final 
long serialVersionUID = 1L;
    
    public 
JPanel panel;
    public 
JTextField textField;
    public 
JButton[] button;
    public 
String[] buttonString = {"7", "8", "9", "/",
                                    
"4", "5", "6", "*",
                                    
"1", "2", "3", "-",
                                    
"0", ".", "+", "="};
    
    public 
Calculator()
    {
        
setTitle("Calculator");
        
setPreferredSize(new Dimension(300, 300));
        
setResizable(false);
        
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        
setLocationRelativeTo(null);
        
        
textField = new JTextField();
        
textField.setHorizontalAlignment(JTextField.RIGHT);
        
textField.setBackground(new Color(243, 220, 155));
        
textField.setEditable(false);
        
        
add(textField, BorderLayout.NORTH);
        
        
panel = new JPanel();
        
panel.setLayout(new GridLayout(4, 4));
        
add(panel);
        
        
button = new JButton[buttonString.length];
        
        for(
int i = 0; i < buttonString.length; i++)
        {
            
button[i] = new JButton(buttonString[i]);
            
panel.add(button[i]);
        }
        
        
    }
    
    public 
void actionPerformed(ActionEvent e)
    {
        while(
true)
        {
            for(
int i = 0; i < buttonString.length; i++)
            {
                if(
e.getSource() == button[i])
                {
                
textField.setText(buttonString[i]);
                }
            }
        }
        
    }
    
    public static 
void main(String[] args)
    {
        
Calculator calc = new Calculator();
        
calc.pack();
        
calc.setVisible(true);
    }
} 

Thanks I really appreciate your help!

Jekwa
You need to handle the events that your JButtons throw when clicked. Simply insert the character clicked on the right side of the JTextField. Your buttons have a method for adding the listener and editing the JTextField is trivial once you know what button was pressed.
PHP Code

<?php 
public void actionPerformed(ActionEvent e)
    {
      while(
true)
      {
      for(
int i = 0; i < buttonString.length; i++)
      {
            if(
e.getSource() == button[i])
            {
            
textField.setText(buttonString[i]);
            }
      }
      }

    } 

Doesn't that part do it?
You never specify that class as the handler for events from the buttons.

Code
.addActionListener(this);

It is sloppy, but it should work for what you are doing.
Thank you for your help Psifour, I appreciate it.
(Aug 4, 2014 12:09 PM)Jekwa Wrote:  Thank you for your help Psifour, I appreciate it.
No worries. I tried to be vague enough that you could have the pleasure of learning :D
lmao all right thanks for that, you know what I want you to criticize every bit of the code tell me how it can be improved, or do you want to wait till I am done with the whole project first?
Hmm, I don't have the time to do a full analyses of your code right this moment, but my primary concerns would be that you separate the listener into it's own class maybe even make two children of JButton that implement ActionListener for the different types of buttons you have: numbers, and functions. If you abstract it like that you can more easily handle the non-numbered buttons and their functions.

Another quick note would be to start on documentation as failing to due so now can make your project hard to read in the future (a calculator with GUI is sufficiently complex to warrant documentation and the practice will benefit you in the long run).

EDIT: Sorry if I wasn't clear. I WILL look at your code so far in-depth, just not right this moment. Besides I am sure that Cosa, Fizzy, or Sparta will comment here soon.
As soon as your action performed fires your GUI is going to freeze and you won't be able to terminate your program correctly. When you use a while(true) like that it will loop that section forever. Since you have no break condition, it will rerun that for loop over. And over. And over. Etc. If I wasn't on my phone I would type more (stupid WiFi at this condo constantly doesn't work, can't wait for the Ethernet to be setup). But until then, that needs to be fixed.

Also. You don't really want the for loop either. That's just waisting time. What I would do in this scenario is run a for loop on the button array, and change the setActionCommand of each one to do as wanted. Then in actionperformed you could simply do your set text line and call e.getActionCommand instead. That would only be one call per action event, instead of constantly checking every button every time you want to do something.

EDIT: I didn't see you already changed what the buttons are. In that case you don't have to change setActionCommand, your entire action performed can just be text field.set text(e.getActionCommand);, but you probably want to do text field.setText(ttextField.get text() + e.getActionCommand()); so you aren't just replacing.
Haha, I had completely missed the while(true) when I looked at that earlier. Thanks for catching that for him.


Possibly Related Threads...
Thread / Author Replies Views Last Post
[Python] by billybluiceezDec 8, 2016 08:13 PM 3 153 Dec 8, 2016 08:13 PM
Last Post: billybluiceez
[Java] by Captain Steve RogersMar 17, 2015 02:40 PM 8 413 Mar 17, 2015 02:40 PM
Last Post: Leostraza
[.NET] by ChessoMar 11, 2015 08:50 AM 7 289 Mar 11, 2015 08:50 AM
Last Post: ElementalTree