How to use code from one class to another? (Java)

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!)

+6
source share
10 answers

You may notice the difference between the line:

 playKnop.addActionListener(this); 

and the other in a later code:

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

I believe that you want to add MenuPanel as an action listener, but you are not passing the link to it in later code. You should have: playKnop = new button("/buttons/PLAY.png", 350, this);

And then set this link in your Button class, for example:

 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); } 
+1
source

First, all class names should start with a capital letter as a convention, this does not affect compilation, but it is good practice.

Now, for your real problem, the first thing that stands out for me is that your Button class does not extend anything. It seems to me that this is not the case on the screen. I suggest making Button extend JButton :

 public class Button extends JButton { // Implementation Code } 

This is similar to how your MenuPanel extends JPanel . JPanel cannot display the custom Button you wrote. By expanding on JButton , he will be able to display it as he knows how to interact with JButton

+2
source

I think you'd better let your Button class extend JButton. That way, you can simply add your own button class to the menupanel menu.

 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
source

The difference is that you no longer add JButtons to JPanel.

For this code to work, your "button" class must extend JPanel.

Another way to extract common code is to create a “button creator method” instead of a class, for example:

 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) { } } 
+1
source

In an existing MenuPanel when you do

 this.add(playKnop); 

you add JButton to your JPanel

In the new MenuPanel you add a button to your JPanel .

You need to add JButton to JPanel .

Also select button instead of button for class names.

The solution is to rewrite this code so that you add JButton and not button to your panel. Other code issues may also occur.

0
source

There is a difference in what you add . In the new code, you add instances of your button class to JPanel. In the old code, you added JButtons . buttons cannot be displayed since they are not swing classes, old JButtons can.

The renew button from JButton should do the trick:

 public class button extends JButton { // public JButton button; // this line is depreceted now public ImageIcon buttonImage; ... 

Also, learn about formatting and code style in Java . This will help you write clearer code and prevent confusion as described above. For instance. you have a class called button with a field named button and a method called button . Use different names like button (classes are always uppercase!), createButton and createButton .

0
source

Try adding getButton to the class button to return the button to add

 public JButton getJButton(){ retun this.button } 

then add this when adding to the menu bar

 this.add(playKnop.getJButton()); 
0
source

There is nothing wrong with the code. The only problem I see is that your button class extends Object instead of JButton . So just make the following change and I feel that it should work:

Instead:

 public class button 

to write

 public class button extends JButton 
0
source

Perhaps you want your button class to inherit from JButton instead of holding the button field. Thus, you can add a button to the panel.

0
source

I am not very familiar with the JButton class. You need to set buttonImage as background. Perhaps there is such a function as:

 this.setBackground(buttonImage); 

If liek is not available, you can set the background in the constructor. As in the examples above. You have always created a new button with:

 JButton b = new JButton(buttonImage); 

You can give the same parameters to the super () method in your own constructor. The result will look like this:

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

This code has not been verified.

0
source

Source: https://habr.com/ru/post/986719/


All Articles