带有BoxLayout的容器通过组件调整大小



我目前正在研究一个研究项目,我对GUI有一个小问题。

这是一些代码:

private JButton zoomUp, zoomDown;
private JComboBox fractalList;
private JLabel choice,space;
private JPanel ui,display;
private JFrame window;
    public FractalView(FractalModel m, FractalController c, String title, int size_X, int size_Y)
    { 
    window =new JFrame();
    window.setTitle(title);
    window.setSize(size_X,size_Y);
    window.setLocationRelativeTo(null);
    window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    window.setResizable(false);
    window.setLayout(new GridBagLayout());

    GridBagConstraints gbc = new GridBagConstraints();
    gbc.anchor=GridBagConstraints.FIRST_LINE_START;
    ui=new JPanel();        
    ui.setBackground(Color.GRAY);
    ui.setBorder(BorderFactory.createTitledBorder("Interface de Controle et Options"));
    BoxLayout uiLayout = new BoxLayout(ui, BoxLayout.PAGE_AXIS);
    ui.setLayout(uiLayout);
    ui.setMaximumSize(ui.getPreferredSize());
    zoomUp = new JButton("Zoom +");
    zoomUp.setAlignmentX(LEFT_ALIGNMENT);
    zoomUp.setBorder(BorderFactory.createLineBorder(Color.RED));
    ui.add(zoomUp);
    zoomDown = new JButton("Zoom -");
    //ui.add(zoomDown);
    choice = new JLabel("Choisir une fractale : ");
    //ui.add(choice);
    Object[] elements = new Object[]{"Mandelbrot", "Julia", "Buddhabrot"};
    fractalList = new JComboBox<Object>(elements);
    //fractalList.setBorder(BorderFactory.createLineBorder(Color.RED));
    fractalList.setAlignmentX(LEFT_ALIGNMENT);
    fractalList.setMaximumSize(new Dimension(Short.MAX_VALUE,20));
    //ui.add(fractalList);

    gbc.weighty=1;
    gbc.ipadx=300;
    gbc.gridheight=1;
    gbc.fill=GridBagConstraints.VERTICAL;
    gbc.gridx=0;
    gbc.gridy=0;
    window.add(ui,gbc);
    display=new JPanel();
    display.setBackground(Color.RED);
    gbc.weightx=1;
    gbc.gridheight=1;
    gbc.gridwidth=1;
    gbc.fill=GridBagConstraints.BOTH;
    gbc.gridx=1;
    gbc.gridy=0;
    window.add(display,gbc);
    window.setVisible(true);
    }

问题来自JPanel ui。只要它是空的,大小就可以了。但是,当我向其添加任何组件时,重量会增加。

我尝试使用setMaxSize(),但是即使它进行了调整大小,这可能会导致我想在JPanel display上显示的问题。我希望再次避免再次使用GridBagLayout

有人有想法吗?

如果您只想将框架拆分而不想与Gridbaglayout战斗,则可以使用BorderLayout。

然后,将一个面板添加到borderlayout.west.west,另一个将一个面板添加到borderlayout.east位置。并记住致电SetPreferredSize以修复所需的宽度和高度。

我已经对您的代码进行了一些更改。检查一下:

public class TestFrame {
    private JButton zoomUp, zoomDown;
    private JComboBox fractalList;
    private JLabel choice, space;
    private JPanel ui, display;
    private JFrame window;
    public TestFrame(String title, int size_X, int size_Y) {
        window = new JFrame();
        window.setTitle(title);
        window.setSize(size_X, size_Y);
        window.setLocationRelativeTo(null);
        window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        window.setResizable(false);
        window.setLayout(new BorderLayout());
        ui = new JPanel();
        ui.setPreferredSize(new Dimension(size_X/2,size_Y));
        ui.setBackground(Color.GRAY);
        ui.setBorder(BorderFactory.createTitledBorder("Interface de Controle et Options"));
        BoxLayout uiLayout = new BoxLayout(ui, BoxLayout.PAGE_AXIS);
        ui.setLayout(uiLayout);
        zoomUp = new JButton("Zoom +");
        // zoomUp.setAlignmentX(LEFT_ALIGNMENT);
        zoomUp.setBorder(BorderFactory.createLineBorder(Color.RED));
        ui.add(zoomUp);
        zoomDown = new JButton("Zoom -");
         ui.add(zoomDown);
        choice = new JLabel("Choisir une fractale : ");
         ui.add(choice);
        Object[] elements = new Object[] { "Mandelbrot", "Julia", "Buddhabrot" };
        fractalList = new JComboBox<Object>(elements);
        // fractalList.setBorder(BorderFactory.createLineBorder(Color.RED));
        // fractalList.setAlignmentX(LEFT_ALIGNMENT);
        fractalList.setMaximumSize(new Dimension(Short.MAX_VALUE, 20));
         ui.add(fractalList);
        window.add(ui, BorderLayout.WEST);
        display = new JPanel();
        display.setBackground(Color.RED);
        display.setPreferredSize(new Dimension(size_X/2,size_Y));
        window.add(display, BorderLayout.EAST);
        window.setVisible(true);
    }
    public static void main(String[] args) {
        new TestFrame("test of BorderLayout with WEST and EAST", 800, 600);
    }
}

如果您需要比这更复杂的东西,则可以选择Miglayout。

相关内容

  • 没有找到相关文章

最新更新