【问题标题】:JScrollPane not working correctlyJScrollPane 无法正常工作
【发布时间】:2012-10-15 10:12:38
【问题描述】:

我有一个 GUI,其中左侧包含图像,右侧是包含一些标签对象的面板。我想要它,以便如果右侧面板上的对象多于无法容纳,则会出现滚动条。

我遇到的问题是当大量对象添加到右侧面板时,整个框架会调整大小。

这里有一些截图。第一个是我希望窗口始终保持的大小,第二个图像显示了调整大小的问题。

第二张图片:

这是包含图像面板和侧栏面板的面板的代码:

/**
 * image panel - displays image and editing area
 */
ImagePanel imagePanel = null;

// Holds the labels
private LabelHolder lHolder = new LabelHolder();

/**
 * handles New Object button action
 */
public void addNewPolygon() {
  imagePanel.addNewPolygon(null);
}

public ContainerPanel(String imageFilename, JFrame frame) {
  //setup main window panel 
  addComponents(imageFilename, frame);
}

public void addComponents(String imageFilename, JFrame frame) {
  setLayout(new FlowLayout());

  // Polygon Title
  JLabel labelPanelTitle = new JLabel("<html><b>Polygons</b></html>");
  labelPanelTitle.setBorder(new EmptyBorder(0, 0, 0, 150));
  Font f = new Font("LabelPanel", Font.PLAIN, 24);     
  labelPanelTitle.setFont(f);
  labelPanelTitle.setAlignmentX(CENTER_ALIGNMENT);

  // Create and set up the image panel.
  try {
    imagePanel = new ImagePanel(imageFilename, frame, this);
  } catch (Exception e1) {
    e1.printStackTrace();
  }
  imagePanel.setOpaque(true); //content panes must be opaque

  JPanel objectsPanel = new JPanel();
  objectsPanel.setLayout(new BoxLayout(objectsPanel, BoxLayout.Y_AXIS));

  JPanel labelHolderContainer = new JPanel();
  labelHolderContainer.setLayout(new BoxLayout(labelHolderContainer, BoxLayout.Y_AXIS));

  labelHolderContainer.add(lHolder);        

  JScrollPane scroller = new JScrollPane(labelHolderContainer); 

  // add the title
  objectsPanel.add(labelPanelTitle);
  // Add a spacer
  objectsPanel.add(Box.createRigidArea(new Dimension(0,25)));
  // Add all the labels
  objectsPanel.add(scroller);

  addNewLabel("Some Object");
  addNewLabel("Some Object");
  addNewLabel("Some Object");
  addNewLabel("Some Object");
  addNewLabel("Some Object");
  addNewLabel("Some Object");
  addNewLabel("Some Object");
  addNewLabel("Some Object");
  addNewLabel("Some Object");
  addNewLabel("Some Object");
//      addNewLabel("Some Object");
//      addNewLabel("Some Object");
//      addNewLabel("Some Object");
//      addNewLabel("Some Object");

  add(imagePanel);
  add(Box.createRigidArea(new Dimension(10,0)));
  add(objectsPanel);

}   

public void addNewLabel(String labelName) {
  lHolder.add(new Label(labelName, imagePanel.getCurrentColour()));
  lHolder.add(Box.createRigidArea(new Dimension(10,10)));

  // set new colour 
  imagePanel.setCurrentColour(imagePanel.getRandomColour());
}

【问题讨论】:

  • 将布局从 FlowLayout 更改为 BorderLayout

标签: java swing jscrollpane


【解决方案1】:
  • 必须覆盖JScrollPane 的父级的最大大小,或覆盖JPanel(在JScrollPane 内),不管图片上显示什么,不知道如何尽快发布SSCCE

  • 如果您想自然滚动(将Icons 与叙述一起放置没有问题)然后

  • 使用JList

  • JTable 与一列一起使用

编辑

从方法中删除JFrame#pack()add / modify / remove JScrollPane 中的项目,否则无法在屏幕上更改Top-Level Containers 的大小

【讨论】:

    【解决方案2】:

    您尝试利用Scrollable 接口。

    public class TestScroll {
    
        public static void main(String[] args) {
            new TestScroll();
        }
    
        public TestScroll() {
            EventQueue.invokeLater(new Runnable() {
                @Override
                public void run() {
                    try {
                        UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                    } catch (ClassNotFoundException ex) {
                    } catch (InstantiationException ex) {
                    } catch (IllegalAccessException ex) {
                    } catch (UnsupportedLookAndFeelException ex) {
                    }
    
                    ScrollablePane content = new ScrollablePane();
                    content.setLayout(new BorderLayout());
                    JLabel imageLabel = new JLabel();
                    try {
                        imageLabel.setIcon(new ImageIcon(ImageIO.read(new File("/Users/swhitehead/Dropbox/issue169.jpg"))));
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
    
                    content.add(imageLabel);
    
                    ScrollablePane listPane = new ScrollablePane();
                    listPane.setLayout(new GridLayout(0, 1));
                    for (int index = 0; index < 100; index++) {
                        listPane.add(new JLabel("This is a simple test - " + index));
                    }
    
                    content.add(new JScrollPane(listPane), BorderLayout.EAST);
    
                    JFrame frame = new JFrame();
                    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                    frame.setLayout(new BorderLayout());
                    frame.add(content);
                    frame.pack();
                    frame.setLocationRelativeTo(null);
                    frame.setVisible(true);
                }
            });
        }
    
        public class ScrollablePane extends JPanel implements Scrollable {
    
            @Override
            public Dimension getPreferredScrollableViewportSize() {
                return new Dimension(200, 100);
            }
    
            @Override
            public int getScrollableUnitIncrement(Rectangle visibleRect, int orientation, int direction) {
                return 100;
            }
    
            @Override
            public int getScrollableBlockIncrement(Rectangle visibleRect, int orientation, int direction) {
                return 100;
            }
    
            @Override
            public boolean getScrollableTracksViewportWidth() {
                return true;
            }
    
            @Override
            public boolean getScrollableTracksViewportHeight() {
                return false;
            }
    
        }
    
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-01-03
      • 2016-12-01
      • 1970-01-01
      • 2016-09-01
      • 2012-07-11
      • 2018-04-08
      • 2017-04-20
      • 2018-10-02
      相关资源
      最近更新 更多