简体   繁体   中英

Adding JPanel to another JPanel in a different class

I'm trying to add a JPanel to another JPanel from another class. The program does not longer throw an error and all methods have been run, but the new panel just has a black screen. A basic version of the program looks as follows:

package ninjadragon;

import java.awt.*;
import javax.swing.*;
import java.awt.event.*;

public class NinjaDragon extends JFrame implements ActionListener{

public JPanel panelMain;
public JPanel panelTurnBase;

public static void main(String[] args) {
   NinjaDragon();
}

public static void NinjaDragon() {
    NinjaDragon frame; 
    frame = new NinjaDragon();
    frame.CreateMenuScreen();
    JFrame.setDefaultLookAndFeelDecorated(true);
    frame.setSize(750, 750);
    frame.show(); 
    frame.setResizable(false);
    frame.pack();
} 

private void CreateMenuScreen() {
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    Container window = getContentPane();
    panelMain =new JPanel();
    panelMain.setPreferredSize(new Dimension(750,750));
    panelMain.setBackground(Color.BLACK);
    panelMain.setLayout (new FlowLayout());
    window.add(panelMain);
    PanelTop();
    PanelButtons();
    PanelIcon();
}

@Override
public void actionPerformed(ActionEvent event) {   
    Object eventSource = event.getSource();

    if (eventSource == buttonStart) {
        panelMain.removeAll();
        TurnBase TB = new TurnBase();
        TB.CreateTurnBase();
    }
}

The other class looks something like this:

public void CreateTurnBase() {
    panelMain=new JPanel();
    panelTurnBase =new JPanel();
    setLayout(new FlowLayout());
    setPreferredSize(new Dimension(750,750));
    setBackground(Color.BLUE);
    panelTurnBase.setLayout (new FlowLayout()); 
    panelMain.add(panelTurnBase);
    System.out.println("1");
    PanelTurnBaseTop();
    PanelGameScreen();
    PanelTurnBaseBottom();
    repaint();
    revalidate();
    buttonAttack = new JButton("Attack");
    buttonAttack.addActionListener(this);
    panelTurnBase.add(buttonAttack);
    System.out.println("2");   
}

The reason the panel has "just a black screen" is because you dont add anything to it, and you tell it to have a black screen.

ie

panel.setBackground(Color.BLACK);

You never actually do anything to that first panel inside of any of those methods, which I can assume based on your representation of your second "class" (it's a method). Hence why it stays black.

You say:

panelMain=new JPanel();
panelTurnBase =new JPanel();

You're creating new JPanel s every time and just call them panelMain and they just sit inside of that method, never leaving. You either need to return a JPanel or give it a JPanel as an argument.

The program is doing exactly what you tell it to do.

Also, do not compare Objects like this:

eventSource == buttonStart

You should use:

eventSource.equals(buttonStart);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM