【问题标题】:JButton size and JScrollPane not working well togetherJButton 大小和 JScrollPane 不能很好地协同工作
【发布时间】:2014-07-17 20:32:42
【问题描述】:

我彻底糊涂了。我对每个布局管理器的工作原理以及每个布局管理器的用途有相当不错的了解,但我不明白布局管理器和 JPanel 的组合对于完成我需要的工作是必要的。

我想要完成的事情

我有一个顶部栏和一个容器面板的底部栏 NORTH 和 BorderLayout 的 SOUTH。 在中心面板中,我想要一个未知数量的按钮 1 或更多。不管有多少按钮,它们都需要具有相同的大小,如果有几十个,那么一旦按钮超过窗口大小限制,就应该开始滚动。

我得到了什么

根据布局管理器的组合和我使用的嵌套 JPanel 数量以及各种故障排除,我得到一个填充整个 CENTER 元素的巨大按钮。我得到了 2 个大小合适的按钮,但分散得很远(填充 CENTER 空间的间隙),或者我得到了十几个大小合适但没有滚动的按钮。

我可以解决其中任何一个问题,但其他问题就会中断。 IE 如果我得到一堆正确滚动的大小正确的按钮,那么当我用一个按钮替换它们时,它就是一个巨大的按钮。或者,如果我得到一个大小合适的按钮,那么更大的数量将不会滚动等等。

我的代码

import javax.swing.*;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.io.*;

public class TestCode extends JFrame {
     private final JFrame frame;

    public TestCode(){

        frame = new JFrame();

        JLabel title = new JLabel("Test Title");

        JPanel windowContainer = new JPanel();      
        JPanel topPanel = new JPanel();             
        final JPanel middlePanel = new JPanel();    
        JPanel bottomPanel = new JPanel();          

        JButton searchButton = new JButton("Search");
        JButton browseButton = new JButton("Browse...");
        JButton testButton = new JButton("Button 1");
        JButton exitButton = new JButton("Exit");

        final JTextField searchBar = new JTextField("Search database...");

        topPanel.setLayout(new GridLayout(2,0));
        topPanel.add(title);
                title.setHorizontalAlignment(JLabel.CENTER);
        topPanel.setPreferredSize(new Dimension(getWidth(), 100));
                // This is a subset of the top section. Top part is two panels, bottom panel is two cells (grid)
                JPanel topPanelSearch = new JPanel();
                topPanelSearch.setLayout(new GridLayout(0,2));
                topPanelSearch.add(searchBar);
                topPanelSearch.add(searchButton);
        topPanel.add(topPanelSearch);

// PROBLEM AREA STARTS

//      middlePanel.setLayout(new FlowLayout());
        middlePanel.setLayout(new GridLayout(0, 1, 10, 10));
//      middlePanel.setLayout(new BoxLayout(middlePanel, BoxLayout.PAGE_AXIS));
        JPanel innerContainer = new JPanel();

        innerContainer.setLayout(new BoxLayout(innerContainer, BoxLayout.PAGE_AXIS));
//      innerContainer.setLayout(new FlowLayout());
//      innerContainer.setLayout(new GridLayout(0, 1, 10, 10));

        for(int i = 0; i < 2; i++){
            JButton button = new JButton("Button ");
            button.setPreferredSize(new Dimension(400, 100));
            JPanel test = new JPanel();
            test.add(button);
            innerContainer.add(test);
        }

        JScrollPane midScroll = new JScrollPane(innerContainer);
        middlePanel.add(midScroll);

// PROBLEM AREA ENDS

        bottomPanel.setLayout(new GridLayout(0, 3));
        bottomPanel.setPreferredSize(new Dimension(getWidth(), 100));
        bottomPanel.add(testButton);
        bottomPanel.add(browseButton);
        bottomPanel.add(exitButton);

        windowContainer.setLayout(new BorderLayout());
        windowContainer.add(topPanel, BorderLayout.NORTH);
        windowContainer.add(middlePanel, BorderLayout.CENTER);
        windowContainer.add(bottomPanel, BorderLayout.SOUTH);

        frame.add(windowContainer);


        frame.setTitle("Test");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setSize(480, 800);
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);

    }

    public static void main(String[] args){
        TestCode test = new TestCode();
    }
}

部分失败结果的可视化

我想要最左边的图片,但是当结果很少时按钮应该堆叠整齐(如中间图片),当结果很多时可以滚动。

我做错了什么?

【问题讨论】:

  • 您是否尝试将新的 JPanel 添加到 CENTER 元素并将其布局设置为 gridlayout?如果你用 0 行和 1 列制作它应该可以为任意数量的按钮做这个技巧(我还没有测试过)之后你只需要添加滚动......
  • @MyNameIsRui 是的,上面代码中的 middlePanel 是 CENTER 中的面板,注释掉的许多布局行之一就是执行此操作的网格布局。这就是我在左边得到的结果。网格不太友好。
  • 我明白了...我认为您应该尝试其他布局,也许是 gridbaglayout 或 flowlayout(定义每行 1 个组件的数量)我有一段时间没有使用 swing,所以我没有即使确定这样的事情是否可能。我希望你能修复它,我真的很确定 gridlayout 会成功

标签: java swing user-interface jframe jbutton


【解决方案1】:

尝试使用GridBagLayout 和填充组件,该组件占用剩余的垂直空间并因此强制按钮向上。

import java.awt.BorderLayout;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import javax.swing.Box.Filler;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.SwingUtilities;

public class GridbagButtons extends JFrame {

    private final JScrollPane jscrpButtons;
    private final JPanel jpButtons;

    public GridbagButtons() {
        setLayout(new BorderLayout());        

        jpButtons = new JPanel(new GridBagLayout());
        jscrpButtons = new JScrollPane(jpButtons);
        add(jscrpButtons, BorderLayout.CENTER);

        // add a custom number of buttons
        int numButtons = 10;
        for (int i = 0; i < numButtons; i++) {
            JButton jbButton = new JButton("Button");
            GridBagConstraints gbc = new GridBagConstraints();
            gbc.gridx = 0;
            gbc.gridy = i;
            jpButtons.add(jbButton, gbc);
        }

        // add a vertical filler as last component to "push" the buttons up
        GridBagConstraints gbc = new GridBagConstraints();
        Filler verticalFiller = new Filler(
                new java.awt.Dimension(0, 0), 
                new java.awt.Dimension(0, 0), 
                new java.awt.Dimension(0, Integer.MAX_VALUE));
        gbc.gridx = 0;
        gbc.gridy = numButtons;
        gbc.fill = java.awt.GridBagConstraints.VERTICAL;
        gbc.weighty = 1.0;
        jpButtons.add(verticalFiller, gbc);

        setSize(300, 200);
        setLocationRelativeTo(null);
    }

    public static void main(String[] args) {        
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                new GridbagButtons().setVisible(true);
            }
        });

    }

}

【讨论】:

    猜你喜欢
    • 2016-12-08
    • 2020-11-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-05-14
    • 2018-01-18
    • 1970-01-01
    • 2018-12-25
    相关资源
    最近更新 更多