在Swing GUI中提供空白

内容来源于 Stack Overflow,并遵循CC BY-SA 3.0许可协议进行翻译与使用

  • 回答 (2)
  • 关注 (0)
  • 查看 (11)

没有空白的GUI看起来“拥挤”。

提问于
用户回答回答于

Swing GUI中有多种方法可以在组件和组件周围的空白之间提供分离:

JToolBar有方法addSeparator()&addSeparator(Dimension)。

JMenu使用更适合菜单的间隔组件,可通过addSeparator()。

但更广泛地说,请注意:

但更广泛地说,请注意:

  • 可以在布局构造函数中定义的间距。
  • 边界。

下面是使用布局分隔符的示例hGap&vGap值和边框(特别是EmptyBorder)提供“白色”(实际上显示为“红色”以使其非常明显)。调整纺丝器以查看结果。

import java.awt.*;
import javax.swing.*;
import javax.swing.border.EmptyBorder;
import javax.swing.event.*;

public class WhiteSpace {

    private JPanel gui = null;
    private BorderLayout mainLayout;
    private FlowLayout buttonLayout;
    private EmptyBorder border;

    public Container getGui() {
        if (gui==null) {
            mainLayout = new BorderLayout(0,0);
            gui = new JPanel(mainLayout);
            gui.setBackground(Color.RED);
            border = new EmptyBorder(0,0,0,0);

            JTree tree = new JTree();
            tree.setVisibleRowCount(10);
            for (int ii = tree.getRowCount(); ii>-1; ii--) {
                tree.expandRow(ii);
            }
            gui.add(new JScrollPane(
                    tree, 
                    JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,
                    JScrollPane.HORIZONTAL_SCROLLBAR_NEVER), 
                    BorderLayout.LINE_START);
            gui.add(new JScrollPane(new JTextArea(10,30)));

            buttonLayout = new FlowLayout(FlowLayout.CENTER,0,0);
            JPanel buttonPanel = new JPanel(buttonLayout);
            gui.add(buttonPanel, BorderLayout.PAGE_START);

            buttonPanel.add(new JLabel("H Gap"));
            final JSpinner hSpinner = 
                    new JSpinner(new SpinnerNumberModel(0,0,15,1));
            buttonPanel.add(hSpinner);

            buttonPanel.add(new JLabel("V Gap"));
            final JSpinner vSpinner = 
                    new JSpinner(new SpinnerNumberModel(0,0,15,1));
            buttonPanel.add(vSpinner);

            buttonPanel.add(new JLabel("H Border"));
            final JSpinner hBorderSpinner = 
                    new JSpinner(new SpinnerNumberModel(0,0,15,1));
            buttonPanel.add(hBorderSpinner);

            buttonPanel.add(new JLabel("V Border"));
            final JSpinner vBorderSpinner = 
                    new JSpinner(new SpinnerNumberModel(0,0,15,1));
            buttonPanel.add(vBorderSpinner);

            ChangeListener changeListener = new ChangeListener() {

                @Override
                public void stateChanged(ChangeEvent e) {
                    int hGap = ((Integer)hSpinner.getValue()).intValue();
                    int vGap = ((Integer)vSpinner.getValue()).intValue();
                    int hBorder = ((Integer)hBorderSpinner.getValue()).intValue();
                    int vBorder = ((Integer)vBorderSpinner.getValue()).intValue();
                    adjustWhiteSpace(hGap,vGap,hBorder,vBorder);
                }
            };

            hSpinner.addChangeListener(changeListener);
            vSpinner.addChangeListener(changeListener);
            hBorderSpinner.addChangeListener(changeListener);
            vBorderSpinner.addChangeListener(changeListener);
        }

        return gui;
    }

    private void adjustWhiteSpace(int hGap, int vGap, int hBorder, int vBorder) {
        mainLayout.setHgap(hGap);
        mainLayout.setVgap(vGap);
        buttonLayout.setHgap(hGap);
        gui.setBorder(new EmptyBorder(vBorder,hBorder,vBorder,hBorder));
        Container c = gui.getTopLevelAncestor();
        if (c instanceof Window) {
            Window w = (Window)c;
            w.pack();
        }
    }

    public static void main(String[] args) {
        Runnable r = new Runnable() {

            @Override
            public void run() {
                WhiteSpace ws = new WhiteSpace();
                // the GUI as seen by the user (without frame)
                Container gui = ws.getGui();

                JFrame f = new JFrame("White (OK Red) Space");
                f.add(gui);
                // Ensures JVM closes after frame(s) closed and
                // all non-daemon threads are finished
                f.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
                // See http://stackoverflow.com/a/7143398/418556 for demo.
                f.setLocationByPlatform(true);

                // ensures the frame is the minimum size it needs to be
                // in order display the components within it
                f.setResizable(false);
                f.pack();
                // should be done last, to avoid flickering, moving,
                // resizing artifacts.
                f.setVisible(true);
            }
        };
        SwingUtilities.invokeLater(r);
    }
}
用户回答回答于

使用各种LayoutManagers一个可以提供不同组件之间的间隔。

1)的BorderLayout 超载结构:BorderLayout的(INT水平间隙,INT垂直间隙) getter和setter方法水平间距:BorderLayout.getHGap()和BorderLayout.setHGap(Int HGAP)垂直间距:BorderLayout.getVGap()和BorderLayout.setVGap() 2.)弗劳莱奥特 超载结构:FlowLayout(int对齐,int HGAP,int vGap) FlowLayout.getHGap()和FlowLayout.setHGap(Int HGAP)垂直间距:FlowLayout.getVGap()和FlowLayout.setVGap() 3)GridLayout的 超载结构:GridLayout(int行,int列,int HGAP,int vGap) getter和setter方法水平间距:GridLayout.getHGap(和GridLayout.setHGap(Int HGAP) 垂直间距:GridLayout.getVGap()和GridLayout.setVGap() 4)的GridBagLayout GridBagConstraints.Inset 5.)卡德莱奥特例 CardLayout(int HGAP,int vGap)

