Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/xamarin/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何将布局修改为这样?_Java_Swing_User Interface - Fatal编程技术网

Java 如何将布局修改为这样?

Java 如何将布局修改为这样?,java,swing,user-interface,Java,Swing,User Interface,我在嵌套Swing布局时遇到问题,我需要创建以下布局,如图所示 左-4个按钮 中间-JMenu栏和2个jLabel 右侧面板-4个按钮 两个侧面板对我来说很好,但我不能让中间的部分工作。我曾尝试在这里使用Borderlayout,但我只能将1项放在北部(JMenu)和1项放在中部(第一个JLabel)。我想也许我可以用另一个JPane细分中心,但它不能正常工作 我还尝试将中间部分设置为网格布局,但当然,所有元素都设置为相同的大小,这样也不行 这就是我想要的样子 import java.awt.*

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

-4个按钮

中间-JMenu栏和2个jLabel

右侧面板-4个按钮

两个侧面板对我来说很好,但我不能让中间的部分工作。我曾尝试在这里使用Borderlayout,但我只能将1项放在北部(JMenu)和1项放在中部(第一个JLabel)。我想也许我可以用另一个JPane细分中心,但它不能正常工作

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

这就是我想要的样子

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

代码

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

在中心JPanel中使用垂直方框布局

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

我回答这个问题的目的是展示一个示例,说明如何接近所需的布局。正如@camickr在上文所述:

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

我认为我们都可以完全支持这一声明

原始答案

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

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

代码:

导入java.awt.BorderLayout;
导入java.awt.GridBagConstraints;
导入java.awt.GridBagLayout;
导入java.awt.GridLayout;
导入java.awt.Insets;
导入javax.swing.JButton;
导入javax.swing.JFrame;
导入javax.swing.JLabel;
导入javax.swing.JMenu;
导入javax.swing.JMenuBar;
导入javax.swing.JPanel;
导入javax.swing.SwingUtilities;
公开课考试
{
公共静态void main(字符串[]a){
SwingUtilities.invokeLater(新的可运行(){
@重写公共无效运行(){
createAndShowGUI();
}
});
}
私有静态void createAndShowGUI(){
JFrame=新JFrame(“测试应用”);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setContentPane(新的主面板());
//为了方便您查看最终结果,请在应用程序上使用pack()。
框架尺寸(800600);
//frame.pack();
frame.setLocationRelativeTo(空);
frame.setVisible(true);
}
}
类主面板扩展了JPanel
{
公共主面板(){
超级(新边界布局());
JPanel left=新的JPanel(新的网格布局(4,1));

对于In i=1;i如何标签应该定位?@ AutoNoSQL SS是的菜单条和J标签,本质上,我不能得到中心块的工作,我可以将中间设置为边框布局,将J菜单Nead和1 JLable放在中间,但是我需要一个J菜单和中间的2个标签。我问,标签应该如何可能,而不是左边和RI。ght按钮栏,因为我仍然不知道。我现在只看到它的外观,但不知道它应该是什么样子,@Antoniosss哦,对了,不,不,图片显示了它应该是什么样子!在中间添加一个新的JPanel并添加标签