在缩小滚动窗格的同时调整JFrame的大小

时间:2015-05-05 11:02:21

标签: java swing

我目前有一个JFrame来启动全屏,在这个jframe里面我有一个jpanel,这个jpanel包含一个垂直滚动窗格。现在,如果我垂直调整我的jframe,它只是有点删除jpanel的底部。有没有办法缩小jscrollpane。

2 个答案:

答案 0 :(得分:2)

  

我目前正在为jframe使用flowlayout,

当添加到滚动窗格区域的组件的首选大小大于滚动窗格的大小时,会自动显示滚动条。

FlowLayout会将组件包装到新行,但它总是将首选大小作为使组件适合单行所需的大小,因此首选高度永远不会改变。

要解决此问题,您可以使用Wrap Layout简单扩展FlowLayout,以便在换行时重新计算首选大小。

答案 1 :(得分:0)

JPanel由3个其他面板组成,顶部面板,中间的滚动窗格和botpanel。顶部和机器人面板只是按钮和复选框和东西

private void initPane() {
    createFolderCompPanel();
    createBotPanel();
    createTopPanel();
    createScrollPane();
    createTotalPanel();
    add(totalPanel);
}

private void createFolderCompPanel() {
    //Create folderCompPanel
    folderCompPanel = new JPanel();
    folderCompPanel.setLayout(new BoxLayout(folderCompPanel, BoxLayout.Y_AXIS));
    folderCompPanel.add(Box.createVerticalGlue());
}

private void createTotalPanel() {
    //Create TotalPanel
    totalPanel = new JPanel();
    totalPanel.setLayout(new BoxLayout(totalPanel, BoxLayout.Y_AXIS));
    totalPanel.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10));
    totalPanel.add(topPanel);
    totalPanel.add(scrollPane);
    totalPanel.add(botPanel);
}

private void createScrollPane() {
    //Create ScrollPane
    scrollPane = new JScrollPane();
    scrollPane.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10));
    scrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
    scrollPane.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
    scrollPane.setViewportBorder(BorderFactory.createEtchedBorder());
    scrollPane.getVerticalScrollBar().setUnitIncrement(6);

}

private void createBotPanel() {
    //Create BotPanel
    botPanel = new JPanel();
    botPanel.setLayout(new BoxLayout(botPanel, BoxLayout.X_AXIS));

    //AddButton
    addButton = new JButton("Add");
    addButton.setEnabled(false);
    addButton.addActionListener(this);

    //SaveButton
    saveButton = new JButton("Save");
    saveButton.setEnabled(false);
    saveButton.addActionListener(this);

    //CancelButton
    cancelButton = new JButton("Cancel");
    cancelButton.setEnabled(false);
    cancelButton.addActionListener(this);

    lblTotalLength = new JLabel("Total Length: " + totalLength);
    botPanel.add(Box.createRigidArea(new Dimension(10, 0)));
    botPanel.add(addButton);
    botPanel.add(Box.createRigidArea(new Dimension(10, 0)));
    botPanel.add(lblTotalLength);
    botPanel.add(Box.createHorizontalGlue());
    botPanel.add(saveButton);
    botPanel.add(Box.createRigidArea(new Dimension(10, 0)));
    botPanel.add(cancelButton);
    botPanel.add(Box.createRigidArea(new Dimension(10, 0)));

}

private void createTopPanel() {
    //Create TopPanel
    topPanel = new JPanel();
    topPanel.setLayout(new BoxLayout(topPanel, BoxLayout.X_AXIS));

    //create deletedisplay button
    deleteDisplayButton = new JButton("Delete Display");
    deleteDisplayButton.addActionListener(this);
    deleteDisplayButton.setEnabled(false);

    //create displaybox
    displayBox = new JComboBox();
    displayBox.addActionListener(this);
    displayBox.addItem("<None>");
    for (String s : connect.getAllDisplays()) {
        displayBox.addItem(s);
    }
    displayBox.setMaximumSize(displayBox.getPreferredSize());

    //create newdisplay button
    newDisplayButton = new JButton("New Display");
    newDisplayButton.addActionListener(this);

    topPanel.add(Box.createRigidArea(new Dimension(10, 0)));
    topPanel.add(displayBox);
    topPanel.add(Box.createHorizontalGlue());
    topPanel.add(newDisplayButton);
    topPanel.add(Box.createRigidArea(new Dimension(5, 0)));
    topPanel.add(deleteDisplayButton);
    topPanel.add(Box.createRigidArea(new Dimension(10, 0)));
}

这是我添加到jframe的面板

public GuiConstructor(){
    super(APPLICATION_NAME);
    this.setExtendedState(JFrame.MAXIMIZED_BOTH);
    this.setMinimumSize(new Dimension(630, 600));
    setLayout(new FlowLayout(FlowLayout.LEFT));
    LoopControlWindow folderSearch = new LoopControlWindow(connect, this);
    add(folderSearch);
    pack();
    setLocationRelativeTo(null);
    setResizable(true);
    setVisible(true);
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}