Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

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

Posted on 2007-03-19
13
Medium Priority
?
205 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
 

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
The top UI technologies you need to be aware of

An important part of the job as a front-end developer is to stay up to date and in contact with new tools, trends and workflows. That’s why you cannot miss this upcoming webinar to explore the latest trends in UI technologies!

 

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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.
Suggested Courses

670 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