Solved

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

Posted on 2007-03-19
13
197 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
Comment Utility
0
 

Author Comment

by:urobins
Comment Utility
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
Comment Utility
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
 

Author Comment

by:urobins
Comment Utility
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
Comment Utility
no button can share ActionListener's
0
 

Author Comment

by:urobins
Comment Utility
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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
LVL 92

Expert Comment

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

Author Comment

by:urobins
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
correct
0
 

Author Comment

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

Featured Post

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
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 about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
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:

763 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now