【发布时间】:2015-07-17 17:56:33
【问题描述】:
我正在制作坦克游戏,为了避免冗余,我正在制作扩展课程。 我的 MenuPanel 看起来像这个 atm (我只写了对问题很重要的代码) (knop = 荷兰语表示按钮)
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();
}
}
因为制作按钮的代码完全一样(除了背景路径和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();
}
}
我不知道为什么,但是当我这样做时,按钮不会出现,尽管按钮类中的代码是正确的(bc 当我在 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);
}
}
}
(所有按钮都可以工作!)
【问题讨论】:
标签: java redundancy extending