Hi, ive been looking for a solid java community to help me when im tearing out my hair :)
Basically ive constructed a GUI that has to represent the same look and functions of the typical windows calculator.
Ive made 4 classes 2 do this, my reasoning so it was easier to look through( when programming) rather than getting mixed up in my own code!
My questions and problems:
Ive been messing around with the windows look and feel, somethign seems a little off about it...have i did somethign wrong?
As you can see from my code ive only made the actual GUI, i dont have a clue how to add functionality to the buttons. Im not here asking for one of you to do everything, is it possible someone could explain how id go about this? OR give a small example? (even if it was an example for one of the buttons id bebale to understand and work through it again :))
I prob realise theres much better ways to go this program than the wya i have done it, but atleast im trying :) -
-
import javax.swing.*;
-
import java.awt.*;
-
-
// The look and feel stuff, still to fix
-
import javax.swing.plaf.metal.MetalLookAndFeel;
-
import com.sun.java.swing.plaf.windows.WindowsLookAndFeel;
-
-
class CalcFrame extends JFrame
-
{
-
/* Main Panels */
-
viewPanel tp;
-
calcPanel1 bp1;
-
calcPanel2 bp2;
-
-
/** menu **/
-
JMenuBar jmb;
-
JMenu jmi1,jmi2,jmi3;
-
CalcFrame()
-
{
-
super("Calculator");
-
Container cp = getContentPane();
-
cp.setLayout(new FlowLayout());
-
-
cp.setBackground(new Color(241,237,222) );
-
tp = new viewPanel();
-
bp1 = new calcPanel1();
-
bp2 = new calcPanel2();
-
cp.add(tp);
-
cp.add(bp1);
-
cp.add(bp2);
-
-
/** Menu (top bar) **/
-
jmb = new JMenuBar();
-
jmb.setBackground(new Color(241,237,222));
-
jmi1 = new JMenu("Edit",true);
-
jmi1.setBackground(new Color(241,237,222));
-
jmi2 = new JMenu("View",true);
-
jmi2.setBackground(new Color(241,237,222));
-
jmi3 = new JMenu("Help",true);
-
jmi3.setBackground(new Color(241,237,222));
-
jmb.add(jmi1);
-
jmb.add(jmi2);
-
jmb.add(jmi3);
-
this.setJMenuBar(jmb);
-
-
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
-
this.setSize(340,320);
-
-
// Twtf, fix this...doesnt look right :S
-
try
-
{
-
UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
-
}
-
catch(Exception cnfe)
-
{
-
System.out.println("Error, Error, Error'");
-
}
-
-
SwingUtilities.updateComponentTreeUI(getContentPane());
-
-
-
setVisible(true);
-
setResizable(false);
-
-
}
-
-
public static void main(String args[])
-
{
-
CalcFrame cf = new CalcFrame();
-
}
-
-
-
}
-
-
-
import javax.swing.*;
-
import java.awt.*;
-
-
-
class calcPanel1 extends JPanel
-
{
-
JButton blank, BackSpace, CE, C;
-
-
calcPanel1()
-
{
-
Color buttons_color = new Color(242,244,241);
-
Color background_color = new Color(241,237,222);
-
blank = new JButton(" ");
-
blank.setDefaultCapable(false);
-
blank.setBackground(buttons_color);
-
blank.setEnabled(false);
-
blank.setMargin(new Insets(6,7,6,7));
-
add(blank);
-
BackSpace = new JButton("BackSpace");
-
BackSpace.setForeground(Color.red);
-
BackSpace.setBackground(buttons_color);
-
BackSpace.setMargin(new Insets(6,4,6,4));
-
add(BackSpace);
-
CE = new JButton("CE");
-
CE.setForeground(Color.red);
-
CE.setBackground(buttons_color);
-
CE.setMargin(new Insets(6,29,6,29));
-
add(CE);
-
C = new JButton("C");
-
C.setForeground(Color.red);
-
C.setBackground(buttons_color);
-
C.setMargin(new Insets(6,29,6,29));
-
-
add(C);
-
setBackground(background_color);
-
-
}
-
-
public static void main(String args[])
-
{
-
JFrame tester = new JFrame("Calculator");
-
Container c = tester.getContentPane();
-
calcPanel1 t = new calcPanel1();
-
c.add(t);
-
tester.setSize(300,300);
-
tester.setVisible(true);
-
}
-
}
-
-
-
import javax.swing.*;
-
import java.awt.*;
-
-
-
-
class calcPanel2 extends JPanel
-
{
-
JButton MC, MR, MS, MPlus;
-
JButton Slash, Star, Minus, Plus, Sqrt, Percent, Reciprocal,Equals, PlusMinus,Point;
-
JButton []digits;
-
-
-
calcPanel2()
-
{
-
setupButtons();
-
Color background_color = new Color(241,237,222);
-
setBackground(background_color);
-
setLayout(new GridLayout(4,6,5,5));
-
-
// row 1
-
this.add(MC);
-
this.add(digits[7]);
-
this.add(digits[8]);
-
this.add(digits[9]);
-
this.add(Slash);
-
this.add(Sqrt);
-
// row 2
-
this.add(MR);
-
this.add(digits[4]);
-
this.add(digits[5]);
-
this.add(digits[6]);
-
this.add(Star);
-
this.add(Percent);
-
// row 3
-
this.add(MS);
-
this.add(digits[1]);
-
this.add(digits[2]);
-
this.add(digits[3]);
-
this.add(Minus);
-
this.add(Reciprocal);
-
//row 4
-
this.add(MPlus);
-
this.add(digits[0]);
-
this.add(PlusMinus);
-
this.add(Point);
-
this.add(Plus);
-
this.add(Equals);
-
-
}
-
-
public static void main(String args[])
-
{
-
JFrame tester = new JFrame("Calculator");
-
Container c = tester.getContentPane();
-
calcPanel2 t = new calcPanel2();
-
c.add(t);
-
tester.setSize(300,300);
-
tester.setVisible(true);
-
}
-
-
public void setupButtons()
-
{
-
Color buttons_color = new Color(242,244,241);
-
Insets buttonMargin = new Insets(5,1,5,1);
-
// set up Mem Clear
-
MC = new JButton("MC");
-
MC.setBackground(buttons_color);
-
MC.setForeground(Color.red);
-
MC.setMargin(buttonMargin);
-
-
// set up Mem Recall
-
MR = new JButton("MR");
-
MR.setBackground(buttons_color);
-
MR.setForeground(Color.red);
-
MR.setMargin(buttonMargin);
-
-
// Set up Mem Store
-
MS = new JButton("MS");
-
MS.setBackground(buttons_color);
-
MS.setForeground(Color.red);
-
MS.setMargin(buttonMargin);
-
-
//Set up Mem +
-
MPlus = new JButton("M+");
-
MPlus.setBackground(buttons_color);
-
MPlus.setForeground(Color.red);
-
MS.setMargin(buttonMargin);
-
-
// Set up /
-
Slash = new JButton("/");
-
Slash.setBackground(buttons_color);
-
Slash.setForeground(Color.red);
-
Slash.setMargin(buttonMargin);
-
-
// Set up *
-
Star = new JButton("*");
-
Star.setBackground(buttons_color);
-
Star.setForeground(Color.red);
-
Star.setMargin(buttonMargin);
-
-
// Set up -
-
Minus = new JButton("-");
-
Minus.setBackground(buttons_color);
-
Minus.setForeground(Color.red);
-
Minus.setMargin(buttonMargin);
-
-
// Set up +
-
Plus = new JButton("+");
-
Plus.setBackground(buttons_color);
-
Plus.setForeground(Color.red);
-
Plus.setMargin(buttonMargin);
-
-
// Set up sqrt
-
Sqrt = new JButton("sqrt");
-
Sqrt.setBackground(buttons_color);
-
Sqrt.setForeground(Color.blue);
-
Sqrt.setMargin(buttonMargin);
-
-
// Set up %
-
Percent = new JButton("%");
-
Percent.setBackground(buttons_color);
-
Percent.setForeground(Color.blue);
-
Percent.setMargin(buttonMargin);
-
-
// Set up Reci
-
Reciprocal = new JButton("1/x");
-
Reciprocal.setBackground(buttons_color);
-
Reciprocal.setForeground(Color.blue);
-
Reciprocal.setMargin(buttonMargin);
-
-
// Set up =
-
Equals = new JButton("=");
-
Equals.setBackground(buttons_color);
-
Equals.setForeground(Color.red);
-
Equals.setMargin(buttonMargin);
-
-
// Set up .
-
Point = new JButton(".");
-
Point.setBackground(buttons_color);
-
Point.setForeground(Color.red);
-
Point.setMargin(buttonMargin);
-
-
// Set up +/-
-
PlusMinus = new JButton("+/-");
-
PlusMinus.setBackground(buttons_color);
-
PlusMinus.setForeground(Color.blue);
-
PlusMinus.setMargin(buttonMargin);
-
-
digits = new JButton[10];
-
-
for (int i=0;i<10;i++)
-
{
-
digits[i] = new JButton(""+i);
-
digits[i].setBackground(buttons_color);
-
digits[i].setForeground(Color.blue);
-
digits[i].setMargin(buttonMargin);
-
}
-
-
}
-
}
-
-
-
import javax.swing.*;
-
import java.awt.*;
-
import javax.swing.plaf.metal.MetalLookAndFeel;
-
import com.sun.java.swing.plaf.windows.WindowsLookAndFeel;
-
-
class viewPanel extends JPanel
-
{
-
JTextField output;
-
-
viewPanel()
-
{
-
output = new JTextField("0",28);
-
output.setFont(new Font("Courier", Font.PLAIN, 16));
-
// Align text to the right.
-
output.setHorizontalAlignment(4);
-
this.add(output);
-
Color background_color = new Color(241,237,222);
-
this.setBackground(background_color);
-
}
-
-
public static void main(String args[])
-
{
-
// change so it looks better
-
-
JFrame tester = new JFrame("Calculator");
-
Container c = tester.getContentPane();
-
textPanel t = new textPanel();
-
c.add(t);
-
tester.setSize(300,300);
-
tester.setVisible(true);
-
}
-
}
-
Thanks for reading
Kind regards
Bob
3 14928
Here it is the same thing as you did, but much simplier .... also the functionality isn't implemented (action performed on each tiny little button) ...
import java.awt.*;
import javax.swing.*;
/**
* @author Sorinel CRISTESCU
*/
public class SccCalculator extends JPanel {
JTextField output;
public SccCalculator () {
super (new GridBagLayout());
// first line ... the digits ...
GridBagConstraints c = new GridBagConstraints ();
c.gridx = 0;
c.gridy = 0;
c.ipadx = 6;
c.ipady = 6;
c.gridwidth = 7;
c.weightx = 100;
c.insets = new Insets (3,3,3,3);
c.fill = c.HORIZONTAL;
output = new JTextField("0");
output.setFont(new Font("Courier", Font.PLAIN, 14));
output.setHorizontalAlignment(SwingConstants.RIGHT );
this.add(output, c);
// second line ... NOBUTTON, Backspace, CE & C buttons
c.gridwidth = 1;
c.gridy = 1;
c.weightx = 1;
c.fill = c.NONE;
JButton nobut = new JButton(" ");
nobut.setBorder(BorderFactory.createEmptyBorder()) ;
nobut.setEnabled(false);
this.add(nobut, c);
c.gridx = 1;
c.gridwidth = 2;
c.weightx = 30;
c.fill = c.HORIZONTAL;
JButton backspace = new JButton("Backspace");
backspace.setForeground(Color.red);
this.add(backspace, c);
c.gridx = 3;
c.gridwidth = 1;
JButton ce = new JButton("CE");
ce.setForeground(Color.red);
this.add(ce, c);
c.gridx = 4;
c.gridwidth = 2;
JButton clear = new JButton("C");
clear.setForeground(Color.red);
this.add(clear, c);
// third line MC, 7, 8, 9, /, sqrt
c.gridy = 2;
c.gridx = 0;
c.gridwidth = 1;
c.weightx = 1;
JButton mc = new JButton("MC");
mc.setForeground(Color.red);
this.add(mc, c);
nobut.setPreferredSize(mc.getPreferredSize());
c.gridx = 1;
JButton seven = new JButton("7");
seven.setForeground(Color.blue);
this.add(seven, c);
c.gridx = 2;
JButton eight = new JButton("8");
eight.setForeground(Color.blue);
this.add(eight, c);
c.gridx = 3;
JButton nine = new JButton("9");
nine.setForeground(Color.blue);
this.add(nine, c);
c.gridx = 4;
JButton divide = new JButton("/");
divide.setForeground(Color.red);
this.add(divide, c);
c.gridx = 5;
JButton sqrt = new JButton("sqrt");
sqrt.setForeground(Color.blue);
this.add(sqrt, c);
// forth line MR, 4, 5, 6, *, %
c.gridy = 3;
c.gridx = 0;
JButton mr = new JButton("MR");
mr.setForeground(Color.red);
this.add(mr, c);
c.gridx = 1;
JButton four = new JButton("4");
four.setForeground(Color.blue);
this.add(four, c);
c.gridx = 2;
JButton five = new JButton("5");
five.setForeground(Color.blue);
this.add(five, c);
c.gridx = 3;
JButton six = new JButton("6");
six.setForeground(Color.blue);
this.add(six, c);
c.gridx = 4;
JButton multiply = new JButton("*");
multiply.setForeground(Color.red);
this.add(multiply, c);
c.gridx = 5;
JButton procent = new JButton("%");
procent.setForeground(Color.blue);
this.add(procent, c);
// fifth line MS, 1, 2, 3, ., +, =
c.gridy = 4;
c.gridx = 0;
JButton ms = new JButton("MS");
ms.setForeground(Color.red);
this.add(ms, c);
c.gridx = 1;
JButton one = new JButton("1");
one.setForeground(Color.blue);
this.add(one, c);
c.gridx = 2;
JButton two = new JButton("2");
two.setForeground(Color.blue);
this.add(two, c);
c.gridx = 3;
JButton three = new JButton("3");
three.setForeground(Color.blue);
this.add(three, c);
c.gridx = 4;
JButton minus = new JButton("-");
minus.setForeground(Color.red);
this.add(minus, c);
c.gridx = 5;
JButton divideby = new JButton("1/x");
divideby.setForeground(Color.blue);
this.add(divideby, c);
// sixth line M+, 0, +/-, ., +, =
c.gridy = 5;
c.gridx = 0;
JButton mplus = new JButton("M+");
mplus.setForeground(Color.red);
this.add(mplus, c);
c.gridx = 1;
JButton zero = new JButton("0");
zero.setForeground(Color.blue);
this.add(zero, c);
c.gridx = 2;
JButton plusminus = new JButton("+/-");
plusminus.setForeground(Color.blue);
this.add(plusminus, c);
c.gridx = 3;
JButton dot = new JButton(".");
dot.setForeground(Color.blue);
this.add(dot, c);
c.gridx = 4;
JButton plus = new JButton("+");
plus.setForeground(Color.red);
this.add(plus, c);
c.gridx = 5;
JButton egal = new JButton("=");
egal.setForeground(Color.blue);
this.add(egal, c);
}
/**
* @param args
*/
public static void main(String[] args) {
// try {
// UIManager.setLookAndFeel(UIManager.getSystemLookAn dFeelClassName());
// } catch (Exception cnfe) {
// System.out.println("Error, Error, Error'");
// }
SccCalculator panel = new SccCalculator ();
JDialog dlg = new JDialog ();
dlg.setTitle("Simple Calculator");
dlg.getContentPane().add (panel);
dlg.pack();
dlg.setModal(true);
dlg.setResizable(false);
dlg.setVisible(true);
System.exit(0);
}
}
well sample action listener for buttons 7,8,9 is added.
rest do yourself by implementing logic.
Different actionlistener for each button is also possible. - import java.awt.*;
-
import java.awt.event.ActionEvent;
-
import java.awt.event.ActionListener;
-
import javax.swing.*;
-
/**
-
* @author Sorinel CRISTESCU
-
*/
-
public class SccCalculator extends JPanel implements ActionListener {
-
JTextField output;
-
public SccCalculator () {
-
super (new GridBagLayout());
-
// first line ... the digits ...
-
GridBagConstraints c = new GridBagConstraints ();
-
c.gridx = 0;
-
c.gridy = 0;
-
c.ipadx = 6;
-
c.ipady = 6;
-
c.gridwidth = 7;
-
c.weightx = 100;
-
c.insets = new Insets (3,3,3,3);
-
c.fill = c.HORIZONTAL;
-
output = new JTextField("0");
-
output.setFont(new Font("Courier", Font.PLAIN, 14));
-
output.setHorizontalAlignment(SwingConstants.RIGHT );
-
this.add(output, c);
-
// second line ... NOBUTTON, Backspace, CE & C buttons
-
c.gridwidth = 1;
-
c.gridy = 1;
-
c.weightx = 1;
-
c.fill = c.NONE;
-
JButton nobut = new JButton(" ");
-
nobut.setBorder(BorderFactory.createEmptyBorder()) ;
-
nobut.setEnabled(false);
-
this.add(nobut, c);
-
c.gridx = 1;
-
c.gridwidth = 2;
-
c.weightx = 30;
-
c.fill = c.HORIZONTAL;
-
JButton backspace = new JButton("Backspace");
-
backspace.setForeground(Color.red);
-
this.add(backspace, c);
-
c.gridx = 3;
-
c.gridwidth = 1;
-
JButton ce = new JButton("CE");
-
ce.setForeground(Color.red);
-
this.add(ce, c);
-
c.gridx = 4;
-
c.gridwidth = 2;
-
JButton clear = new JButton("C");
-
clear.setForeground(Color.red);
-
this.add(clear, c);
-
// third line MC, 7, 8, 9, /, sqrt
-
c.gridy = 2;
-
c.gridx = 0;
-
c.gridwidth = 1;
-
c.weightx = 1;
-
JButton mc = new JButton("MC");
-
mc.setForeground(Color.red);
-
this.add(mc, c);
-
nobut.setPreferredSize(mc.getPreferredSize());
-
c.gridx = 1;
-
JButton seven = new JButton("7");
-
seven.setForeground(Color.blue);
-
seven.setActionCommand("7");
-
seven.addActionListener(this);
-
this.add(seven, c);
-
c.gridx = 2;
-
JButton eight = new JButton("8");
-
eight.setForeground(Color.blue);
-
eight.setActionCommand("8");
-
eight.addActionListener(this);
-
this.add(eight, c);
-
c.gridx = 3;
-
JButton nine = new JButton("9");
-
nine.setForeground(Color.blue);
-
nine.setActionCommand("9");
-
nine.addActionListener(this);
-
this.add(nine, c);
-
c.gridx = 4;
-
JButton divide = new JButton("/");
-
divide.setForeground(Color.red);
-
this.add(divide, c);
-
c.gridx = 5;
-
JButton sqrt = new JButton("sqrt");
-
sqrt.setForeground(Color.blue);
-
this.add(sqrt, c);
-
// forth line MR, 4, 5, 6, *, %
-
c.gridy = 3;
-
c.gridx = 0;
-
JButton mr = new JButton("MR");
-
mr.setForeground(Color.red);
-
this.add(mr, c);
-
c.gridx = 1;
-
JButton four = new JButton("4");
-
four.setForeground(Color.blue);
-
this.add(four, c);
-
c.gridx = 2;
-
JButton five = new JButton("5");
-
five.setForeground(Color.blue);
-
this.add(five, c);
-
c.gridx = 3;
-
JButton six = new JButton("6");
-
six.setForeground(Color.blue);
-
this.add(six, c);
-
c.gridx = 4;
-
JButton multiply = new JButton("*");
-
multiply.setForeground(Color.red);
-
this.add(multiply, c);
-
c.gridx = 5;
-
JButton procent = new JButton("%");
-
procent.setForeground(Color.blue);
-
this.add(procent, c);
-
// fifth line MS, 1, 2, 3, ., +, =
-
c.gridy = 4;
-
c.gridx = 0;
-
JButton ms = new JButton("MS");
-
ms.setForeground(Color.red);
-
this.add(ms, c);
-
c.gridx = 1;
-
JButton one = new JButton("1");
-
one.setForeground(Color.blue);
-
this.add(one, c);
-
c.gridx = 2;
-
JButton two = new JButton("2");
-
two.setForeground(Color.blue);
-
this.add(two, c);
-
c.gridx = 3;
-
JButton three = new JButton("3");
-
three.setForeground(Color.blue);
-
this.add(three, c);
-
c.gridx = 4;
-
JButton minus = new JButton("-");
-
minus.setForeground(Color.red);
-
this.add(minus, c);
-
c.gridx = 5;
-
JButton divideby = new JButton("1/x");
-
divideby.setForeground(Color.blue);
-
this.add(divideby, c);
-
// sixth line M+, 0, +/-, ., +, =
-
c.gridy = 5;
-
c.gridx = 0;
-
JButton mplus = new JButton("M+");
-
mplus.setForeground(Color.red);
-
this.add(mplus, c);
-
c.gridx = 1;
-
JButton zero = new JButton("0");
-
zero.setForeground(Color.blue);
-
this.add(zero, c);
-
c.gridx = 2;
-
JButton plusminus = new JButton("+/-");
-
plusminus.setForeground(Color.blue);
-
this.add(plusminus, c);
-
c.gridx = 3;
-
JButton dot = new JButton(".");
-
dot.setForeground(Color.blue);
-
this.add(dot, c);
-
c.gridx = 4;
-
JButton plus = new JButton("+");
-
plus.setForeground(Color.red);
-
this.add(plus, c);
-
c.gridx = 5;
-
JButton egal = new JButton("=");
-
egal.setForeground(Color.blue);
-
this.add(egal, c);
-
}
-
/**
-
* @param args
-
*/
-
public static void main(String[] args) {
-
// try {
-
// UIManager.setLookAndFeel(UIManager.getSystemLookAn dFeelClassName());
-
// } catch (Exception cnfe) {
-
// System.out.println("Error, Error, Error'");
-
// }
-
SccCalculator panel = new SccCalculator ();
-
JDialog dlg = new JDialog ();
-
dlg.setTitle("Simple Calculator");
-
dlg.getContentPane().add (panel);
-
dlg.pack();
-
dlg.setModal(true);
-
dlg.setResizable(false);
-
dlg.setVisible(true);
-
System.exit(0);
-
}
-
public void actionPerformed(ActionEvent ae) {
-
// TODO Auto-generated method stub
-
String comm=ae.getActionCommand();
-
//String count[]=new String[2];
-
if(comm=="7")
-
{
-
output.setText(comm);
-
}
-
if(comm=="8")
-
{
-
output.setText(comm);
-
}
-
if(comm=="9")
-
{
-
output.setText(comm);
-
}
-
}
-
}
-
Now that's some thread digging.
Post your reply Sign in to post your reply or Sign up for a free account.
Similar topics
10 posts
views
Thread by Dr. Mercury |
last post: by
| | | | | | | | | | | | | | | | | | |