Solved

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

Posted on 2007-03-19
13
203 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
Revamp Your Training Process

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action.

 

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: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone 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
swing controls 2 37
Java 8 to Java 6 8 43
spring maven example issues 3 105
Hibernate Inheritance Strategy for Abstract Base class and two concrete classes 3 39
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…
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
The viewer will learn how to implement Singleton Design Pattern in Java.
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.
Suggested Courses

738 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