Java 根面板内未显示所有单个面板
我想向jpanel添加多个jpanel。因此我向jscrollpane添加了一个根面板。然后将所有单独的jpanel添加到此根面板。我根据需要制定了jscrollpane的滚动策略。即水平滚动条按需,垂直滚动条按需。 但问题是所有单独的面板都没有显示在根面板中 代码: 还有一件事是,当我滚动滚动滚动条时,面板会从jscrollpane区域消失。 我无法看到所有单独的面板, 请推荐我 编辑:来自double post的代码段:Java 根面板内未显示所有单个面板,java,swing,jpanel,layout-manager,flowlayout,Java,Swing,Jpanel,Layout Manager,Flowlayout,我想向jpanel添加多个jpanel。因此我向jscrollpane添加了一个根面板。然后将所有单独的jpanel添加到此根面板。我根据需要制定了jscrollpane的滚动策略。即水平滚动条按需,垂直滚动条按需。 但问题是所有单独的面板都没有显示在根面板中 代码: 还有一件事是,当我滚动滚动滚动条时,面板会从jscrollpane区域消失。 我无法看到所有单独的面板, 请推荐我 编辑:来自double post的代码段: MosaicFilesStatusBean mosaicFilesSt
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();
}
MosaicFileStatusBean MosaicFileStatusBean=新的MosaicFileStatusBean();
DefaultTableModel tableModel=null;
tableModel=MosaicFileStatusBean.GetFileStatusBetweenDates(startDate,endDate);
if(tableModel!=null){
rootPanel.removeAll();
rootPanel.setLayout(新的BoxLayout(rootPanel,BoxLayout.PAGE_轴));
对于(int tempRow=0;tempRow
不要对根面板使用FlowLayout。取而代之考虑使用:
编辑1以下是一个基于您发布的最新代码的示例:
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.util.Random;
import javax.swing.*;
@SuppressWarnings("serial")
public class PanelsEg extends JPanel {
private static final int MAX_ROW_COUNT = 100;
private Random random = new Random();
private JPanel rootPanel = new JPanel();
public PanelsEg() {
rootPanel.setLayout(new BoxLayout(rootPanel, BoxLayout.PAGE_AXIS));
JScrollPane scrollPane = new JScrollPane(rootPanel);
scrollPane.setPreferredSize(new Dimension(400, 400)); // sorry kleopatra
add(scrollPane);
add(new JButton(new AbstractAction("Foo") {
@Override
public void actionPerformed(ActionEvent arg0) {
foo();
}
}));
}
public void foo() {
rootPanel.removeAll();
// rootPanel.setLayout(new BoxLayout(rootPanel, BoxLayout.PAGE_AXIS)); // only need to set layout once
int rowCount = random.nextInt(MAX_ROW_COUNT);
for (int tempRow = 0; tempRow < rowCount ; tempRow++) {
int fileIdTemp = tempRow;
String data = "Data " + (tempRow + 1);
MosaicPanel tempPanel =
new MosaicPanel(fileIdTemp, data);
rootPanel.add(tempPanel);
}
rootPanel.revalidate();
rootPanel.repaint(); // don't forget to repaint if removing
}
private class MosaicPanel extends JPanel {
public MosaicPanel(int fileIdTemp, String data) {
add(new JLabel(data));
}
}
private static void createAndShowGui() {
PanelsEg mainPanel = new PanelsEg();
JFrame frame = new JFrame("PanelsEg");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add(mainPanel);
frame.pack();
frame.setLocationByPlatform(true);
frame.setVisible(true);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGui();
}
});
}
}
导入java.awt.Dimension;
导入java.awt.event.ActionEvent;
导入java.util.Random;
导入javax.swing.*;
@抑制警告(“串行”)
公共类PanelsEg扩展了JPanel{
私有静态最终整数最大行数=100;
私有随机=新随机();
private JPanel rootPanel=new JPanel();
公共小组委员会{
rootPanel.setLayout(新的BoxLayout(rootPanel,BoxLayout.PAGE_轴));
JScrollPane scrollPane=新的JScrollPane(rootPanel);
scrollPane.setPreferredSize(新维度(400400));//抱歉,kleopatra
添加(滚动窗格);
添加(新JButton(新抽象操作(“Foo”){
@凌驾
已执行的公共无效操作(操作事件arg0){
foo();
}
}));
}
公共图书馆{
rootPanel.removeAll();
//rootPanel.setLayout(新的BoxLayout(rootPanel,BoxLayout.PAGE_AXIS));//只需设置一次布局
int rowCount=random.nextInt(最大行数);
对于(int-tempRow=0;tempRow
这个SSCCE很有效,因为它很容易显示如何删除JPanel并将其添加到JScrollPane持有的另一个JPanel中。如果您仍然有问题,您应该修改此SSCE,使其显示您的问题。无法查看
JPanel
的主要原因是您正在使用FlowLayout
作为rootPanel
的LayoutManager
。由于添加到此rootPanel
中的JPanel
中没有任何内容,因此它将分别将宽度和高度的大小设置为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));
}
}
import java.awt.*;
导入javax.swing.*;
公共类面板添加
{
私有void createAndDisplayGUI()
{
JFrame=新JFrame(“面板添加示例”);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel contentPane=新的JPanel();
setLayout(新的GridLayout(0,1));
JScrollPane scroller=新的JScrollPane();
CustomPanel=新的CustomPanel(1);
contentPane.add(面板);
scroller.setViewportView(contentPane);
frame.getContentPane().add(滚动条,BorderLayout.CENTER);
frame.pack();
frame.setLocationByPlatform(真);
frame.setVisible(true);
对于(int i=2;i<20;i++)
{
CustomPanel pane=新的CustomPanel(i);
contentPane.add(窗格);
contentPane.revalidate();
contentPane.repaint();
}
}
公共静态void main(字符串…参数)
{
SwingUtilities.invokeLater(新的Runnable()
{
公开募捐
{
新建PanelAddition().createAndDisplayGUI();
}
});
}
}
类CustomPanel扩展了JPanel
{
公共自定义面板(int num)
{
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.util.Random;
import javax.swing.*;
@SuppressWarnings("serial")
public class PanelsEg extends JPanel {
private static final int MAX_ROW_COUNT = 100;
private Random random = new Random();
private JPanel rootPanel = new JPanel();
public PanelsEg() {
rootPanel.setLayout(new BoxLayout(rootPanel, BoxLayout.PAGE_AXIS));
JScrollPane scrollPane = new JScrollPane(rootPanel);
scrollPane.setPreferredSize(new Dimension(400, 400)); // sorry kleopatra
add(scrollPane);
add(new JButton(new AbstractAction("Foo") {
@Override
public void actionPerformed(ActionEvent arg0) {
foo();
}
}));
}
public void foo() {
rootPanel.removeAll();
// rootPanel.setLayout(new BoxLayout(rootPanel, BoxLayout.PAGE_AXIS)); // only need to set layout once
int rowCount = random.nextInt(MAX_ROW_COUNT);
for (int tempRow = 0; tempRow < rowCount ; tempRow++) {
int fileIdTemp = tempRow;
String data = "Data " + (tempRow + 1);
MosaicPanel tempPanel =
new MosaicPanel(fileIdTemp, data);
rootPanel.add(tempPanel);
}
rootPanel.revalidate();
rootPanel.repaint(); // don't forget to repaint if removing
}
private class MosaicPanel extends JPanel {
public MosaicPanel(int fileIdTemp, String data) {
add(new JLabel(data));
}
}
private static void createAndShowGui() {
PanelsEg mainPanel = new PanelsEg();
JFrame frame = new JFrame("PanelsEg");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add(mainPanel);
frame.pack();
frame.setLocationByPlatform(true);
frame.setVisible(true);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGui();
}
});
}
}
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));
}
}