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