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
Thanks I really appreciate your help!
Jekwa
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

