I make a tank game and avoid redundancy. I do classes for expansion. My MenuPanel looks like this: (I just wrote the code that matters for the question) (knop = Dutch for the 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(); } }
because the code so that the buttons are exactly the same (with the exception of backgroundPath and y-coordinates), I made a class button:
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); } }
So my Panel COULD menu looks like this:
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(); } }
I don’t know why, but when I do this, the buttons will not appear, although the code from the button class is correct (bc, when I use the code in the Panel menu itself, it works)
I don’t know how to fix this problem, and I have many problems like this in my JavaProject, but if someone can explain to me how to fix it, I could get rid of all the redundancy in my project.
Thanks in advance, Lola
Thanks everyone! The combination of your answers helped me display the buttons, but for some reason, the images do not load with them. now my code is as follows:
public class MenuPanel extends JPanel implements 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); } }
}
(all buttons work!)