Results 1 to 6 of 6

Thread: GUI Display

  1. #1
    modhz is offline Member
    Join Date
    Oct 2011
    Posts
    10
    Rep Power
    0

    Default 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.

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: GUI Display

    In this code:

    Java Code:
      public void multiply()
      {
        if (b1.isSelected())
        {
          p = (b * b);
        }
    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.

    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.

  3. #3
    tnrh1 is offline Senior Member
    Join Date
    Aug 2011
    Posts
    251
    Rep Power
    3

    Default 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));

  4. #4
    modhz is offline Member
    Join Date
    Oct 2011
    Posts
    10
    Rep Power
    0

    Default Re: GUI Display

    I updated my post with the changed code.
    Quote Originally Posted by Fubarable View Post
    In this code:

    Java Code:
      public void multiply()
      {
        if (b1.isSelected())
        {
          p = (b * b);
        }
    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.

    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.
    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.

    Quote Originally Posted by tnrh1 View Post
    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));
    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.

  5. #5
    tnrh1 is offline Senior Member
    Join Date
    Aug 2011
    Posts
    251
    Rep Power
    3

    Default Re: GUI Display

    There is nothing bad in setting a default value :)

  6. #6
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: GUI Display

    Quote Originally Posted by modhz View Post
    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.
    Nope, your code looks much better, and so you don't need my help.

Similar Threads

  1. Replies: 1
    Last Post: 12-01-2010, 06:35 PM
  2. no display
    By Kyle227 in forum Java Applets
    Replies: 6
    Last Post: 07-22-2010, 10:01 AM
  3. Replies: 1
    Last Post: 05-31-2010, 08:46 AM
  4. Replies: 8
    Last Post: 12-29-2008, 07:12 AM
  5. Replies: 0
    Last Post: 06-28-2008, 09:26 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •