简体   繁体   中英

How to pass JPanel instance to another class?

I'm struggling to display the JPanel instances I've created in another class.

I can draw from another class, but I want to have JButton s which are on a JPanel in my BorderLayout.NORTH ; I only wanted to draw to my mainGame which is BorderLayout.CENTER . I'm unsure how to achieve this though.

import javax.swing.*;
import javax.swing.event.*;
import java.awt.event.*;
import java.awt.*;
import java.awt.image.*;
      
public class RPGClicker {
         
    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                mainGameScene();
            }
        });
    }
      
    private static void mainGameScene() {
        JFrame window;
        mainGamePanel mainGameInstance;
        statusPanel statusInstance;
        playerInfoPanel playerInfoInstance;
        navBarDisplayPanel navBarDisplayInstance;
            
        window = new JFrame("RPGClicker: An Unknown Quest!");
        window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        window.setLayout(new BorderLayout());
            
        mainGameInstance = new mainGamePanel();
        statusInstance = new statusPanel();
        playerInfoInstance = new playerInfoPanel();
        navBarDisplayInstance = new navBarDisplayPanel();
            
        window.add(mainGameInstance, BorderLayout.CENTER);
        window.add(statusInstance, BorderLayout.SOUTH);
        window.add(playerInfoInstance, BorderLayout.EAST);
        window.add(navBarDisplayInstance, BorderLayout.NORTH);
            
        window.pack();
        window.setLocationRelativeTo(null);
        window.setVisible(true);
    }
}

I'm able to draw mainGamePanel , but now I can only draw to my other panels; I wanted them to contain textfields and JButtons though.

import javax.swing.*;
import javax.swing.event.*;
import java.awt.event.*;
import java.awt.*;
import java.awt.image.*;
      
class navBarDisplayPanel extends JPanel {   
         
    JButton yourStory;
         
    public Dimension getPreferredSize() {
        return new Dimension(1280, 256);
    }
      
    private void navBarDisplayPanel() {
        yourStory = new JButton("Your Story");    
        add(yourStory);
    }
}

The code compiles, but nothing appears when it's run. The north bar is completely empty. Can anyone gives any ideas?

I am assuming this:

private void navBarDisplayPanel()

is what you think the constructor of your class navBarDisplayPanel really is.

As a constructor, you should not let this be private if you want to make objects with it from external classes, you should remove the void mark as this is not a regular method, and (optional but not optional) you should name all your classes (and their constructors) with the first letter as capital.

In order to make your code work, I guess you just need to rename this:

private void navBarDisplayPanel(){
    yourStory = new JButton("Your Story");
    add(yourStory);
}

to this:

public navBarDisplayPanel(){
    yourStory = new JButton("Your Story");
    add(yourStory);
}

But as I pointed it, try to follow the Java Naming Conventions (and others conventions too) to avoid future mistakes.

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