Results 1 to 4 of 4
- 03-13-2011, 06:39 PM #1
Member
- Join Date
- Nov 2010
- Posts
- 38
- Rep Power
- 0
How to use method in ActionListener
I have a calculator.java file and calculatorGUI.java file.
In the calculator file, I've got
etc....Java Code:import java.util.Scanner; import java.lang.Math; public class Calculator { Double total = 0.0; Scanner scan = new Scanner(System.in); public void add(double numberToAdd) { total += numberToAdd; } public void minus(double numberToMinus) { total -= numberToMinus; } public void divide(double numberToDivide) { total /= numberToDivide; }
and in the calculator GUI I've got...
What I need help with is how to call and use a method (add) and use it in the ActionEventJava Code:import javax.swing.*; import java.awt.event.*; public class ButtonDemo extends Calculator{ private JButton btnAdd, btnMinus, btnMultiply, btnDivide, btnPercent, btnSqrRoot, btnTotal, btnExit; private JLabel lblTotal, lblNumber; private JTextField txtTotal, txtNumber; private JPanel panel; private JFrame frame; public ButtonDemo() { frame = new JFrame(); frame.setTitle("Calculator"); frame.setSize(300, 300); frame.setDefaultCloseOperation(frame.EXIT_ON_CLOSE); panel = new JPanel(); panel.setLayout(null); lblTotal = new JLabel("Total"); lblTotal.setBounds(10, 50, 100, 20); panel.add(lblTotal); txtTotal = new JTextField(); txtTotal.setBounds(110, 50, 150, 20); txtTotal.addActionListener(new TotalHandler()); panel.add(txtTotal); lblNumber = new JLabel("Number"); lblNumber.setBounds(10, 70, 100, 20); panel.add(lblNumber); txtNumber = new JTextField(""); txtNumber.setBounds(110, 70, 150, 20); panel.add(txtNumber); btnAdd = new JButton("Add"); btnAdd.setBounds(50, 110, 100, 20); btnAdd.addActionListener(new AddHandler()); panel.add(btnAdd); btnMinus = new JButton("Minus"); btnMinus.setBounds(150, 110, 100, 20); btnMinus.addActionListener(new MinusHandler()); panel.add(btnMinus); btnMultiply = new JButton("Multiply"); btnMultiply.setBounds(50, 130, 100, 20); btnMultiply.addActionListener(new MultiplyHandler()); panel.add(btnMultiply); btnDivide = new JButton("Divide"); btnDivide.setBounds(150, 130, 100, 20); btnDivide.addActionListener(new DivideHandler()); panel.add(btnDivide); btnPercent = new JButton("Percent"); btnPercent.setBounds(50, 150, 100, 20); btnPercent.addActionListener(new PercentHandler()); panel.add(btnPercent); btnSqrRoot = new JButton("SqrRoot"); btnSqrRoot.setBounds(150, 150, 100, 20); btnSqrRoot.addActionListener(new SqrRootHandler()); panel.add(btnSqrRoot); btnTotal = new JButton("Total"); btnTotal.setBounds(50, 170, 100, 20); btnTotal.addActionListener(new TotalHandler()); panel.add(btnTotal); btnExit = new JButton("Exit"); btnExit.setBounds(150, 170, 100, 20); btnExit.addActionListener(new ExitHandler()); panel.add(btnExit); frame.getContentPane().add(panel); frame.setVisible(true); } class TotalHandler implements ActionListener { public void actionPerformed(ActionEvent event) { txtTotal.setText(total + ""); } } class AddHandler implements ActionListener { public void actionPerformed(ActionEvent event) { Calculator c = new Calculator(); c.add(); } } class MinusHandler implements ActionListener { public void actionPerformed(ActionEvent event) { } } class MultiplyHandler implements ActionListener { public void actionPerformed(ActionEvent event) { } } class DivideHandler implements ActionListener { public void actionPerformed(ActionEvent event) { } } class PercentHandler implements ActionListener { public void actionPerformed(ActionEvent event) { } } class SqrRootHandler implements ActionListener { public void actionPerformed(ActionEvent event) { } } class ExitHandler implements ActionListener { public void actionPerformed(ActionEvent event) { System.exit(0); } } public static void main(String[] args) { new ButtonDemo(); } }
'' class AddHandler implements ActionListener {
public void actionPerformed(ActionEvent event) {
Calculator c = new Calculator();
c.add();
}
}''
I've tried the above but it doesn't work....
Help please!!!
I'm really new to Java, so simple terms please! :D
-
Declare a Calculator variable in your class, not in the actionPerformed method, and use that same variable in all your GUI calculation methods.
- 03-13-2011, 07:11 PM #3
Member
- Join Date
- Nov 2010
- Posts
- 38
- Rep Power
- 0
-
Well the error message makes sense because you don't want to call the add method without passing in a double into it. If you do that, what are you adding to the Calculator? It doesn't make sense. So you must pass a double into the method, right? And you will need to extract the appropriate String from one of your JTextFields, convert it to a double via Double.parseDouble(myString) and then call the add method with a double. Please let us know if this is confusing, and if so, what in particular confuses you.
Similar Threads
-
ActionListener issue.
By kbro3 in forum New To JavaReplies: 14Last Post: 02-24-2011, 06:08 AM -
Serialize ActionListener
By Dennis in forum Advanced JavaReplies: 4Last Post: 01-21-2011, 08:36 AM -
Determining ActionListener
By siamino in forum New To JavaReplies: 12Last Post: 05-25-2009, 11:04 PM -
ActionListener Error
By blackstormattack in forum New To JavaReplies: 1Last Post: 03-05-2009, 08:36 AM -
Java actionlistener help
By justsomeguy in forum AWT / SwingReplies: 1Last Post: 05-27-2008, 05:42 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks