在 Java 中将面板添加到滚动窗格内的面板不起作用

问题描述 投票:0回答:1

我正在研究这个调度程序项目。我已经获得了甘特图的数据(进程、执行的时钟滴答以及剩余的突发时间)。但是,我似乎无法使 processBlock 显示在 blockObjectsPane (滚动窗格)内的 blockObjectsPanel (面板)上。

我尝试输出面板的组件,确实添加了 processBlock 面板。但无法显示。此外,每当我尝试打印该块时,它都会输出无效字段。

这是终端中的输出。 SchedulerComponents.processBlock 无效

===============================
id: ID: -1
bt: Burst: 0
Before adding block: 0
After adding block: 1
schedulerComponents.processBlock[,0,0,0x0,invalid,layout=javax.swing.GroupLayout,alignmentX=0.0,alignmentY=0.0,border=javax.swing.border.LineBorder@74d39a2a,flags=9,maximumSize=java.awt.Dimension[width=70,height=70],minimumSize=java.awt.Dimension[width=70,height=70],preferredSize=java.awt.Dimension[width=70,height=70]]
===============================
id: ID: 2
bt: Burst: 7
Before adding block: 1
After adding block: 2
schedulerComponents.processBlock[,0,0,0x0,invalid,layout=javax.swing.GroupLayout,alignmentX=0.0,alignmentY=0.0,border=javax.swing.border.LineBorder@2d1e5004,flags=9,maximumSize=java.awt.Dimension[width=70,height=70],minimumSize=java.awt.Dimension[width=70,height=70],preferredSize=java.awt.Dimension[width=70,height=70]]
===============================
id: ID: 4
bt: Burst: 7
Before adding block: 2
After adding block: 3
schedulerComponents.processBlock[,0,0,0x0,invalid,layout=javax.swing.GroupLayout,alignmentX=0.0,alignmentY=0.0,border=javax.swing.border.LineBorder@6706ee81,flags=9,maximumSize=java.awt.Dimension[width=70,height=70],minimumSize=java.awt.Dimension[width=70,height=70],preferredSize=java.awt.Dimension[width=70,height=70]]
===============================
id: ID: 10
bt: Burst: 7
Before adding block: 3
After adding block: 4
schedulerComponents.processBlock[,0,0,0x0,invalid,layout=javax.swing.GroupLayout,alignmentX=0.0,alignmentY=0.0,border=javax.swing.border.LineBorder@612b0999,flags=9,maximumSize=java.awt.Dimension[width=70,height=70],minimumSize=java.awt.Dimension[width=70,height=70],preferredSize=java.awt.Dimension[width=70,height=70]]
===============================
id: ID: 8
bt: Burst: 2
Before adding block: 4
After adding block: 5
schedulerComponents.processBlock[,0,0,0x0,invalid,layout=javax.swing.GroupLayout,alignmentX=0.0,alignmentY=0.0,border=javax.swing.border.LineBorder@3c2dd6fd,flags=9,maximumSize=java.awt.Dimension[width=70,height=70],minimumSize=java.awt.Dimension[width=70,height=70],preferredSize=java.awt.Dimension[width=70,height=70]]
===============================
id: ID: 9
bt: Burst: 5
Before adding block: 5
After adding block: 6
schedulerComponents.processBlock[,0,0,0x0,invalid,layout=javax.swing.GroupLayout,alignmentX=0.0,alignmentY=0.0,border=javax.swing.border.LineBorder@3944661a,flags=9,maximumSize=java.awt.Dimension[width=70,height=70],minimumSize=java.awt.Dimension[width=70,height=70],preferredSize=java.awt.Dimension[width=70,height=70]]
===============================
id: ID: 7
bt: Burst: 5
Before adding block: 6
After adding block: 7
schedulerComponents.processBlock[,0,0,0x0,invalid,layout=javax.swing.GroupLayout,alignmentX=0.0,alignmentY=0.0,border=javax.swing.border.LineBorder@40ac6329,flags=9,maximumSize=java.awt.Dimension[width=70,height=70],minimumSize=java.awt.Dimension[width=70,height=70],preferredSize=java.awt.Dimension[width=70,height=70]]
===============================
id: ID: 1
bt: Burst: 6
Before adding block: 7
After adding block: 8
schedulerComponents.processBlock[,0,0,0x0,invalid,layout=javax.swing.GroupLayout,alignmentX=0.0,alignmentY=0.0,border=javax.swing.border.LineBorder@65a86d03,flags=9,maximumSize=java.awt.Dimension[width=70,height=70],minimumSize=java.awt.Dimension[width=70,height=70],preferredSize=java.awt.Dimension[width=70,height=70]]
===============================
id: ID: 3
bt: Burst: 7
Before adding block: 8
After adding block: 9
schedulerComponents.processBlock[,0,0,0x0,invalid,layout=javax.swing.GroupLayout,alignmentX=0.0,alignmentY=0.0,border=javax.swing.border.LineBorder@3cc5327d,flags=9,maximumSize=java.awt.Dimension[width=70,height=70],minimumSize=java.awt.Dimension[width=70,height=70],preferredSize=java.awt.Dimension[width=70,height=70]]
===============================
id: ID: 6
bt: Burst: 7
Before adding block: 9
After adding block: 10
schedulerComponents.processBlock[,0,0,0x0,invalid,layout=javax.swing.GroupLayout,alignmentX=0.0,alignmentY=0.0,border=javax.swing.border.LineBorder@5c5ff31c,flags=9,maximumSize=java.awt.Dimension[width=70,height=70],minimumSize=java.awt.Dimension[width=70,height=70],preferredSize=java.awt.Dimension[width=70,height=70]]
===============================
id: ID: 5
bt: Burst: 6
Before adding block: 10
After adding block: 11
schedulerComponents.processBlock[,0,0,0x0,invalid,layout=javax.swing.GroupLayout,alignmentX=0.0,alignmentY=0.0,border=javax.swing.border.LineBorder@68321c2c,flags=9,maximumSize=java.awt.Dimension[width=70,height=70],minimumSize=java.awt.Dimension[width=70,height=70],preferredSize=java.awt.Dimension[width=70,height=70]]
===============================
id: ID: 2
bt: Burst: 1
Before adding block: 11
After adding block: 12
schedulerComponents.processBlock[,0,0,0x0,invalid,layout=javax.swing.GroupLayout,alignmentX=0.0,alignmentY=0.0,border=javax.swing.border.LineBorder@4049a239,flags=9,maximumSize=java.awt.Dimension[width=70,height=70],minimumSize=java.awt.Dimension[width=70,height=70],preferredSize=java.awt.Dimension[width=70,height=70]]
===============================
id: ID: 4
bt: Burst: 0
Before adding block: 12
After adding block: 13
schedulerComponents.processBlock[,0,0,0x0,invalid,layout=javax.swing.GroupLayout,alignmentX=0.0,alignmentY=0.0,border=javax.swing.border.LineBorder@2c6438ce,flags=9,maximumSize=java.awt.Dimension[width=70,height=70],minimumSize=java.awt.Dimension[width=70,height=70],preferredSize=java.awt.Dimension[width=70,height=70]]
===============================
id: ID: 10
bt: Burst: 1
Before adding block: 13
After adding block: 14
schedulerComponents.processBlock[,0,0,0x0,invalid,layout=javax.swing.GroupLayout,alignmentX=0.0,alignmentY=0.0,border=javax.swing.border.LineBorder@4874936c,flags=9,maximumSize=java.awt.Dimension[width=70,height=70],minimumSize=java.awt.Dimension[width=70,height=70],preferredSize=java.awt.Dimension[width=70,height=70]]
===============================
id: ID: 3
bt: Burst: 1
Before adding block: 14
After adding block: 15
schedulerComponents.processBlock[,0,0,0x0,invalid,layout=javax.swing.GroupLayout,alignmentX=0.0,alignmentY=0.0,border=javax.swing.border.LineBorder@67da96cb,flags=9,maximumSize=java.awt.Dimension[width=70,height=70],minimumSize=java.awt.Dimension[width=70,height=70],preferredSize=java.awt.Dimension[width=70,height=70]]
===============================

这是 ganttChart.java 的代码

/*
 * Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
 * Click nbfs://nbhost/SystemFileSystem/Templates/GUIForms/JPanel.java to edit this template
 */
package schedulerComponents;

import com.mycompany.processFinal.MFQS.GanttChartEntry;
import java.awt.Dimension;
import java.util.List;
import javax.swing.BoxLayout;

/**
 *
 * @author LENOVO
 */
public class ganttChart extends javax.swing.JPanel {

    /**
     * Creates new form ganttChart
     */
    public ganttChart() {
        initComponents();
//      processBlock block = new processBlock();
//      blockObjectsPanel.add(block);
//      System.out.println("Hello hays");
////        blockObjectsPanel.revalidate();
////        blockObjectsPanel.repaint();
//          
    }

    /**
     * This method is called from within the constructor to initialize the
     * form. WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
        // <editor-fold defaultstate="collapsed" desc="Generated Code">                          
        private void initComponents() {

                ganttChartLabel = new javax.swing.JLabel();
                initPanel = new javax.swing.JPanel();
                blockObjectsPane = new javax.swing.JScrollPane();
                blockObjectsPanel = new javax.swing.JPanel();

                ganttChartLabel.setFont(new java.awt.Font("Segoe UI", 1, 24)); // NOI18N
                ganttChartLabel.setText("Gantt Chart");

                blockObjectsPane.setToolTipText("");
                blockObjectsPane.setVerticalScrollBarPolicy(javax.swing.ScrollPaneConstants.VERTICAL_SCROLLBAR_NEVER);
                blockObjectsPane.setAutoscrolls(true);
                blockObjectsPane.setDoubleBuffered(true);
                blockObjectsPane.setMinimumSize(new java.awt.Dimension(1000, 3000));
                blockObjectsPane.setPreferredSize(new java.awt.Dimension(1000, 300));
                blockObjectsPane.setViewportView(null);

                blockObjectsPanel.setAutoscrolls(true);
                blockObjectsPanel.setDoubleBuffered(false);
                blockObjectsPanel.setMinimumSize(new java.awt.Dimension(1500, 300));
                blockObjectsPanel.setLayout(new javax.swing.BoxLayout(blockObjectsPanel, javax.swing.BoxLayout.LINE_AXIS));
                blockObjectsPane.setViewportView(blockObjectsPanel);

                javax.swing.GroupLayout initPanelLayout = new javax.swing.GroupLayout(initPanel);
                initPanel.setLayout(initPanelLayout);
                initPanelLayout.setHorizontalGroup(
                        initPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addComponent(blockObjectsPane, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                );
                initPanelLayout.setVerticalGroup(
                        initPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addComponent(blockObjectsPane, javax.swing.GroupLayout.PREFERRED_SIZE, 300, javax.swing.GroupLayout.PREFERRED_SIZE)
                );

                blockObjectsPane.getAccessibleContext().setAccessibleParent(this);

                javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
                this.setLayout(layout);
                layout.setHorizontalGroup(
                        layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addGroup(layout.createSequentialGroup()
                                .addContainerGap()
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                        .addGroup(layout.createSequentialGroup()
                                                .addComponent(ganttChartLabel)
                                                .addGap(0, 0, Short.MAX_VALUE))
                                        .addComponent(initPanel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                                .addContainerGap())
                );
                layout.setVerticalGroup(
                        layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addGroup(layout.createSequentialGroup()
                                .addContainerGap()
                                .addComponent(ganttChartLabel)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(initPanel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                .addContainerGap())
                );
        }// </editor-fold>                        
    public void displayGanttChart (List<GanttChartEntry> ganttChartEntries){
        blockObjectsPanel.removeAll();
        for(GanttChartEntry entry: ganttChartEntries){
            processBlock block = new processBlock();
            block.setPreferredSize(new Dimension(70, 70)); // Set the preferred size as needed

            blockObjectsPanel.setLayout(new BoxLayout(blockObjectsPanel, BoxLayout.X_AXIS));

            block.setProcessID(entry.getProcessId());
            block.setBurstTime(entry.getEndTime() - entry.getStartTime());
            System.out.println("===============================");
            System.out.println("id: "+block.getProcessId());
            System.out.println("bt: "+block.getBurstTimeLabel());
            
            System.out.println("Before adding block: " + blockObjectsPanel.getComponentCount());
            blockObjectsPanel.add(block);
            System.out.println("After adding block: " + blockObjectsPanel.getComponentCount());
            
            blockObjectsPanel.setVisible(true);
            blockObjectsPanel.revalidate();
            blockObjectsPanel.repaint();
            System.out.println(blockObjectsPanel.getComponent(blockObjectsPanel.getComponentCount()-1));
        }
        blockObjectsPanel.revalidate();
        blockObjectsPanel.repaint();
        blockObjectsPane.revalidate();
        blockObjectsPane.repaint();
    }

        // Variables declaration - do not modify                     
        private javax.swing.JScrollPane blockObjectsPane;
        public javax.swing.JPanel blockObjectsPanel;
        private javax.swing.JLabel ganttChartLabel;
        private javax.swing.JPanel initPanel;
        // End of variables declaration                   
}

java swing panel scrollpane
1个回答
0
投票

您可以在每次迭代的循环内设置布局管理器。在添加任何组件之前应设置一次,以确保它不会被重置。将 BoxLayout 配置移到循环之外:

blockObjectsPanel.setLayout(new BoxLayout(blockObjectsPanel, BoxLayout.X_AXIS));

每次添加后都调用 revalidate 和 repaint,这很好。但是,您可能想要验证在添加所有组件后所有这些调用是否都发生在循环之外。这可确保添加所有组件后最终布局正确更新:

for(GanttChartEntry entry: ganttChartEntries){
}

blockObjectsPanel.setVisible(true);
blockObjectsPanel.revalidate();
blockObjectsPanel.repaint();
blockObjectsPane.revalidate();
blockObjectsPane.repaint();

尝试暂时用 FlowLayout 等更简单的布局管理器替换 BoxLayout,看看它是否会改变行为。这可以帮助确定问题是否与布局相关。

public void displayGanttChart(List<GanttChartEntry> ganttChartEntries) {
    blockObjectsPanel.removeAll();
    blockObjectsPanel.setLayout(new BoxLayout(blockObjectsPanel, BoxLayout.X_AXIS)); // Move outside the loop

    for (GanttChartEntry entry : ganttChartEntries) {
        processBlock block = new processBlock();
        block.setPreferredSize(new Dimension(70, 70)); // Set the preferred size as needed
        block.setProcessID(entry.getProcessId());
        block.setBurstTime(entry.getEndTime() - entry.getStartTime());

        System.out.println("===============================");
        System.out.println("id: " + block.getProcessId());
        System.out.println("bt: " + block.getBurstTimeLabel());

        System.out.println("Before adding block: " + blockObjectsPanel.getComponentCount());
        blockObjectsPanel.add(block);
        System.out.println("After adding block: " + blockObjectsPanel.getComponentCount());
    }

    blockObjectsPanel.setVisible(true);
    blockObjectsPanel.revalidate();
    blockObjectsPanel.repaint();
    blockObjectsPane.revalidate();
    blockObjectsPane.repaint();
}
© www.soinside.com 2019 - 2024. All rights reserved.