gpt4 book ai didi

java - 所有单独的面板都没有显示在根面板内

转载 作者:塔克拉玛干 更新时间:2023-11-01 22:52:01 25 4
gpt4 key购买 nike

我想向 jpanel 添加多个 jpanel。所以我向 jscrollpane 添加了一个根面板。然后将所有单独的 jpanel 添加到这个根面板。我根据需要制定了 jscrollpane 的滚动策略。即 HORIZONTAL_SCROLLBAR_​​AS_NEEDED、VERTICAL_SCROLLBAR_​​AS_NEEDED。但问题是所有单独的面板都没有显示在根面板内。

代码:

JScrollPane scPanel=new JScrollPane();

JPanel rootPanel=new JPanel();
rootPanel.setLayout(new FlowLayout());

JPanel indPanel = new JPanel();
rootPanel.add(indPanel);

JPanel indPanel2 = new JPanel();
rootPanel.add(indPanel2);

//.....like this added indPanals to rootPanel.
scPanel.setViewPortView(rootPanel);
//scPanel.setHorizontalScrollPolicy(HORIZONTAL_SCROLLBAR_AS_NEEDED);

还有一件事是,当我滚动滚动条时,面板会超出 jscrollpane 区域。我无法看到所有单独的面板,请给我建议。

编辑:来自双重帖子的代码片段:

MosaicFilesStatusBean mosaicFilesStatusBean = new MosaicFilesStatusBean();
DefaultTableModel tableModel = null;
tableModel = mosaicFilesStatusBean.getFilesStatusBetweenDates(startDate, endDate);
if (tableModel != null) {
rootPanel.removeAll();
rootPanel.setLayout(new BoxLayout(rootPanel, BoxLayout.PAGE_AXIS));
for (int tempRow = 0; tempRow < tableModel.getRowCount(); tempRow++) {

int fileIdTemp = Integer.parseInt(tableModel.getValueAt(tempRow, 0).toString());
String dateFromTemp = tableModel.getValueAt(tempRow, 3).toString();
String dateToTemp = tableModel.getValueAt(tempRow, 4).toString();
int processIdTemp = Integer.parseInt(tableModel.getValueAt(tempRow, 5).toString());
int statusIdTemp = Integer.parseInt(tableModel.getValueAt(tempRow, 6).toString());
String operatingDateTemp = tableModel.getValueAt(tempRow, 7).toString();
MosaicPanel tempPanel =
new MosaicPanel(fileIdTemp, dateFromTemp, dateToTemp, processIdTemp, statusIdTemp, operatingDateTemp);
rootPanel.add(tempPanel);
}
rootPanel.revalidate();
}

最佳答案

看不到 JPanel 的主要原因是您使用 FlowLayout 作为 LayoutManager根面板。由于添加到此 rootPanelJPanel 中没有任何内容,因此它的大小分别为 0、0,宽度和高度.虽然使用 GridLayout 这种情况不应该出现。看看随附的代码示例:

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

public class PanelAddition
{
private void createAndDisplayGUI()
{
JFrame frame = new JFrame("Panel Addition Example");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

JPanel contentPane = new JPanel();
contentPane.setLayout(new GridLayout(0, 1));
JScrollPane scroller = new JScrollPane();

CustomPanel panel = new CustomPanel(1);
contentPane.add(panel);
scroller.setViewportView(contentPane);
frame.getContentPane().add(scroller, BorderLayout.CENTER);
frame.pack();
frame.setLocationByPlatform(true);
frame.setVisible(true);

for (int i = 2; i < 20; i++)
{
CustomPanel pane = new CustomPanel(i);
contentPane.add(pane);
contentPane.revalidate();
contentPane.repaint();
}
}

public static void main(String... args)
{
SwingUtilities.invokeLater(new Runnable()
{
public void run()
{
new PanelAddition().createAndDisplayGUI();
}
});
}
}

class CustomPanel extends JPanel
{

public CustomPanel(int num)
{
JLabel label = new JLabel("" + num);
add(label);
}

@Override
public Dimension getPreferredSize()
{
return (new Dimension(200, 50));
}
}

关于java - 所有单独的面板都没有显示在根面板内,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11103427/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com