Results 1 to 5 of 5
Thread: Calculator
- 09-23-2009, 04:27 AM #1
Member
- Join Date
- Sep 2009
- Posts
- 4
- Rep Power
- 0
-
If you have a specific question regarding your code, we'd be more than happy to help you. If on the other hand you're looking for a "give me some code" site, then this isn't it.
Much luck.
- 09-23-2009, 05:34 AM #3
Member
- Join Date
- Sep 2009
- Posts
- 4
- Rep Power
- 0
Calculator
i have just designed GUI,but i have got a problem in solving events.
XML Code:import java.text.DecimalFormat; import java.awt.*; import javax.swing.*; import java.awt.event.*; class Caculator implements ActionListener{ double currentTotal = 0.0; DecimalFormat df = new DecimalFormat("0.00"); boolean totalDisplayed = true; JFrame f; JTextField t1; JLabel l1; JPanel p2 ; JButton b1; JButton b2; JButton b3; JButton b4; JButton b5; JButton b6; JButton b7; JButton b8; JButton b9; JButton b10; JButton b11; JButton b12; JButton b13; JButton b14; JButton b15; JButton b16; JButton b17; JButton b18; JButton b19; JButton b20; public Caculator() { f = new JFrame("Caculator"); l1 = new JLabel("|--------CASIO--------|"); p2 = new JPanel(); t1 = new JTextField("0.00",25); b1 = new JButton("MODE"); b2 = new JButton("^"); b3 = new JButton("AC"); b4 = new JButton("DEL"); b3.setBackground(Color.pink); b4.setBackground(Color.pink); b5 = new JButton("1"); b6 = new JButton("2"); b7 = new JButton("3"); b8 = new JButton("+"); b9 = new JButton("4"); b10 = new JButton("5"); b11 = new JButton("6"); b12 = new JButton("-"); b13 = new JButton("7"); b14 = new JButton("8"); b15 = new JButton("9"); b16 = new JButton("x"); b17 = new JButton("P"); b18 = new JButton("."); b19 = new JButton("/"); b20 = new JButton("="); } public void fFrame(){ p2.setLayout(new GridLayout(5,4,4,4)); p2.add(b1); p2.add(b2); p2.add(b3); p2.add(b4); p2.add(b5); p2.add(b6); p2.add(b7); p2.add(b8); p2.add(b9); p2.add(b10); p2.add(b11); p2.add(b12); p2.add(b13); p2.add(b14); p2.add(b15); p2.add(b16); p2.add(b17); p2.add(b18); p2.add(b19); p2.add(b20); f.setLayout(new FlowLayout()); f.add(t1); f.add(l1); f.add(p2); t1.setBackground(Color.YELLOW); f.setBackground(Color.YELLOW); f.setResizable(false); f.setBounds(300,300,300,240); f.setVisible(true); /* b1.setActionCommand("b1"); b2.setActionCommand("b2"); b3.setActionCommand("b3"); b4.setActionCommand("b4"); b5.setActionCommand("b5"); b6.setActionCommand("b6"); b7.setActionCommand("b7"); b8.setActionCommand("b8"); b9.setActionCommand("b9"); b10.setActionCommand("b10"); b11.setActionCommand("b11"); b12.setActionCommand("b12"); b13.setActionCommand("b13"); b14.setActionCommand("b14"); b15.setActionCommand("b15"); b16.setActionCommand("b16"); b17.setActionCommand("b17"); b18.setActionCommand("b18"); b19.setActionCommand("b19"); b20.setActionCommand("b20"); */ b1.addActionListener(this); b2.addActionListener(this); b3.addActionListener(this); b4.addActionListener(this); b5.addActionListener(this); b6.addActionListener(this); b7.addActionListener(this); b8.addActionListener(this); b9.addActionListener(this); b10.addActionListener(this); b11.addActionListener(this); b12.addActionListener(this); b13.addActionListener(this); b14.addActionListener(this); b15.addActionListener(this); b16.addActionListener(this); b17.addActionListener(this); b18.addActionListener(this); b19.addActionListener(this); b20.addActionListener(this); } public void actionPerformed(ActionEvent e){ //[COLOR="Red"][SIZE="5"] i can't do it.[/SIZE][/COLOR] } public static void main (String[] args) { Caculator a= new Caculator(); a.fFrame(); } }
-
Hello again. What problems are you having? I'm not trying to nit pic here, but rather I'm trying to make it easier for others to help you since the more specific your question, the more specific and helpful the answer. Much luck and welcome to the forum.
Edit: also some other advice:
1) If you used arrays here, you could shorten this code 3-fold if not more. This would also make it easier to debug and enhance the code since any changes desired in the behavior of say the number buttons could be done in one small method, rather than 10 separate methods. This lessons the chances of making an error.
2) I strongly suggest that you not use "this" as your ActoinListener but rather use a few private inner classes instead. What this would do would be to group the actions that should stay together (such as the number button actions) yet separate the actions that should be kept apart (for instance a number button shouldn't share the same ActionListener as the "+" operations button since they do vastly different things. This separation will make it easier to figure out what goes in the action listeners -- starting with the easiest -- the numbers.
Much luck!Last edited by Fubarable; 09-23-2009 at 05:49 AM.
- 09-23-2009, 06:00 AM #5
Member
- Join Date
- Sep 2009
- Posts
- 4
- Rep Power
- 0
Similar Threads
-
help with calculator
By kalibballer in forum New To JavaReplies: 8Last Post: 04-01-2009, 12:57 PM -
mortgage calculator help
By kalibballer in forum New To JavaReplies: 1Last Post: 03-26-2009, 11:35 AM -
Calculator program
By kevzspeare in forum New To JavaReplies: 6Last Post: 03-18-2009, 01:43 PM -
Calculator help.
By madkidd02 in forum New To JavaReplies: 2Last Post: 10-25-2008, 07:42 AM -
Swing Calculator
By nemo in forum AWT / SwingReplies: 1Last Post: 05-28-2007, 11:07 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks