简体   繁体   English

调整JFrame的大小,同时相应地缩小滚动窗格

[英]Resizing JFrame while shrinking a scrollpane acordingly

I currently have a JFrame to start fullscreen, inside this jframe i have a jpanel, this jpanel includes a vertical scrollpane. 我目前有一个JFrame可以全屏启动,在这个jframe中我有一个jpanel,这个jpanel包括一个垂直滚动窗格。 Now if i resize my jframe vertically it just kinda removes the bottom part of the jpanel. 现在,如果我垂直调整jframe的大小,就可以删除jpanel的底部。 Is there any way to just shrink the jscrollpane. 有什么方法可以缩小jscrollpane。

im currently using flowlayout for the jframe, 我目前正在为jframe使用flowlayout,

Scrollbar appear automatically when the preferred size of the components added to the scroll pane area greater than the size of the scroll pane. 当添加到滚动窗格区域的组件的首选大小大于滚动窗格的大小时,滚动条会自动出现。

The FlowLayout will wrap components to a new row, but it always gives the preferred size as the size required to fit the components on a single row, so the preferred height will never change. FlowLayout会将组件包装到新行,但是它总是提供首选的大小,作为将组件安装在单行中所需的大小,因此首选的高度永远不会改变。

To solve this problem you can use the Wrap Layout which simple extend FlowLayout to recalculate the preferred size when wrapping occurs. 要解决此问题,您可以使用Wrap Layout ,它简单地扩展了FlowLayout以在发生包装时重新计算首选大小。

The JPanel consists of 3 other panels, a top panel, a scrollpane in the middle and a botpanel. JPanel包括其他3个面板,一个顶面板,一个中间的滚动面板和一个botpanel。 The top and bot panel are just button and checkboxes and stuff 顶部和机器人面板只是按钮,复选框和其他内容

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)));
}

this is the panel i add to the jframe 这是我添加到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);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM