简体   繁体   English

Java Swing BoxLayout:在多面板设置中调整面板之间的空间?

[英]Java Swing BoxLayout: Adjusting space between panels in a multi-panel setup?

I'm writing this swing application and I'm using multiple panels in a BoxLayout format, but it seems to be that the empty space between the panels is being divided up between them and it looks really ugly.我正在编写这个 swing 应用程序,并且我正在使用 BoxLayout 格式的多个面板,但似乎面板之间的空白空间正在它们之间被分割,看起来真的很难看。 How do you adjust and customize how much space is put between panels?您如何调整和自定义面板之间的空间大小? Sorry if this is a repost;抱歉,如果这是转发; I was unable to find an older post.我找不到更旧的帖子。

    import java.awt.*;
import java.awt.datatransfer.Clipboard;
import java.awt.datatransfer.StringSelection;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.*;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;

class gui extends JFrame implements ActionListener{
    JFrame frame = new JFrame("Ark Admin Spawn Commands");
    JPanel panel = new JPanel();
    JComboBox dinos;
    JComboBox corruptedDinos;
    public JSlider dinoLevel;
    JLabel input;
    JLabel dino;
    JLabel title;
    
    
    
    
    
    gui(){
        
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

          JPanel mainPanel = new JPanel();
          mainPanel.setLayout(new BoxLayout(mainPanel, BoxLayout.Y_AXIS));

          JPanel panelOne = new JPanel();
          JPanel panelTwo = new JPanel();
          JPanel panelThree = new JPanel();

          mainPanel.add(panelOne);
          mainPanel.add(panelTwo);
          mainPanel.add(panelThree);
         
        
        
        
        title = new JLabel("Spawning Dinos");
        
        String[] dinoNames= {"The Island:"};
        String[] corruptedDinoNames = {"Extinction:"};
        dinos = new JComboBox(dinoNames);
        corruptedDinos = new JComboBox(corruptedDinoNames);
        dinoLevel = new JSlider(JSlider.HORIZONTAL, 1, 600, 400);
        dinoLevel.setMajorTickSpacing(20);
        dinoLevel.setPaintTicks(true);
        input = new JLabel("Select Level: ");
        event e = new event();
        dinoLevel.addChangeListener(e);
        
        
        dinos.addActionListener(this);
        corruptedDinos.addActionListener(this);
        panelOne.add(title);
        
        panelTwo.add(input);
        panelTwo.add(dinoLevel);
        
        panelThree.add(dinos);
        panelThree.add(corruptedDinos);
        this.getContentPane().add(mainPanel);
        this.setTitle("Ark Admin Spawn Commands");
        this.setSize(600, 600);
        this.setVisible(true);
        
    }
    public static void main (String args[]) {

        new gui();
        
        
    
    
    }
    
    public class event implements ChangeListener{
        
        @Override
        public void stateChanged(ChangeEvent e) {
            int value = dinoLevel.getValue();
            input.setText("Level: " + value);
            
        }
    }
    
    @Override
    public void actionPerformed(ActionEvent arg0) {
        
        
    }
}

but it seems to be that the empty space between the panels is being divided up between them但似乎面板之间的空白空间正在它们之间被分割

Correct.正确的。 A BoxLayout will attempt to allocate extra space to all components. BoxLayout将尝试为所有组件分配额外的空间。

However, it will respect the maximum size of each panel.但是,它将尊重每个面板的最大尺寸。

So to prevent the panels height from growing you can use code like:因此,为了防止面板高度增长,您可以使用如下代码:

JPanel panelOne = new JPanel()
{
    @Override
    public dimension getMaximumSize()
    {
        Dimension d = getPreferredSize()
        d.width = Integer.MAX_VALUE;

        return d;
    }
};

Or because the default layout manager of the frame is a BorderLayout , you can just use:或者因为框架的默认布局管理器是BorderLayout ,您可以使用:

//this.getContentPane().add(mainPanel);
add(mainPanel, BorderLayout.PAGE_START); // getContentPane() is not needed

The PAGE_START of the BorderLayout respects the preferred height of the component. BorderLayout 的 PAGE_START 尊重组件的首选高度。

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

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