Lol*_*ola 6 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();
}
}
Run Code Online (Sandbox Code Playgroud)
因为制作按钮的代码完全相同(除了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);
}
}
Run Code Online (Sandbox Code Playgroud)
这样,我的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();
}
}
Run Code Online (Sandbox Code Playgroud)
我不知道为什么,但是当我这样做时,按钮将不会出现,虽然按钮类的代码是正确的(当我使用menuPanel本身的代码时,它工作)
我不知道如何解决这个问题,我在整个JavaProject中有很多这样的问题,但如果有人能解释我如何解决这个问题,我可以摆脱项目中的所有冗余.
谢谢,萝拉
非常感谢你们!你的答案的组合帮助我让按钮出现,但由于某种原因,图像不会加载它们.我的代码现在看起来像:
公共类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);
}
}
Run Code Online (Sandbox Code Playgroud)
}
(所有按钮都能正常工作!)
你能观察到这两条线之间的区别吗:
playKnop.addActionListener(this);
Run Code Online (Sandbox Code Playgroud)
以及后面代码中的一个:
playKnop = new button("/buttons/PLAY.png", 350);
Run Code Online (Sandbox Code Playgroud)
我相信您想添加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);
}
Run Code Online (Sandbox Code Playgroud)