I've created a calculator. Two .java files, one the calculator, one the GUI.
I'm stuck on how to link up the jButton ActionListeners.... wondering if anyone can help...
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) { AddTotal totals = new AddTotal(); } } 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(); } }
import javax.swing.*; import java.awt.event.*; public class ButtonDemo extends Calculator{ private JButton btnAdd, btnMinus, btnMultiply, btnDivide, btnPercent, btnSqrRoot, 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(); 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); btnExit = new JButton("Exit"); btnExit.setBounds(100, 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) { !!!!!!STUCK HERE!!!!!! Need to call the 'add()' method from calculator......!!!! } } 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(); } }