1

我有一个 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());
}
4

2 回答 2

2
  • 必须覆盖父级的最大大小JScrollPane,或者覆盖JPanel(内部JScrollPane)helt 图片上显示的任何内容,不知道尽快发布SSCCE以获得更好的帮助

  • 如果您想要自然滚动(没有问题可以Icons使用叙述)然后

  • 使用JList

  • JTable与一列一起使用

编辑

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

于 2012-10-15T10:31:07.457 回答
1

您尝试利用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;
        }

    }

}
于 2012-10-15T11:07:07.997 回答