我有一个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());
}
答案 0 :(得分:2)
必须覆盖JScrollPane
的父级的最大大小,或覆盖JPanel
(在JScrollPane
内)helt在图片上显示的任何内容,不知道在发布{{ {3}}
如果你想自然滚动(没有问题将Icons
与叙述相提并论),那么
使用SSCCE
将JList与一列
修改
将方法中的JFrame#pack()
移至add
中的modify
/ remove
/ JScrollPane
项目,否则无法更改{{}的大小1}}在屏幕上
答案 1 :(得分: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;
}
}
}