Results 1 to 6 of 6
Thread: GUI Display
- 10-26-2011, 11:58 PM #1
Member
- Join Date
- Oct 2011
- Posts
- 10
- Rep Power
- 0
GUI Display
I am trying to make a program where you can select to square or cube the numbers two or three.
By default, the options "square" and "2" are selected. I would like for this value (4) to appear in the product box once the program is run.
Java Code:import java.awt.* ; import java.awt.event.*; import javax.swing.*; import java.text.*; public class Calc extends JFrame implements ActionListener { JRadioButton button1, button2; ButtonGroup buttonGroup; JPanel buttonPanel; JRadioButton number1, number2; ButtonGroup numberGroup; JPanel numberPanel; JTextField result; JLabel resultLabel; JPanel resultPanel; int n; double product; public Calc() { setTitle( "Multiplying Values" ); setDefaultCloseOperation( EXIT_ON_CLOSE ); button1 = new JRadioButton("Square"); button2 = new JRadioButton("Cube"); button1.setSelected(true); button1.addActionListener(this); button2.addActionListener(this); buttonGroup = new ButtonGroup(); buttonGroup.add( button1); buttonGroup.add( button2 ); buttonPanel = new JPanel(); buttonPanel.setLayout( new BoxLayout( buttonPanel, BoxLayout.Y_AXIS ) ); buttonPanel.add( new JLabel("Square or Cube") ); buttonPanel.add( button1 ); buttonPanel.add( button2 ); number1 = new JRadioButton("2"); number2 = new JRadioButton("3"); number1.setSelected(true); number1.addActionListener(this); number2.addActionListener(this); numberGroup = new ButtonGroup(); numberGroup.add( number1 ); numberGroup.add( number2 ); numberPanel = new JPanel(); numberPanel.setLayout( new BoxLayout( numberPanel, BoxLayout.Y_AXIS ) ); numberPanel.add( new JLabel("Numbers") ); numberPanel.add( number1 ); numberPanel.add( number2 ); result = new JTextField(3); result.setEditable( false ); resultLabel = new JLabel("Product"); resultPanel = new JPanel(); resultPanel.add( resultLabel ); resultPanel.add( result ); add( buttonPanel, BorderLayout.WEST ); add(numberPanel, BorderLayout.EAST); add( resultPanel, BorderLayout.SOUTH ); } public void multiply() { if (number1.isSelected()) { n = 2; } if (number2.isSelected()) { n = 3; } if (button1.isSelected()) { product = (n * n); } if (button2.isSelected()) { product = (n * n * n); } } public void actionPerformed(ActionEvent evt) { multiply(); result.setText( new DecimalFormat("#0.0##").format(product)); repaint(); } public static void main ( String[] args ) { Calc multV = new Calc() ; multV.setSize( 300, 300 ); multV.setResizable( false ); multV.setVisible( true ); } }Last edited by modhz; 10-27-2011 at 12:55 AM.
-
Re: GUI Display
In this code:
Your presupposing that b has already been set, when it will only be set if the action command is A or B. Rather than do it that way, why not directly poll the radiobutton and set b that way.Java Code:public void multiply() { if (b1.isSelected()) { p = (b * b); }
So consider first: checking which number has been selected by checking the corresponding JRadioButton and then setting p, and then checking which operation has been selected, again by calling a method on the corresponding JRadioButton and then performing this operation. This will help simplify your program by allowing you to remove a bunch of redundant code as well.
As an aside: you really should change the names of your variables as they don't have any intrinsic sense. Give them names so you know what they should be doing and so that your code is self-documenting. This will make it much easier for us to understand and be able to help you and much easier for your future self to understand as well.
- 10-27-2011, 12:21 AM #3
Senior Member
- Join Date
- Aug 2011
- Posts
- 249
- Rep Power
- 2
Re: GUI Display
You wrote all of that and you sure you don't know how to do it?alittle bit wierd.
look for this line: result.setEditable( false );
and just add result.setText("4");
Exactly as you did down there: result.setText( new DecimalFormat("#0.0##").format(p));
- 10-27-2011, 12:50 AM #4
Member
- Join Date
- Oct 2011
- Posts
- 10
- Rep Power
- 0
Re: GUI Display
I updated my post with the changed code.
Could you use some more simple words (not when referring to syntax of course)?
I'm not entirely sure if you meant to make more separate methods other than multiply(), or just to use the isSelected() method. If I use the isSelected() method, I can remove some lines from the actionPerformed() method.
I thought this was a forum for beginners. Beginners will be confused.
I am aware that I could do this, but I thought it would be better to set the default displayed value using a calculation.Last edited by modhz; 10-27-2011 at 12:53 AM.
- 10-27-2011, 01:16 AM #5
Senior Member
- Join Date
- Aug 2011
- Posts
- 249
- Rep Power
- 2
Re: GUI Display
There is nothing bad in setting a default value :)
-
Re: GUI Display
Similar Threads
-
Display only certain contents of text file and edit display
By blkshrk81 in forum New To JavaReplies: 1Last Post: 12-01-2010, 06:35 PM -
no display
By Kyle227 in forum Java AppletsReplies: 6Last Post: 07-22-2010, 10:01 AM -
What is the different between Text format display on web browser and display on midle
By Basit781 in forum CLDC and MIDPReplies: 1Last Post: 05-31-2010, 08:46 AM -
How to display a list of items and on click display subitems?
By mandyj in forum New To JavaReplies: 8Last Post: 12-29-2008, 07:12 AM -
How to display information about the display device in SWT
By Java Tip in forum SWTReplies: 0Last Post: 06-28-2008, 09:26 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks