简体   繁体   English

不同尺寸的摆板

[英]Swing panels with different sizes

I'm trying to put components into panels having different sizes. 我正在尝试将组件放入尺寸不同的面板中。 But, I realized that GridLayout divides the parts with same sizes. 但是,我意识到GridLayout将具有相同尺寸的零件分开。 How can it be accomplished as explained below image 如何实现,如下图所示

enter image description here 在此处输入图片说明

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

class PanelDemo {

    PanelDemo() {

        // Create a new JFrame container. Use the default
        // border layout.
        JFrame jfrm = new JFrame("Use Three JPanels with Different Sizes");

        // Specify FlowLayout manager.
        jfrm.getContentPane().setLayout(new GridLayout(1, 3));

        // Give the frame an initial size.
        jfrm.setSize(900, 300);

        // Terminate the program when the user closes the application.
        jfrm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        // Create the first JPanel.
        JPanel jpnl = new JPanel();

        jpnl.setLayout(new GridLayout(2, 4));

        // Set the preferred size of the first panel.
        jpnl.setPreferredSize(new Dimension(500, 300));

        // Make the panel opaque.
        jpnl.setOpaque(true);

        // Add a blue border to the panel.
        jpnl.setBorder(
                BorderFactory.createLineBorder(Color.BLUE));

        // Create the second JPanel.
        JPanel jpnl2 = new JPanel();

        //jpnl2.setLayout(new FlowLayout());
        // Set the preferred size of the second panel.
        jpnl2.setPreferredSize(new Dimension(300, 300));

        // Make the panel opaque.
        jpnl2.setOpaque(true);

        jpnl2.setBorder(
                BorderFactory.createLineBorder(Color.RED));

        JPanel jpnl3 = new JPanel();
        jpnl3.setOpaque(true);
        jpnl3.setPreferredSize(new Dimension(100, 300));
        jpnl3.setBorder(
                BorderFactory.createLineBorder(Color.ORANGE));

        // Add the panels to the frame.
        jfrm.getContentPane().add(jpnl);
        jfrm.getContentPane().add(jpnl3);
        jfrm.getContentPane().add(jpnl2);

        // Display the frame.
        jfrm.setVisible(true);
    }

    public static void main(String args[]) {
        // Create the frame on the event dispatching thread.
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                new PanelDemo();
            }
        });
    }
}

您可以使用FlowLayout,但如果他们调整框架的大小,则它将包裹面板。

在此处输入图片说明

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

public class TwoPanelWithButtonsLayout {

    private JComponent ui = null;
    private Insets buttonMargin = new Insets(10,10,10,10);

    TwoPanelWithButtonsLayout() {
        initUI();
    }

    public void initUI() {
        if (ui!=null) return;

        ui = new JPanel(new BorderLayout(4,4));
        ui.setBorder(new EmptyBorder(4,4,4,4));

        int gap = 5;

        JPanel buttons1 = new JPanel(new GridLayout(2, 4, gap, gap));
        // 50 is the gap on right, alter as needed
        buttons1.setBorder(new EmptyBorder(0, 0, 0, 50)); 
        for (int ii=1; ii<9; ii++) {
            buttons1.add(getBigButton("" + ii));
        }
        ui.add(buttons1, BorderLayout.CENTER);

        JPanel buttons2 = new JPanel(new GridLayout(2, 2, gap, gap));
        for (int ii=1; ii<5; ii++) {
            buttons2.add(getBigButton("" + ii));
        }
        ui.add(buttons2, BorderLayout.LINE_END);
    }

    private JButton getBigButton(String text) {
        JButton b = new JButton(text);
        Font f = b.getFont();
        b.setFont(f.deriveFont(f.getSize()*3f));
        b.setMargin(buttonMargin);
        return b;
    }

    public JComponent getUI() {
        return ui;
    }

    public static void main(String[] args) {
        Runnable r = new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (Exception useDefault) {
                }
                TwoPanelWithButtonsLayout o = new TwoPanelWithButtonsLayout();

                JFrame f = new JFrame(o.getClass().getSimpleName());
                f.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
                f.setLocationByPlatform(true);

                f.setContentPane(o.getUI());
                f.pack();
                f.setMinimumSize(f.getSize());

                f.setVisible(true);
            }
        };
        SwingUtilities.invokeLater(r);
    }
}

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

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