Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/339.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 调整JFrame的大小,同时相应地缩小滚动窗格_Java_Swing - Fatal编程技术网

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

Java 调整JFrame的大小,同时相应地缩小滚动窗格,java,swing,Java,Swing,我现在有一个JFrame来启动全屏,在这个JFrame中我有一个jpanel,这个jpanel包括一个垂直滚动窗格。现在,如果我垂直调整jframe的大小,它只会移除jpanel的底部。是否有任何方法来收缩JScLanPan.< /P> < P> JPanel由3个其他面板组成,一个顶部面板,一个中间的滚动窗格和一个BoTPanel。顶部和机器人面板只是按钮和复选框之类的东西 private void initPane() { createFolderCompPanel(); c

我现在有一个JFrame来启动全屏,在这个JFrame中我有一个jpanel,这个jpanel包括一个垂直滚动窗格。现在,如果我垂直调整jframe的大小,它只会移除jpanel的底部。是否有任何方法来收缩JScLanPan.< /P> < P> 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)));
}

im当前正在使用jframe的flowlayout

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

FlowLayout会将组件包装到一个新行,但它始终提供首选尺寸,作为在一行上安装组件所需的尺寸,因此首选高度永远不会改变


要解决此问题,您可以使用which simple extend FlowLayout在进行换行时重新计算首选大小。

似乎您需要使用正确的方法。Post for help.im当前正在使用jframe的flowlayout,如果我更改jpanel的preferedsize,滚动窗格将调整大小。因此,我只需要一种方法,在调整JFrame的大小时更改jpanel的首选大小jpanel的底部是什么?请多解释一点或显示代码,这会有帮助的。
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);
}