左側が画像を保持し、右側がいくつかのラベルオブジェクトを保持するパネルであるGUIがあります。右側のパネルに収まるよりも多くのオブジェクトがある場合に、スクロールバーが表示されるようにしたいと思います。
私が抱えている問題は、右側のパネルに多くのオブジェクトが追加されると、フレーム全体のサイズが変更されることです。
ここにいくつかのスクリーンショットがあります。最初の画像はウィンドウのサイズを常に設定したいサイズで、2番目の画像はサイズ変更の問題を示しています。
2番目の画像:
画像パネルとサイドバーパネルを保持するパネルのコードは次のとおりです。
/**
* 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());
}