如何从另一个 class 添加到 JPanel

How to add to a JPanel from another class

在过去的几个小时里,我一直在努力弄清楚如何在我的 GUI class 中使用来自名为 algorithms 的 class 的面板。我知道之前有人问过这个问题,但我似乎无法弄清楚如何将它合并到我的代码中,所以我将不胜感激任何形式的指导。

具体来说,在我的算法 class 中,我正在测试通过创建我的 GUI class 实例然后尝试使用 title.setText() 来尝试更改 JLabel 命名标题,但是它returns 空指针异常。

title JLabel 位于我名为 topPanel

的 JPanel 上

我知道我做错了什么,过去几个小时我一直在努力解决这个问题,但似乎没有取得任何进展。就像我说的,任何指导将不胜感激。

GUI.java

import javax.swing.*;
import java.awt.*;
import java.util.Date;

public class GUI extends JFrame {
    public JFrame myFrame;
    public JPanel firstFitDisplay,topPanel;
    public JLabel title;
    public Timer timer;
    public int count=0;


    public GUI(){



    }


    public void initGUI(){


        JFrame myFrame = new JFrame();
        myFrame.setTitle("CIS 452 Dynamic Memory Allocation Project");
        myFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        myFrame.setResizable(true);
        myFrame.setLayout(new BorderLayout(6,6));
        myFrame.setSize(1000,700);
        myFrame.setVisible(true);
        //myFrame.getContentPane().setBackground(Color.white);

        //setup panels
        JPanel infoPanel = new JPanel();
        JPanel centerPanel = new JPanel();
        JPanel topPanel = new JPanel();
        JPanel bottom = new JPanel();
        infoPanel.setBackground(Color.cyan);
        centerPanel.setBackground(Color.red);
        topPanel.setBackground(Color.yellow);
        bottom.setBackground(Color.pink);
        infoPanel.setPreferredSize(new Dimension(200,1000));
        centerPanel.setPreferredSize(new Dimension(500,500));
        topPanel.setPreferredSize(new Dimension(750,20));
        bottom.setPreferredSize(new Dimension(750,25));



        //setup layout for panels, so that we can add subpanels
        infoPanel.setLayout(new BoxLayout(infoPanel,BoxLayout.PAGE_AXIS));

        //add panels to main frame
        myFrame.add(infoPanel,BorderLayout.WEST);
        myFrame.add(centerPanel,BorderLayout.CENTER);
        myFrame.add(topPanel,BorderLayout.NORTH);
        myFrame.add(bottom,BorderLayout.SOUTH);


        // setup sub panels for infoPanel
        JPanel infoSubPanel = new JPanel();
        infoSubPanel.setBackground(Color.pink);
        infoSubPanel.setPreferredSize(new Dimension(50,90));
        infoPanel.add(infoSubPanel);
        //setting up text for infoSubPanel
        JLabel subPanelTitle = new JLabel("Next Process Size to be allocated");
        JLabel firstFitNextUpLabel = new JLabel("First:0");
        JLabel bestFitNextUpLabel = new JLabel("Best:0");
        JLabel worstFitNextUpLabel = new JLabel("Worst:0");
        infoSubPanel.add(subPanelTitle);
        infoSubPanel.add(firstFitNextUpLabel);
        infoSubPanel.add(bestFitNextUpLabel);
        infoSubPanel.add(worstFitNextUpLabel);
        //subClockPanel
        JPanel infoSubClockPanel = new JPanel();
        infoSubClockPanel.setBackground(Color.GRAY);
        infoSubClockPanel.setPreferredSize(new Dimension(50,90));
        infoPanel.add(infoSubClockPanel);

        //setting up text for sub clock panel
        JLabel clockText = new JLabel("Seconds passed: ");
        int ten = 10;
        JLabel clockCounter = new JLabel("0");
        infoSubClockPanel.add(clockText);
        infoSubClockPanel.add(clockCounter);


        //logic for running timer;
        timer = new Timer(1000, e -> {
            clockCounter.setText(String.valueOf(count++));
        });
        timer.start();

        //setting up sub panels for the main center panel
        centerPanel.setLayout(new FlowLayout());
        JPanel firstFitDisplay = new JPanel();
        JPanel bestFitDisplay = new JPanel();
        JPanel worstFitDisplay = new JPanel();
        firstFitDisplay.setPreferredSize(new Dimension(200,500));
        bestFitDisplay.setPreferredSize(new Dimension(200,500));
        worstFitDisplay.setPreferredSize(new Dimension(200,500));
        centerPanel.add(firstFitDisplay);
        centerPanel.add(bestFitDisplay);
        centerPanel.add(worstFitDisplay);
        //center components
        centerPanel.setAlignmentX(Component.CENTER_ALIGNMENT);
        firstFitDisplay.setAlignmentX(Component.CENTER_ALIGNMENT);


        //setup title
        JLabel title = new JLabel("Dynamic Memory Allocation Simulator");
        topPanel.add(title);



    }
}


这里是algorithms.java

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

public class algorithms {

    public static void main(String[] args) {
        GUI f = new GUI();
        f.initGUI();
        f.title.setText("HHHHHHHHHHH");
    }



}

所以您的错误是您从未在 GUI 中定义标题 class。我想你打算用这条线:

JLabel title = new JLabel("Dynamic Memory Allocation Simulator");

这实际上创建了一个名为 title 的新局部变量,而不是定义全局变量。所以全局变量仍然为空。要解决此问题,只需将其更改为喜欢:

this.title = new JLabel("Dynamic Memory Allocation Simulator");

现在就要定义全局变量了!现在这不应该给出空指针异常。