簡體   English   中英

如何從另一個 class 添加到 JPanel

[英]How to add to a JPanel from another class

在過去的幾個小時里,我一直在試圖弄清楚如何在我的GUI class 中使用名為 class 的面板algorithms 我知道之前有人問過這個問題,但我似乎無法弄清楚如何將它合並到我的代碼中,因此我將不勝感激任何形式的指導。

具體來說,在我的算法 class 中,我正在測試嘗試通過創建我的 GUI 實例 class 來更改名為 title 的 JLabel,然后嘗試使用title.setText() ,但是它返回 Null 指針異常。

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 的新局部變量,而不是定義全局變量。 所以全局變量仍然是 null。要解決這個問題,只需將其更改為:

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

現在它將定義全局變量。 這現在不應該給出 null 指針異常。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM