Reputation: 53
for a project I am doing, I have to create a class called BirdButton which has either 4 or 5 arguments in its constructor. It looks something like the following. Note animals is a separate class which is a picture of some birds. birdbutton will, depending on the input, highlight different birds in the image.
import java.awt.event.ActionEvent;
public class BirdButton extends EventButton
{
public BirdButton(String n, int x, int y, Animals a){
super(n);
setLabel(n);
setBounds(50,10,x,y);
a.add(this);
}
public void actionPerformed(ActionEvent e) {
}
}
In another class I attempt to add the Buttons to the JFrame, however the number of agruments passed to the button is either 4 or 5. For this project the other class, driver, cannot be changed and looks like this
public class Driver {
private JFrame win;
private Animals animals = new Animals();
private BirdButton nextBtn, enlargeBtn, shrinkBtn, moveToBtn;
private JTextField field;
public Driver() {
win = new JFrame("Angry Animal Name Game");
win.setBounds(100, 100, 600, 600);
win.setLayout(null);
win.setVisible(true);
nextBtn = new BirdButton( "NEXT", 10, 10, animals);
win.add(nextBtn, 0);
enlargeBtn = new BirdButton( "ENLARGE", 10, 60, animals);
win.add(enlargeBtn, 0);
shrinkBtn = new BirdButton( "SHRINK", 10, 110, animals);
win.add(shrinkBtn, 0);
field = new JTextField();
field.setBounds(10, 250, 100, 20);
win.add(field, 0);
moveToBtn = new BirdButton( "MOVETO", 10, 275, animals, field);
win.add(moveToBtn, 0);
win.add(animals, 0);
animals.recenter();
win.repaint();
}
}
When I try to compile the code, I receive an error that the number of arguments differs in length and so it wont work. How do I code the BirdButton to accept either 4 or 5 inputs?
Thanks
Upvotes: 1
Views: 56
Reputation: 50716
Use an overloaded constructor:
public class BirdButton extends EventButton
{
public BirdButton(String n, int x, int y, Animals a, JTextField field) {
super(n);
setLabel(n);
setBounds(50,10,x,y);
setField(field);
a.add(this);
}
public BirdButton(String n, int x, int y, Animals a) {
this(n, x, y, a, null);
}
}
Upvotes: 5