示例以显示所有实际操作的构造函数:

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class LayoutExample {

    private final int hGap = 5;
    private final int vGap = 5;

    private String[] borderConstraints = {
        BorderLayout.PAGE_START,
        BorderLayout.LINE_START,
        BorderLayout.CENTER,
        BorderLayout.LINE_END,
        BorderLayout.PAGE_END
    };

    private JButton[] buttons;

    private GridBagConstraints gbc;

    private JPanel borderPanel;
    private JPanel flowPanel;
    private JPanel gridPanel;
    private JPanel gridBagPanel;
    private JPanel cardPanel;

    public LayoutExample() {
        buttons = new JButton[16];
        gbc = new GridBagConstraints();
        gbc.anchor = GridBagConstraints.FIRST_LINE_START;   
        gbc.insets = new Insets(hGap, vGap, hGap, vGap);        
    }

    private void displayGUI() {
        JFrame frame = new JFrame("Layout Example");
        frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);

        JPanel contentPane = new JPanel(
                        new GridLayout(0, 1, hGap, vGap));
        contentPane.setBorder(
            BorderFactory.createEmptyBorder(hGap, vGap, hGap, vGap));
        borderPanel = new JPanel(new BorderLayout(hGap, vGap));
        borderPanel.setBorder(
            BorderFactory.createTitledBorder("BorderLayout"));
        borderPanel.setOpaque(true);
        borderPanel.setBackground(Color.WHITE);
        for (int i = 0; i < 5; i++) {
            buttons[i] = new JButton(borderConstraints[i]);
            borderPanel.add(buttons[i], borderConstraints[i]);
        }
        contentPane.add(borderPanel);

        flowPanel = new JPanel(new FlowLayout(
                    FlowLayout.CENTER, hGap, vGap));
        flowPanel.setBorder(
            BorderFactory.createTitledBorder("FlowLayout"));
        flowPanel.setOpaque(true);
        flowPanel.setBackground(Color.WHITE);
        for (int i = 5; i < 8; i++) {
            buttons[i] = new JButton(Integer.toString(i));
            flowPanel.add(buttons[i]);
        }
        contentPane.add(flowPanel);

        gridPanel = new JPanel(new GridLayout(2, 2, hGap, vGap));
        gridPanel.setBorder(
            BorderFactory.createTitledBorder("GridLayout"));
        gridPanel.setOpaque(true);
        gridPanel.setBackground(Color.WHITE);
        for (int i = 8; i < 12; i++) {
            buttons[i] = new JButton(Integer.toString(i));
            gridPanel.add(buttons[i]);
        }
        contentPane.add(gridPanel);

        gridBagPanel = new JPanel(new GridBagLayout());
        gridBagPanel.setBorder(
            BorderFactory.createTitledBorder("GridBagLayout"));
        gridBagPanel.setOpaque(true);
        gridBagPanel.setBackground(Color.WHITE);
        buttons[12] = new JButton(Integer.toString(12));
        addComp(gridBagPanel, buttons[12], 0, 0, 1, 1
                            , GridBagConstraints.BOTH, 0.33, 0.5);
        buttons[13] = new JButton(Integer.toString(13));
        addComp(gridBagPanel, buttons[13], 1, 0, 1, 1
                            , GridBagConstraints.BOTH, 0.33, 0.5);
        buttons[14] = new JButton(Integer.toString(14));
        addComp(gridBagPanel, buttons[14], 0, 1, 2, 1
                            , GridBagConstraints.BOTH, 0.66, 0.5);
        buttons[15] = new JButton(Integer.toString(15));
        addComp(gridBagPanel, buttons[15], 2, 0, 1, 2
                            , GridBagConstraints.BOTH, 0.33, 1.0);
        contentPane.add(gridBagPanel);

        cardPanel = new JPanel(new CardLayout(hGap, vGap));
        cardPanel.setBorder(
            BorderFactory.createTitledBorder("CardLayout"));
        cardPanel.setOpaque(true);
        cardPanel.setBackground(Color.WHITE);
        cardPanel.add(getPanel(Color.BLUE));
        cardPanel.add(getPanel(Color.GREEN));
        contentPane.add(cardPanel);

        frame.setContentPane(contentPane);
        frame.pack();
        frame.setLocationByPlatform(true);
        frame.setVisible(true);
    }

    private JPanel getPanel(Color bColor) {
        JPanel panel = new JPanel(new FlowLayout(
                    FlowLayout.CENTER, hGap, vGap));
        panel.setOpaque(true);
        panel.setBackground(bColor.darker().darker());
        JButton swapperButton = new JButton("Next");
        swapperButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent ae) {
                CardLayout cardLayout = (CardLayout) cardPanel.getLayout();
                cardLayout.next(cardPanel);
            }
        });

        panel.add(swapperButton);

        return panel;
    }

    private void addComp(JPanel panel, JComponent comp
                                , int x, int y, int gWidth
                                    , int gHeight, int fill
                                        , double weightx, double weighty) {
        gbc.gridx = x;
        gbc.gridy = y;
        gbc.gridwidth = gWidth;
        gbc.gridheight = gHeight;
        gbc.fill = fill;
        gbc.weightx = weightx;
        gbc.weighty = weighty;      

        panel.add(comp, gbc);
    }

    public static void main(String[] args) {
        Runnable runnable = new Runnable(){
            @Override
            public void run() {
                new LayoutExample().displayGUI();
            }
        };
        EventQueue.invokeLater(runnable);
    }
}

产出:

扫码关注云+社区