如何修复我的布局看起来像这样?

时间:2017-11-03 18:16:45

标签: java swing user-interface

我遇到了嵌套Swing布局的问题,我需要创建如图所示的以下布局。

- 4个按钮

中间 - JMenu Bar和2个JLabels

正确面板 - 4个按钮

两个侧面板对我来说很好,但我不能让中间件工作。我在这里尝试过使用Borderlayout但是我只能放置1个项目NORTH(JMenu)和1个项目CENTER(第1个JLabel)。我想也许我可以用另一个JPane细分CENTER,但它不会正常工作。

我也尝试将中间部分设置为网格布局,但当然所有元素都设置为相同的大小,以便不会这样做。

这就是我想要的样子

enter image description here

代码

import java.awt.*;
import java.io.IOException;
import java.net.URL;
import javax.imageio.ImageIO;
import javax.swing.*;

class Test102 extends JFrame
{
    private JMenuBar menuTop;
    private JMenu fileMenu;

    public Test102() throws IOException
    {

        //Create 3 new Jpanels
        JPanel left = new JPanel();
        JPanel center = new JPanel();
        JPanel centerSubdivision1 = new JPanel();
        JPanel centerSubdivision2 = new JPanel();
        JPanel centerSubdivision3 = new JPanel();
        JPanel right = new JPanel();


        ////////////////////////////////////////////////////////
        //Create a grid layout - This will go to the left
        ////////////////////////////////////////////////////////

        left.setLayout ( new GridLayout ( 4, 1 ) );  //4 Rows and 1 Columns
        //Button 1
        left.add ( new JButton ( "Button 1" ) );
        //Button 2
        left.add ( new JButton ( "Button 2" ) );
        //Button 3
        left.add ( new JButton ( "Button 3" ) );
        //Button 4
        left.add ( new JButton ( "Button 4" ) );

        ////////////////////////////////////////////////////////
        //Create a grid layout - This will go in the middle.
        ////////////////////////////////////////////////////////

        center.setLayout ( new GridLayout( 4, 1) );

        //Top Menu Bar
        menuTop = new JMenuBar();
        center.add ( menuTop );

            //Top level menu
            fileMenu = new JMenu ( "File Menu", true );
            menuTop.add ( fileMenu ); //Add menu

            ///////////////////////////////////
            //Add another panel to the Centre
            ///////////////////////////////////
            center.add ( ( centerSubdivision1 ) );
            centerSubdivision1.setBackground(new Color(192, 192, 0));

            center.add ( ( centerSubdivision2 ) );
            centerSubdivision2.setBackground(new Color(192, 0, 192));
                JLabel label1 = new JLabel ( "Label 1" );
                centerSubdivision2.add ( label1 );

            center.add ( ( centerSubdivision3 ) );
            centerSubdivision3.setBackground(new Color(0, 192, 192));
                JLabel label2 = new JLabel ( "Label 2" );
                centerSubdivision2.add ( label2 );

        ////////////////////////////////////////////////////////
        //Create a grid layout - This will go to the right
        ////////////////////////////////////////////////////////

        right.setLayout ( new GridLayout ( 4, 1 ) ); //4 Rows and 1 Columns
        //Button 5
        right.add ( new JButton ( "Button 5" ) );
        //Button 6
        right.add ( new JButton ( "Button 6" ) );
        //Button 7
        right.add ( new JButton ( "Button 7" ) );
        //Button 8
        right.add ( new JButton ( "Button 8" ) );

        //Add our Jpanels to the content pane.
        getContentPane().add ( left, BorderLayout.WEST );
        getContentPane().add ( center, BorderLayout.CENTER );
        getContentPane().add ( right, BorderLayout.EAST );

        //Set window parameters
        setDefaultCloseOperation ( JFrame.EXIT_ON_CLOSE );
        setTitle ( "Test Application" );
        setSize ( 800,600 );
        setVisible ( true );
    }

    public static void main ( String[] args ) throws IOException
    {
        new Test102();
    }//End main
}//End Class

2 个答案:

答案 0 :(得分:1)

我的回答是展示一个如何接近所需布局的示例。正如@camickr所述:

  

菜单栏应放置在整个框架的顶部。那里   甚至是一个特殊的方法,setJMenuBar(...)允许你在框架上   这样做。

我认为我们所有可以完全支持此声明。

原始答案

BorderLayout面板上使用CENTER,将JMenuBar放在BorderLayout.NORTH上,带有标签的子面板将放在BorderLayout.CENTER上。 该子面板可以有一个GridBagLayout来自动对齐面板中心的标签。

enter image description here

您可以使用GridBagConstraints.insets在标签之间插入一些空格,在下面的代码中我使用5个像素作为insets.top

代码:

import java.awt.BorderLayout;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.GridLayout;
import java.awt.Insets;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
public class Test
{
    public static void main (String [] a) {
        SwingUtilities.invokeLater (new Runnable () {
            @Override public void run () {
                createAndShowGUI ();
            }
        });
    }
    private static void createAndShowGUI () {
        JFrame frame = new JFrame ("Test Application");
        frame.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
        frame.setContentPane (new MainPanel ());

        // This instruction is used for convenience to let you see the final result, use pack () on your application.
        frame.setSize (800, 600);  
        // frame.pack ();

        frame.setLocationRelativeTo (null);
        frame.setVisible (true);
    }
}
class MainPanel extends JPanel
{
    public MainPanel () {
        super (new BorderLayout ());

        JPanel left = new JPanel (new GridLayout (4, 1));
        for (int i = 1; i <= 4; i ++) left.add (new JButton ("Button " + i));

        JPanel center = new JPanel (new BorderLayout ());

        JMenuBar menuBar = new JMenuBar ();
        JMenu fileMenu = new JMenu ("File Menu", true);
        menuBar.add (fileMenu);
        center.add (menuBar, BorderLayout.NORTH);

        JPanel labelsPanel = new JPanel (new GridBagLayout ());
        labelsPanel.add (new JLabel ("Label 1"));

        GridBagConstraints c = new GridBagConstraints ();
        c.gridy = 1;
        c.insets = new Insets (5, 0, 0, 0);
        labelsPanel.add (new JLabel ("Label 2"), c);

        center.add (labelsPanel, BorderLayout.CENTER);

        JPanel right = new JPanel (new GridLayout (4, 1));
        for (int i = 5; i <= 8; i ++) right.add (new JButton ("Button " + i));

        add (left, BorderLayout.WEST);
        add (center, BorderLayout.CENTER);
        add (right, BorderLayout.EAST);
    }
}

答案 1 :(得分:0)

在中心JPanel中使用垂直BoxLayout。

BoxLayout bx = new BoxLayout();
bx.setAxis(JBoxLayout.VERTICAL);
center.setLayout(bx);