Java 如何在另一个类中使用来自一个类的代码?(爪哇)

Java 如何在另一个类中使用来自一个类的代码?(爪哇),java,redundancy,extending,Java,Redundancy,Extending,我正在制作一个坦克游戏,为了避免冗余,我正在制作扩展类。 我的菜单面板看起来像自动取款机 (我只写了与问题有关的代码) (knop=按钮的荷兰语) 因为制作按钮的代码完全相同(背景路径和y坐标除外),所以我制作了一个类按钮: package menu; import java.awt.Image; import java.awt.event.ActionListener; import javax.swing.ImageIcon; import javax.swing.JButton; p

我正在制作一个坦克游戏,为了避免冗余,我正在制作扩展类。 我的菜单面板看起来像自动取款机 (我只写了与问题有关的代码) (knop=按钮的荷兰语)

因为制作按钮的代码完全相同(背景路径和y坐标除外),所以我制作了一个类按钮:

package menu;

import java.awt.Image;
import java.awt.event.ActionListener;

import javax.swing.ImageIcon;
import javax.swing.JButton;

public class button 
{


    public JButton button;
    public ImageIcon buttonImage;

    public int x = 95;
    public int width = 200;
    public int height = 50;

    public String backgroundPath;
    public int y;



    public button(String backgroundPath, int y)
    {
        this.backgroundPath = backgroundPath;
        this.y = y;

        buttonImage = new ImageIcon(PlayPanel.class.getResource(backgroundPath));
        button = new JButton();
        button.setBounds(x, y, width, height);;
        button.addActionListener(this); 
    }



}
这样,我的menuPanel可以如下所示:

    package menu;

    @SuppressWarnings("serial")
    public class MenuPanel extends JPanel implements ActionListener
    {

    private button playKnop, highScoreKnop, quitKnop, HTPKnop;
    private JTextField naam;
    private Tanks mainVenster;



    public MenuPanel(Tanks mainVenster) 
    {
        this.mainVenster = mainVenster;
        this.setLayout(null);       

        playKnop = new button("/buttons/PLAY.png", 350);        
        highScoreKnop = new button("/buttons/HS.png", 460);
        quitKnop = new button("/buttons/QUIT.png", 515);
        HTPKnop = new button("/buttons/HTP.png", 570);


        this.add(playKnop);
        this.add(quitKnop);
        this.add(HTPKnop);
        this.add(highScoreKnop);


        validate();

    }


}
我不知道为什么,但是当我这样做时,按钮不会出现,尽管button类中的代码是正确的(bc当我在menuPanel中使用代码时,它会工作)

我不知道如何解决这个问题,在我的整个JavaProject中有很多类似的问题,但是如果有人能告诉我如何解决这个问题,我就可以消除项目中的所有冗余

提前感谢,, 萝拉

非常感谢大家!您的答案组合帮助我让按钮出现,但出于某种原因,图像无法加载。我的代码现在看起来像:

公共类MenuPanel扩展JPanel实现ActionListener {

}


(所有的按钮都能用!)

你能观察到这两行之间的区别吗:

playKnop.addActionListener(this);
以及后面代码中的一个:

playKnop = new button("/buttons/PLAY.png", 350);  
我相信您希望添加
MenuPanel
作为操作侦听器,但在以后的代码中不会传递对它的引用。你应该:
playKnop=newbutton(“/buttons/PLAY.png”,350,这个)

然后在
按钮
类中设置此引用,如下所示:

public button(String backgroundPath, int y, MenuPanel menuPanel)
    {
        this.backgroundPath = backgroundPath;
        this.y = y;

        buttonImage = new ImageIcon(PlayPanel.class.getResource(backgroundPath));
        button = new JButton();
        button.setBounds(x, y, width, height);;
        button.addActionListener(menuPanel); 
    }

在现有的
菜单面板

this.add(playKnop);
您正在将
JButton
添加到
JPanel

在新的
菜单面板
中,您正在将
按钮
添加到
JPanel

您需要将
JButton
添加到
JPanel

还可以选择
按钮
而不是
按钮
作为类名


解决方案是重写此代码,以便将
JButton
s而不是
button
s添加到面板中。代码可能还有其他问题。

首先,所有类名都应该以大写字母开头,这是一种惯例,这不影响编译,但是一种良好的做法

现在,对于您的实际问题,我首先想到的是,您的
按钮
类没有扩展任何内容。在我看来,这似乎是它没有出现在屏幕上的原因。 我建议制作
按钮
扩展
JButton

public class Button extends JButton {
// Implementation Code
}

这类似于
MenuPanel
扩展
JPanel
的方式。
JPanel
无法呈现您编写的自定义
按钮。通过扩展
JButton
,它将能够呈现它,因为它知道如何与
JButton

