Results 1 to 9 of 9
Thread: multiple windows coming up
- 04-26-2010, 03:11 AM #1
multiple windows coming up
i am trying to create a calculator and when i run it multiple windows come up and don't stop. i don't know what is did wrong. also i am using eclipse.
here is my Method class
here is my bhandler classJava Code:import javax.swing.*; import java.awt.*; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.JFrame; public class Method extends JFrame { JTextField result; //numbers JButton zero; JButton one; JButton two; JButton three; JButton four; JButton five; JButton six; JButton seven; JButton eight; JButton nine; JButton decimal; //operators JButton clear; JButton subnum; JButton addnum; JButton disnum; JButton equals; JButton divide; JButton mult; JButton subtract; JButton add; public Method() { super("the title"); setSize(300, 260); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); FlowLayout flow = new FlowLayout(); setLayout(flow); setVisible(true); result = new JTextField("0", 20); result.setEditable(false); clear = new JButton("CE"); subnum = new JButton("M-"); addnum = new JButton("M+"); disnum = new JButton("MS"); equals = new JButton("="); decimal = new JButton("."); zero = new JButton("0"); one = new JButton("1"); two = new JButton("2"); three = new JButton("3"); four = new JButton("4"); five = new JButton("5"); six = new JButton("6"); seven = new JButton("7"); eight = new JButton("8"); nine = new JButton("9"); divide = new JButton("/"); mult = new JButton("*"); subtract = new JButton("-"); add = new JButton("+"); //row1 JPanel row1 = new JPanel(); row1.add(result); add(row1); //row2 JPanel row2 = new JPanel(); row2.add(seven); row2.add(eight); row2.add(nine); row2.add(divide); row2.add(disnum); add(row2); //row3 JPanel row3 = new JPanel(); row3.add(four); row3.add(five); row3.add(six); row3.add(mult); row3.add(addnum); add(row3); //row4 JPanel row4 = new JPanel(); row4.add(one); row4.add(two); row4.add(three); row4.add(subtract); row4.add(subnum); add(row4); //row5 JPanel row5 = new JPanel(); row5.add(zero); row5.add(decimal); row5.add(equals); row5.add(add); row5.add(clear); add(row5); bhandler bh = new bhandler(); zero.addActionListener(bh); one.addActionListener(bh); two.addActionListener(bh); three.addActionListener(bh); four.addActionListener(bh); five.addActionListener(bh); six.addActionListener(bh); seven.addActionListener(bh); eight.addActionListener(bh); nine.addActionListener(bh); decimal.addActionListener(bh); clear.addActionListener(bh); subnum.addActionListener(bh); addnum.addActionListener(bh); disnum.addActionListener(bh); equals.addActionListener(bh); divide.addActionListener(bh); mult.addActionListener(bh); subtract.addActionListener(bh); add.addActionListener(bh); result.addActionListener(bh); } public static void main(String[] args) { Method md = new Method(); } }
please helpJava Code:import java.awt.event.ActionListener; import java.awt.event.ActionEvent; import javax.swing.JOptionPane; public class bhandler extends Method implements ActionListener { int operator; double innumber1; double innumber2; public void actionPerformed(ActionEvent event) { if(event.getSource()==zero) { if(result.getText().equals("0")) { result.setText("0"); }else{ result.setText(Double.toString(1.0 / Double.parseDouble(result.getText()))); } }else if(event.getSource()==one){ if(result.getText().equals("0")) { result.setText("1"); }else{ result.setText(result.getText() + "1"); } }else if(event.getSource()==two){ if(result.getText().equals("0")) { result.setText("2"); }else{ result.setText(result.getText() + "2"); } }else if(event.getSource()==three) { if(result.getText().equals("0")) { result.setText("3"); }else{ result.setText(result.getText() + "3"); } }else if(event.getSource()==four) { if(result.getText().equals("0")) { result.setText("4"); }else{ result.setText(result.getText() + "4"); } }else if(event.getSource()==five) { if(result.getText().equals("0")) { result.setText("5"); }else{ result.setText(result.getText() + "5"); } }else if(event.getSource()==six) { if(result.getText().equals("0")) { result.setText("6"); }else{ result.setText(result.getText() + "6"); } }else if(event.getSource()==seven) { if(result.getText().equals("0")) { result.setText("7"); }else{ result.setText(result.getText() + "7"); } }else if(event.getSource()==eight) { if(result.getText().equals("0")) { result.setText("8"); }else{ result.setText(result.getText() + "8"); } }else if(event.getSource()==nine) { if(result.getText().equals("0")) { result.setText("9"); }else{ result.setText(result.getText() + "9"); } }else if(event.getSource()==add) { innumber1 = Double.parseDouble(result.getText()); result.setText("0"); operator = 1; }else if(event.getSource()==subtract) { innumber1 = Double.parseDouble(result.getText()); result.setText("0"); operator = 2; }else if(event.getSource()==mult) { innumber1 = Double.parseDouble(result.getText()); result.setText("0"); operator = 3; }else if(event.getSource()==divide) { innumber1 = Double.parseDouble(result.getText()); result.setText("0"); operator = 4; }else if(event.getSource()==equals) { innumber2 = Double.parseDouble(result.getText()); switch(operator) { case 1: result.setText(Double.toString(innumber1 + innumber2)); innumber1 = 0.0; innumber2 = 0.0; case 2: result.setText(Double.toString(innumber1 - innumber2)); innumber1 = 0.0; innumber2 = 0.0; case 3: result.setText(Double.toString(innumber1 * innumber2)); innumber1 = 0.0; innumber2 = 0.0; case 4: if(innumber1!= 0.0) { result.setText(Double.toString(innumber1 / innumber2)); innumber1 = 0.0; innumber2 = 0.0; }else{ JOptionPane.showConfirmDialog(null, "You can not divide by 0", "error", JOptionPane.ERROR_MESSAGE); innumber1 = 0.0; innumber2 = 0.0; } } }else if(event.getSource()==clear) { result.setText("0"); } } }
-
You're using inheritance for the wrong purpose, and it's biting you in the rear by causing unexpected recursion.
First of all what you're doing wrong is this:
Java Code:public class bhandler [color="red"][b]extends Method[/b][/color] implements ActionListener {
What this does is that when Method is created and its constructor is called, it createsa bhandler object whose constructor (since it extends Method) calls the Method constructor, which will create a bhandler object which will call the Method constructor, which will... ad infinitum.
So this is obviously bad because it causes the recursion, but it's also bad because a bhandler object isn't a Method object -- the two things are completely different, and thus bhandler shouldn't be extending Method. You likely made it extend Method so you could have a reference to the fields contained in Method such as the reference field, but even though bhandler has a reference to this field, it's not the same reference field as the one held in the Method object that is being displayed initially.
What you really should be doing is giving the bhandler a Method field and pass a reference to the Method object (this) into bhandler in bhandler's constructor:
Java Code:class Bhandler implements ActionListener { int operator; double innumber1; double innumber2; Method method; public Bhandler(Method method) { this.method = method; } public void actionPerformed(ActionEvent event) { if (event.getSource() == zero) { if (method.result.getText().equals("0")) { method.result.setText("0"); //.....
- 04-26-2010, 04:46 AM #3
thanks Fubarable. :)
-
You're welcome. Next thing you should do is to change all class names to have their first letter capitalized. This may seem trivial, but if you adhere to conventions, your code will be more easily understood by others (such as us or your instructors) who read it.
Then you should make the fields of your classes private (unless you have a good reason not to do so), and create public accessor and mutator methods. For instance you would need methods to get and set the reference JTextField and then call these methods from the Listener object.
Much luck!
- 04-26-2010, 05:12 AM #5
another question. is it possible to create a array of JButtons.
like instead of writing
if so can you show me.Java Code:JButton zero; JButton one; JButton two; JButton three; JButton four; JButton five; JButton six; JButton seven; JButton eight; JButton nine; JButton decimal; JButton clear; JButton subnum; JButton addnum; JButton disnum; JButton equals; JButton divide; JButton mult; JButton subtract; JButton add;
-
You don't really need an array of JButton since you don't really need a reference to the JButton but rather need to make sense of the text that the JButton is displaying. When I've done this before, I've used a two-dimensional array of String, because then I could see logically how the buttons are laid out, something like so:
Java Code:public class SimpleCalc { private static final String[][] BTN_STRINGS = { {"7", "8", "9", "/", "sqrt"}, {"4", "5", "6", "*", "%"}, {"1", "2", "3", "-", "1/x"}, {"0", "+/-", ".", "+", "="}}; private static final String NUMBER_BTN_STRINGS = "0123456789."; private JPanel mainPanel = new JPanel(); private JTextField displayTField = new JTextField();
Here I used the String constant NUMBER_BTN_STRINGS to determine which buttons will get the NumberActionListener added to them:
I'm not saying that this will work for you, but it has for me. Best of luck.Java Code:public SimpleCalc() { JPanel buttonPanel = new JPanel(new GridLayout(0, BTN_STRINGS[0].length, 3, 3)); NumberActionListener numberActionListener = new NumberActionListener(); OperationActionListener operationActionListener = new OperationActionListener(); for (int i = 0; i < BTN_STRINGS.length; i++) { for (int j = 0; j < BTN_STRINGS[i].length; j++) { String btnString = BTN_STRINGS[i][j]; JButton button = new JButton(btnString); buttonPanel.add(button); // if button represents a number or period: if (NUMBER_BTN_STRINGS.contains(btnString)) { button.addActionListener(numberActionListener); } else { // it's a math operation button.addActionListener(operationActionListener); } } } mainPanel.setBorder(new EmptyBorder(3, 3, 3, 3)); mainPanel.setLayout(new BorderLayout(3, 3)); mainPanel.add(displayTField, BorderLayout.NORTH); mainPanel.add(buttonPanel, BorderLayout.CENTER); }
- 04-26-2010, 08:41 PM #7
thanks but i am going to leave it as it is for now.
-
-
Here's one of the listeners:
Java Code:import java.awt.*; import java.awt.event.*; import javax.swing.*; import javax.swing.border.EmptyBorder; public class SimpleCalc { private static final String[][] BTN_STRINGS = { {"7", "8", "9", "/", "sqrt"}, {"4", "5", "6", "*", "%"}, {"1", "2", "3", "-", "1/x"}, {"0", "+/-", ".", "+", "="}}; private static final String NUMBER_BTN_STRINGS = "0123456789."; private JPanel mainPanel = new JPanel(); private JTextField displayTField = new JTextField(); public SimpleCalc() { JPanel buttonPanel = new JPanel(new GridLayout(0, BTN_STRINGS[0].length, 3, 3)); NumberActionListener numberActionListener = new NumberActionListener(); OperationActionListener operationActionListener = new OperationActionListener(); for (int i = 0; i < BTN_STRINGS.length; i++) { for (int j = 0; j < BTN_STRINGS[i].length; j++) { String btnString = BTN_STRINGS[i][j]; JButton button = new JButton(btnString); buttonPanel.add(button); // if button represents a number or period: if (NUMBER_BTN_STRINGS.contains(btnString)) { button.addActionListener(numberActionListener); } else { // it's a math operation button.addActionListener(operationActionListener); } } } mainPanel.setBorder(new EmptyBorder(3, 3, 3, 3)); mainPanel.setLayout(new BorderLayout(3, 3)); mainPanel.add(displayTField, BorderLayout.NORTH); mainPanel.add(buttonPanel, BorderLayout.CENTER); } public JPanel getMainPanel() { return mainPanel; } private class NumberActionListener implements ActionListener { private static final int MAX_LENGTH = 15; public void actionPerformed(ActionEvent e) { String displayText = displayTField.getText(); String buttonString = e.getActionCommand(); if (displayText.length() >= MAX_LENGTH) { return; } // to avoid putting duplicate period in display text: if (!(buttonString.equals(".") && displayText.contains("."))) { displayText += buttonString; displayTField.setText(displayText); } } } private class OperationActionListener implements ActionListener { public void actionPerformed(ActionEvent e) { // TODO: finish this } } private static void createAndShowUI() { JFrame frame = new JFrame("Simple Calc"); frame.getContentPane().add(new SimpleCalc().getMainPanel()); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.pack(); frame.setLocationRelativeTo(null); frame.setVisible(true); } public static void main(String[] args) { java.awt.EventQueue.invokeLater(new Runnable() { public void run() { createAndShowUI(); } }); } }
Similar Threads
-
multiple windows and eventlisteners
By kmyr in forum New To JavaReplies: 3Last Post: 01-10-2010, 01:26 PM -
Please help output not coming
By majin_harish in forum New To JavaReplies: 2Last Post: 02-05-2009, 09:45 AM -
Why is the answer not coming out
By anonymous18 in forum New To JavaReplies: 4Last Post: 11-12-2008, 03:10 AM -
How can i know from which jsp request is coming?
By vishnujava in forum Java ServletReplies: 1Last Post: 08-06-2008, 01:13 PM -
Opeing multiple pdf files in different acrobat reader windows
By shweta.ahuja in forum Web FrameworksReplies: 2Last Post: 05-07-2008, 12:33 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks