Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

what is easier using jbuttons or drawing squares and using them as buttons?

Posted on 2007-03-19
13
201 Views
Last Modified: 2010-03-31
I need to write  a program for class that allows the user to press buttons that will display coins to the screen and add time to a parking meter

I have the coins figured out from an earlier project but I was wondering about the button my instructor had mentioned just drawing squares on the screen, but wouldn't it be easier to use jbuttons?

so essentially the actions necessary would be if you press the quarter button it draws a quarter on the screen and adds 5 minutes to the meter

if you press te dime button it draws a dime and adds 2 minutes

the meter will be a semi cirlce with a line pinting to the time remaining.

thanks for any help you can provide.  I can provide what code I have written if that will help to make a decision.

U
0
Comment
Question by:urobins
  • 7
  • 6
13 Comments
 
LVL 92

Expert Comment

by:objects
ID: 18753163
0
 

Author Comment

by:urobins
ID: 18753185
Okay the part that I am confused on is how to assign a different action to each button... d you have an example of this?
0
 
LVL 92

Expert Comment

by:objects
ID: 18753283
eg. to call xyz() when button is pressed use:

JButton button = new JButton("Click me");
button.addActionListener(new ActionListener() {
   public void actionPerformed(ActionEvent event) {
       xyz();
   }
});

0
Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

 

Author Comment

by:urobins
ID: 18753298
Do I have to have a seperate action performed for each button?  This is how I am creating my buttons
I had planned on calling this addwidgets in the method I am creating my window in



// method to add widgets to the panel
       private void addWidgets() {
              //Create widgets.
              amount = new JTextField(2);
              amountLabel = new JLabel("Amount of change between 1 and 99", SwingConstants.HORIZONTAL);
              enterAmount = new JButton("Click to enter value");
              txtAmount = new JTextField(50);

              //Listen to events from the click button.
              enterAmount.addActionListener(this);

              //Add the widgets to the container.
              coinPanel.add(amount);
              coinPanel.add(amountLabel);
              coinPanel.add(enterAmount);
              textPanel.add(txtAmount);
              
              amountLabel.setBorder(BorderFactory.createEmptyBorder(5,5,5,5));
              
          }
0
 
LVL 92

Expert Comment

by:objects
ID: 18753307
no button can share ActionListener's
0
 

Author Comment

by:urobins
ID: 18753320
Okay so Im sorry for the next question, looking at the way I set it up is that right? and if so how do I differentiate between buttons?

IE how do I make quarters call the paint quarters
and dime call paint dimes etc...
0
 
LVL 92

Expert Comment

by:objects
ID: 18753331
if u want different buttons to have different actions, then use different ActionListeners :)
0
 

Author Comment

by:urobins
ID: 18753334
Sorry I posted the wrong code

public void addWidgets()
      {

            JButton quarterButton,nickleButton,dimeButton,pennyButton;
          
            //create the buttons
          quarterButton = new JButton ("25");
          dimeButton = new JButton("10");
          nickleButton = new JButton("5");
          pennyButton = new JButton("1");
          
          //add listeners to the buttons
          quarterButton.addActionListener(this);
          dimeButton.addActionListener(this);
          nickleButton.addActionListener(this);
          pennyButton.addActionListener(this);
          
          //add the buttons to the panel
          meterPanel.add(quarterButton);
          meterPanel.add(dimeButton);
          meterPanel.add(nickleButton);
          meterPanel.add(pennyButton);
          
      }
0
 

Author Comment

by:urobins
ID: 18753337
so is that right what I have above?  Im sorry I hav only ever used one actionlistener so I just need to be pointed in the right direction or an onilne tut or something :)
0
 
LVL 92

Accepted Solution

by:
objects earned 500 total points
ID: 18753378
they are all using the same listener.
you can do that and then in your listener check the source of the event

if (event.getSource()==quarterButton)
{
   // quarterButton pressed
}
if (event.getSource()==dimeButton)
{
   // dimeButton pressed
}


better to use seperate listeners though

quarterButton.addActionListener(new ActionListener() {
   public void actionPerformed(ActionEvent event) {
       quarterButtonPressed();
   }
});
0
 

Author Comment

by:urobins
ID: 18753391
Okay I think I get it so like this? and so on....

quarterButton.addActionListener(new ActionListener() {
   public void actionPerformed(ActionEvent event) {
       quarterButtonPressed();
   }
});

dimeButton.addActionListener(new ActionListener() {
   public void actionPerformed(ActionEvent event) {
       dimeButtonPressed();
   }
});
0
 
LVL 92

Expert Comment

by:objects
ID: 18753430
correct
0
 

Author Comment

by:urobins
ID: 18753437
Thanks I think I get it.  I have another question, but I'll open a new question for that!  
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
javap not working 8 57
servlet filter example 37 71
jdbc error in jsp application 20 63
going to wrong jsp page 2 33
This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…

792 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question