Maya
Maya

Reputation: 3

JPanel changes not showing on Jframe

Hello I'm trying to create a java game using using JPanel and JFrame. However I can't get the JPanel to show changes I make to it. I'm wondering if I might be setting up the JFrame wrong or improperly adding to it or something like that.

Here's my window class:

import javax.swing.*;
import java.awt.*
public class Window extends Canvas {

public static final int ScreenWidth = 1000;
public static final int ScreenHeight = 700;
public static JFrame frame;

Window(int ScreenWidth, int ScreenHeight, JFrame frame) {
    this.frame = frame;
    displaywindow();
    frame.add(this);
}

public void paint(Graphics g) {
    super.paint(g);
}

public void displaywindow() {
    this.frame.setPreferredSize(new Dimension(ScreenWidth,ScreenHeight));
    this.frame.setMaximumSize(new Dimension(ScreenWidth,ScreenHeight));
    this.frame.setMinimumSize(new Dimension(ScreenWidth,ScreenHeight));
    this.frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    this.frame.setResizable(false);
    this.frame.setBackground(Color.pink);
    this.frame.setLocationRelativeTo(null);
}

public JFrame getFrame() {
    return this.frame;
}

}

Here's the Menu class I'm working on:

public class Menu extends JPanel implements ActionListener, KeyListener {
private Window win = new Window(Window.ScreenWidth, Window.ScreenHeight, Window.frame);
private int width = Window.frame.getContentPane().getWidth();
private int height = Window.frame.getContentPane().getHeight();
BufferedImage bg;
  
Menu(JPanel panel) {
    this.panel = panel;
}
@Override
protected void paintComponent(Graphics g) {
    super.paintComponent(g);

    g.fillRect(110, 110, 20, 20);
}


public void init() throws IOException {
    this.panel.setBackground(Color.cyan);
    this.panel.setPreferredSize(new Dimension(width, height));
    this.panel.setMaximumSize(new Dimension(width, height));
    this.panel.setMinimumSize(new Dimension(width, height));
    JLabel start = new JLabel("Start");
    start.setFont(new Font("Verdana", Font.PLAIN, 50));
    start.setLocation(500, 400);
    this.panel.add(new JLabel("Exit"));
    this.panel.add(start);
    this.panel.revalidate();
    this.panel.repaint();
    this.panel.setVisible(true);
}

enter code here
public static void main(String[] args) throws IOException {
    
    Window win = new Window(Window.ScreenWidth, Window.HEIGHT, new JFrame());
    Menu mene = new Menu();
    mene.init();
    win.getFrame().getContentPane().add(mene);
    win.getFrame().setVisible(true);

}

I tried adding a rectangle to the paintComponent just so I was sure that the panel was actually being added to the frame and it shows that it is, but none of the changes I make in init() ever actually show. Any help would be much appreciated.

`

Upvotes: 0

Views: 228

Answers (1)

Code-Apprentice
Code-Apprentice

Reputation: 83527

The problem is that you do both class Menu extends JPanel and declare JPanel panel inside of the Menu class. This duplication is unnecessary and causes the problem because you add items to the panel field, but you never do anything with that panel.

On the other hand, you declare Menu mene and add it to the content pane, but you never add anything to the Menu object itself. Don't mix and match like this. Pick a field member or inheritance, but not both.

Upvotes: 1

Related Questions