PDA

Click to See Complete Forum and Search --> : couple of questions on this simple calculator


System_Error
Jan 15th, 2005, 07:44 PM
Right now, I've just got the interface of the calculator. It's not too great and that's one thing I would like help with, any improvements will do. Also, the biggest thing I need help with, is the textfield. If you look at other calculators, you see they go right to left, instead of left to right. How can I do this? Another thing, I tried the setMaximizable(false) method, and it didn't work.

Hope I'm not asking to much.

System_Error
Jan 15th, 2005, 07:44 PM
I guess it would help if you had the code! :bigyello:


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

public class Calculator extends JFrame implements ActionListener
{
JButton[] btnNums;
JButton btnBack;
JButton btnClear;
JButton btnCalculate;

JTextField txtDisplay;

public Calculator()
{
Container content = getContentPane();
setSize(210,250);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

/* construct a JPanel and the textfield that
* will show the inputed values and outputed
* results
*/
JPanel jpDisplay = new JPanel();
jpDisplay.setLayout(new BorderLayout());
txtDisplay = new JTextField(15);
jpDisplay.add(txtDisplay);

/* contstruct a JPanel that will contain a back
* button and a clear button.
*/
JPanel jpRow2 = new JPanel();
btnBack = new JButton("Backspace");
btnClear = new JButton("Clear");
jpRow2.add(btnBack);
jpRow2.add(btnClear);

/* construct a string array with all the names of the
* buttons, then in a for loop create the new buttons
* and add their name and an actionListener to them.
*/
String[] strNames = {"7","8", "9","/", "4", "5", "6","*", "1", "2", "3","+", "0", "+/-", ".", "-"};
btnNums = new JButton[16];
JPanel jpButtons = new JPanel();
jpButtons.setLayout(new GridLayout(4,4));

for (int i = 0; i < 16; i++)
{
btnNums[i] = new JButton(strNames[i]);
btnNums[i].addActionListener(this);
jpButtons.add(btnNums[i]);
}

/* construct the final JPanel and add a
* calculate button to it.
*/
JPanel jpLastRow = new JPanel();
btnCalculate = new JButton("Calculate");
jpLastRow.add(btnCalculate);

/* set the contentPane and create the layout
* add the panels to the container
*/
setContentPane(content);
setLayout(new FlowLayout());
content.add(jpDisplay, BorderLayout.NORTH);
content.add(jpRow2);
content.add(jpButtons);
content.add(jpLastRow);
setTitle("Mini Calculator");
setVisible(true);

}

public void actionPerformed(ActionEvent ae)
{
}

public static void main(String[] args)
{
Calculator calc = new Calculator();
}
}

manavo11
Jan 15th, 2005, 08:37 PM
For the alignment, this came up in my search : http://www.it.bton.ac.uk/staff/je/java/jewl/javadoc/jewl/TextField.html :)

manavo11
Jan 15th, 2005, 08:50 PM
Never mind with that...

TF.setHorizontalAlignment(JTextField.RIGHT);

manavo11
Jan 15th, 2005, 08:55 PM
To disable the maximizing you can do this :

setResizable(false);

that will disable all resizing (including maximizing). But I'm not sure it disabling all resizing is what you want :)

System_Error
Jan 15th, 2005, 08:58 PM
ahhh thank you manova!!

manavo11
Jan 15th, 2005, 09:01 PM
ahhh thank you manova!!

You're welcome :)

*manavo :D

System_Error
Jan 16th, 2005, 11:11 AM
Hey, one more thing! How does the layout/interface look? Does anything need to be changed or added?

manavo11
Jan 16th, 2005, 02:36 PM
Hey, one more thing! How does the layout/interface look? Does anything need to be changed or added?

Something that might be useful is a square root button, it's common :)

Also, as a side note, when I tried to run it I got this note :

Exception in thread "main" java.lang.Error: Do not use Calculator.setLayout() us
e Calculator.getContentPane().setLayout() instead
at javax.swing.JFrame.createRootPaneException(JFrame.java:465)
at javax.swing.JFrame.setLayout(JFrame.java:531)
at Calculator.<init>(Calculator.java:65)
at Calculator.main(Calculator.java:81)

Which of course was fixed by changing this line :

setLayout(new FlowLayout());

to :

getContentPane().setLayout(new FlowLayout());

as it suggested :)

System_Error
Jan 16th, 2005, 03:21 PM
hmm I didn't get that error. Im running 1.5, so that's probably why.

manavo11
Jan 16th, 2005, 03:27 PM
hmm I didn't get that error. Im running 1.5, so that's probably why.

That would probably explain it, I have 1.4.2 :)