如何使用另一个类中的代码? (JAVA)

时间:2015-05-07 08:57:13

标签: java redundancy extending

我正在进行坦克游戏并避免冗余我正在进行课程扩展。 我的MenuPanel看起来像这个atm (我只编写了对问题很重要的代码) (knop = dutch for button)

public class MenuPanel extends JPanel implements ActionListener
{

    private JButton playKnop, highScoreKnop, quitKnop, HTPKnop;
    private ImageIcon play, HS, quit, HTP;
    private Tanks mainVenster;

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

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


        play = new ImageIcon(PlayPanel.class.getResource(/buttons/PLAY.png));
        playKnop = new JButton(play);
        playKnop.setBounds(x, y, width, height);
        playKnop.addActionListener(this);

        HS = new ImageIcon(PlayPanel.class.getResource(/buttons/HS.png));
        highScoreKnop = new JButton(HS);
        highScoreKnop.setBounds(x, 460, width, height);
        highScoreKnop.addActionListener(this);

        HTP = new ImageIcon(PlayPanel.class.getResource(/buttons/HTP.png));
        HTPKnop = new JButton(HTP);
        HTPKnop.setBounds(x, 515, width, height);
        HTPKnop.addActionListener(this);

        quit = new ImageIcon(PlayPanel.class.getResource(/buttons/QUIT.png));
        quitKnop = new JButton(quit);
        quitKnop.setBounds(x, 570, width, height);
        quitKnop.addActionListener(this);

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

        validate();
    }
}

因为制作按钮的代码完全相同(除了backgroundPath和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();

    }


}

我不知道为什么,但是当我这样做时,按钮不会出现,虽然按钮类中的代码是正确的(当我使用menuPanel本身的代码时它是有效的)< / p>

我不知道如何解决这个问题,我在整个JavaProject中遇到了类似的问题,但如果有人能解释我如何解决这个问题,我可以摆脱项目中的所有冗余。 / p>

提前致谢, 洛拉

非常感谢你们!你的答案的组合帮助我让按钮出现,但由于某种原因,图像不会加载它们。我的代码现在看起来像:

公共类MenuPanel扩展了JPanel实现的ActionListener {

private Button playKnop, highScoreKnop, quitKnop, HTPKnop;

private Tanks mainVenster;

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

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

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

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

    validate();

}

public class Button extends JButton
{
    JButton button;
    ImageIcon buttonImage;

    String backgroundPath;
    int y;


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

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

}

(所有按钮都有效!)

10 个答案:

答案 0 :(得分:2)

首先,所有类名都应以大写字母作为约定开头,这不会影响编译,但这是一种很好的做法。

现在,对于您的实际问题,首先要表明的是Button类不扩展任何内容。这对我来说似乎是它没有出现在屏幕上的原因。 我建议Button延长JButton

public class Button extends JButton {
// Implementation Code
}

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

答案 1 :(得分:2)

我认为你可以更好地让你的Button类扩展JButton。这样,您只需将自己的按钮类添加到菜单面板即可。

public class Button extends JButton
{


    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, MenuPanel menuPanel)
    {
        super()
        this.backgroundPath = backgroundPath;
        this.y = y;

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



}

答案 2 :(得分:1)

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

playKnop.addActionListener(this);

以及后面代码中的那个:

playKnop = new button("/buttons/PLAY.png", 350);  

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

然后在Button类中设置此引用,如:

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); 
    }

答案 3 :(得分:1)

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

要使此代码生效,您的“按钮”类必须扩展JPanel。

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

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) {
    }
}

答案 4 :(得分:0)

在现有MenuPanel中,当您执行

this.add(playKnop);

您要在JButton

中添加JPanel

在新的MenuPanel中,您要为button添加JPanel

您需要将JButton添加到JPanel

同样为类名选择Button而不是button

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

答案 5 :(得分:0)

您添加的内容存在差异。在新代码中,您将button类的实例添加到JPanel。在旧代码中添加了JButtonsbuttons无法显示,因为它们不是摇摆类,旧JButtons可以。

JButton的扩展按钮应该可以解决问题:

public class button extends JButton {

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

public ImageIcon buttonImage;

...

此外,了解Java中的代码格式和样式。这将帮助您编写更清晰的代码并防止上述混淆。例如。您有一个名为button的类,其中包含名为button的字段和名为button的方法。使用不同的名称Button(类总是大写!),buttoncreateButton

答案 6 :(得分:0)

尝试在类按钮中添加getButton以返回添加按钮

public JButton getJButton(){
 retun this.button
}

然后在菜单面板上添加使用此功能

this.add(playKnop.getJButton());

答案 7 :(得分:0)

您的代码没有任何问题。我能看到的唯一问题是,您的班级button会延伸Object而不是JButton。所以,只需做以下更改,我觉得它应该有效:

而不是:

public class button 

public class button extends JButton

答案 8 :(得分:0)

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

答案 9 :(得分:0)

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

this.setBackground(buttonImage);

如果没有任何功能,可以在构造函数中设置背景。与上面的例子类似。您总是创建一个新按钮:

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); 
   }
}

此代码未经过测试。