couple of questions on this simple calculator
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.
Re: couple of questions on this simple calculator
I guess it would help if you had the code! :bigyello:
Code:
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();
}
}
Re: couple of questions on this simple calculator
Re: couple of questions on this simple calculator
Never mind with that...
Code:
TF.setHorizontalAlignment(JTextField.RIGHT);
Re: couple of questions on this simple calculator
To disable the maximizing you can do this :
Code:
setResizable(false);
that will disable all resizing (including maximizing). But I'm not sure it disabling all resizing is what you want :)
Re: couple of questions on this simple calculator
Re: couple of questions on this simple calculator
Quote:
Originally Posted by System_Error
ahhh thank you manova!!
You're welcome :)
*manavo :D
Re: couple of questions on this simple calculator
Hey, one more thing! How does the layout/interface look? Does anything need to be changed or added?
Re: couple of questions on this simple calculator
Quote:
Originally Posted by System_Error
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 :
Quote:
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 :
Code:
setLayout(new FlowLayout());
to :
Code:
getContentPane().setLayout(new FlowLayout());
as it suggested :)
Re: couple of questions on this simple calculator
hmm I didn't get that error. Im running 1.5, so that's probably why.
Re: couple of questions on this simple calculator
Quote:
Originally Posted by System_Error
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 :)