对于我正在做的项目,我必须创建一个名为BirdButton的类,它的构造函数中有4个或5个参数.它看起来像下面这样.注意动物是一个单独的类,是一些鸟类的图片.鸟笼将根据输入突出显示图像中的不同鸟类.
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) {
}
}
Run Code Online (Sandbox Code Playgroud)
在另一个类中,我尝试将按钮添加到JFrame,但是传递给按钮的agruments数量是4或5.对于这个项目,另一个类,驱动程序,不能更改,看起来像这样
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();
}
}
Run Code Online (Sandbox Code Playgroud)
当我尝试编译代码时,我收到一个错误,参数的数量不同,所以它不会工作.如何编写BirdButton以接受4或5个输入?
谢谢
使用重载的构造函数:
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);
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
39 次 |
| 最近记录: |