Solved

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

Posted on 2007-03-19
13
202 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
[X]
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
  • 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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
runtime exception 2 50
maven module vs maven project 3 73
Desingning Refactoring existing code 2 31
add projects t working set in maven 2 38
Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues undeā€¦
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilationā€¦
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.

761 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