交互,您添加的内容有差异。在新代码中,您将
按钮的实例添加到JPanel中。在旧代码中添加了
JButtons
<代码>按钮
不能显示,因为它们不是swing类,旧的JButton可以

从JButton扩展按钮应该可以做到:

public class button extends JButton {

// public JButton button; // this line is depreceted now

public ImageIcon buttonImage;

...

另外,了解Java中的代码格式和样式。这将帮助您编写更清晰的代码,并防止上述混淆。例如,您有一个名为
button
的类,其中有一个名为
button
的字段和一个名为
button
的方法。使用不同的名称,如
按钮
(类总是大写!)、
按钮
创建按钮
,尝试在类按钮中添加getButton以返回用于添加的按钮

public JButton getJButton(){
 retun this.button
}
然后添加在菜单面板上添加时使用此选项

this.add(playKnop.getJButton());

你的代码没有问题。我能看到的唯一问题是类
按钮
扩展
对象
,而不是
JButton
。因此,只要做以下更改,我觉得它应该会起作用:

而不是:

public class button 

public class button extends JButton

也许您想让button类继承JButton,而不是保留button字段。这样,您就可以将按钮添加到面板中。

不同之处在于您不再将JButtons添加到JPanel中

要使此代码正常工作,您的“button”类必须扩展JPanel

提取共享代码的另一种方法是创建一个“按钮创建者方法”,而不是一个类,如:

public class MenuPanel extends JPanel implements ActionListener
{
    int y = 95, width = 200, height = 50;
    private JButton playKnop, highScoreKnop, quitKnop, HTPKnop;

    public MenuPanel()
    {
        this.setLayout(null);

        playKnop = createButton("/buttons/PLAY.png", 350);
        highScoreKnop = createButton("/buttons/HS.png", 460);
        quitKnop = createButton("/buttons/QUIT.png", 515);
        HTPKnop = createButton("/buttons/HTP.png", 570);

        this.add(playKnop);
        this.add(quitKnop);
        this.add(HTPKnop);
        this.add(highScoreKnop);

        validate();
    }

    private JButton createButton(String path, int x) {
        ImageIcon icon = new ImageIcon(MenuPanel.class.getResource(path));
        JButton button = new JButton(icon);
        button.setBounds(x, y, width, height);
        button.addActionListener(this);
        return button;
    }

    @Override
    public void actionPerformed(ActionEvent e) {
    }
}

我认为你最好让你的Button类扩展JButton。这样,您就可以将自己的button类添加到menupanel中

public类按钮扩展JButton
{
公共按钮;
公共图像图标按钮图像;
公共整数x=95;
公共整数宽度=200;
公共内部高度=50;
公共字符串背景路径;
公共智力;
公共按钮(字符串背景路径,int y,菜单面板菜单面板)
{
超级()
this.backgroundPath=backgroundPath;
这个。y=y;
buttonImage=newImageIcon(PlayPanel.class.getResource(backgroundPath));
这是.立根(x,y,宽度,高度);;
this.addActionListener(menuPanel);
}
}

我对JButton类不是很熟悉。您需要将buttonImage设置为背景。可能有一些功能,如:

this.setBackground(buttonImage);
如果没有函数liek this,可以在构造函数中设置背景。与上面的示例类似。您始终使用以下选项创建新按钮:

JButton b = new JButton(buttonImage);
您可以在自己的构造函数中为super()-methode提供相同的参数。结果将如下所示:

public class Button extends JButton
{
   String backgroundPath;
   int y;

   public Button(String backgroundPath, int y, MenuPanel menuPanel)
   {
       super(new ImageIcon(PlayPanel.class.getResource(backgroundPath)));
       this.backgroundPath = backgroundPath;
       this.y = y;

       this.setBounds(x, y, width, height);
       this.addActionListener(menuPanel); 
   }
}

此代码未经测试。

当您传递
时,实际上传递了对该类实例的引用,该实例在您的情况下为
MenuPanel
。顺便问一下,它解决了你的问题吗?是的,非常感谢,我收到的一系列答案对我帮助很大。一切正常,但我还有最后一个问题。我想使用这段代码是多个面板,所以我需要一个参数
MenuPanel menuPan
JButton b = new JButton(buttonImage);
public class Button extends JButton
{
   String backgroundPath;
   int y;

   public Button(String backgroundPath, int y, MenuPanel menuPanel)
   {
       super(new ImageIcon(PlayPanel.class.getResource(backgroundPath)));
       this.backgroundPath = backgroundPath;
       this.y = y;

       this.setBounds(x, y, width, height);
       this.addActionListener(menuPanel); 
   }
}