Results 1 to 4 of 4
- 05-25-2009, 12:41 AM #1
[SOLVED] [newbie] AbstractButton.setText() not setting text within JButton
I have this calculator program and it does not seem to clear the display text. So when I select the '=' the previous text does not clear.
:confused:
NOTE: It would be nice to have line numbers for code snippets.Java Code:CalculatorPanel.java package homenetwork.bkr.training; import java.awt.*; import java.awt.event.*; import javax.swing.*; @SuppressWarnings("serial") public class CalculatorPanel extends JPanel { public CalculatorPanel() { setLayout(new BorderLayout()); result =0; lastCommand = "="; start=true; //add the display display = new JButton("0"); display.setEnabled(false); add(display, BorderLayout.NORTH); ActionListener insert = new InsertAction(); @SuppressWarnings("unused") ActionListener command = new CommandAction(); //add the buttons in a 4 X 4 grid panel = new JPanel(); panel.setLayout(new GridLayout(4,4)); addButton("7", insert); addButton("8", insert); addButton("9", insert); addButton("/", insert); addButton("4", insert); addButton("5", insert); addButton("6", insert); addButton("*", insert); addButton("1", insert); addButton("2", insert); addButton("3", insert); addButton("-", insert); addButton("0", insert); addButton(".", insert); addButton("=", insert); addButton("+", insert); add(panel,BorderLayout.CENTER); } /** * Adds a button to the center panel. * @param label: the button label * @param listener: the button listener */ private void addButton(String label, ActionListener listener) { JButton button = new JButton(label); button.addActionListener(listener); panel.add(button); } /** * This action inserts the button action string to the end of the display text. */ private class InsertAction implements ActionListener { public void actionPerformed (ActionEvent event) { String input = event.getActionCommand(); if (start) { display.setText(""); start = false; } display.setText(display.getText() + input); } } /** * This action executes the command that the button action string denotes. */ private class CommandAction implements ActionListener { public void actionPerformed (ActionEvent event) { String command = event.getActionCommand(); if (start) { if (command.equals("-")) { display.setText(command); start=false; } else lastCommand = command; } else { calculate(Double.parseDouble(display.getText())); lastCommand = command; start = true; } } } /** * Carries out the pending calculation */ public void calculate(double x) { if (lastCommand.equals("+")) result += x; else if (lastCommand.equals("-")) result -= x; else if (lastCommand.equals("*")) result *= x; else if (lastCommand.equals("/")) result /= x; else if (lastCommand.equals("=")) result = x; [B]display.setText("" + result);[/B] } [B]private JButton display;[/B] private JPanel panel; private double result; private String lastCommand; private boolean start; } CalculatorFrame.java package homenetwork.bkr.training; import javax.swing.*; @SuppressWarnings("serial") public class CalculatorFrame extends JFrame { public CalculatorFrame() { setTitle("Calculator"); CalculatorPanel panel = new CalculatorPanel(); add(panel); pack(); } } CalculatorTest.java package homenetwork.bkr.training; import java.awt.EventQueue; import javax.swing.JFrame; public class CalculatorTest { /** * @param args */ public static void main(String[] args) { EventQueue.invokeLater(new Runnable() { public void run() { CalculatorFrame frame = new CalculatorFrame(); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setVisible(true); } }); } }Last edited by jon80; 05-25-2009 at 12:42 AM. Reason: update
- 05-25-2009, 12:55 AM #2
Don't forget to mark threads as [SOLVED] and give reps to helpful posts.
How To Ask Questions The Smart Way
- 05-25-2009, 02:27 AM #3
Senior Member
- Join Date
- Mar 2009
- Posts
- 552
- Rep Power
- 5
Hmm... line numbers would be nice in code snippets...
@OrangeDog Probably wouldn't be very good for the code to have line numbers entered into it. I think he means line numbers added to it whenever you post.
@OP Post a thread in the suggestions forum, see what happensIf the above doesn't make sense to you, ignore it, but remember it - might be useful!
And if you just randomly taught yourself to program, well... you're just like me!
-
repaint() may not help here as you're calling setText on a JTextField, and Swing should update that automatically without a repaint. Rather, I wonder if the problem is that you're giving all of your buttons InsertAction ActionListeners including the non-numeric buttons, the ones that perhaps would work better if assigned CommandAction listeners?
Similar Threads
-
setText() problem
By jls7168 in forum New To JavaReplies: 2Last Post: 02-20-2009, 10:34 PM -
how to use live validation with autocomplete in dojo text boxes in <s:text box>
By subashm28 in forum Suggestions & FeedbackReplies: 2Last Post: 01-23-2009, 04:09 PM -
[SOLVED] new2java- gettext settext
By obdi in forum New To JavaReplies: 4Last Post: 07-21-2008, 09:28 AM -
setText in event
By sniezna.stopa in forum SWT / JFaceReplies: 0Last Post: 06-20-2008, 02:56 PM -
Error: javax.swing.AbstractButton cannot be applied to...
By barney in forum AWT / SwingReplies: 1Last Post: 08-01-2007, 06:10 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks