簡體   English   中英

如何在Swing中創建以下布局?

[英]How can I create the following layout in Swing?

我是Swing的新手,我不了解如何正確地進行布局。 我需要創建以下布局

我嘗試使用網格布局和邊框布局,但是我無法以它在圖片中的設計方式進行顯示。 誰能幫我?

在此處輸入圖片說明

嘗試

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

class Test extends JFrame
{
    public Test()
    {

        //Make a content frame
        Container contentPane = getContentPane();
        Container contentPane2 = getContentPane();
        Container contentPane3 = getContentPane();

        //Create a grid layout - This will go to the left
        contentPane.setLayout ( new GridLayout ( 4, 1 ) );  //4 Rows and 1 Columns
            //Button 1
            contentPane.add ( new JButton ( "Button 1" ) );
            //Button 2
            contentPane.add ( new JButton ( "Button 2" ) );
            //Button 3
            contentPane.add ( new JButton ( "Button 3" ) );
            //Button 4
            contentPane.add ( new JButton ( "Button 4" ) );

        //Create a border layout - This will go in the middle.
        contentPane2.setLayout ( new BorderLayout() );
            //Label - Welcome to my application
            contentPane2.add ( new JLabel ( "Welcome to my application" ) );
            //Image 1
            contentPane2.add  ( new ImageIcon("img/button.png" ) );
            //Change background colour

        //Create a grid layout - This will go to the right
        contentPane3.setLayout ( new GridLayout ( 4, 1 ) ); //4 Rows and 1 Columns
            //Button 5
            contentPane3.add ( new JButton ( "Button 5" ) );
            //Button 6
            contentPane3.add ( new JButton ( "Button 6" ) );
            //Button 7
            contentPane3.add ( new JButton ( "Button 7" ) );
            //Button 8
            contentPane3.add ( new JButton ( "Button 8" ) );


        //Set window parameters
        setTitle ( "Test Application" );
        setSize ( 200, 200 );
        setVisible ( true );
    }

    public static void main ( String[] args )
    {

        Test myFrame = new Test();

    }//End main


}//End Class

請閱讀評論:

import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.io.IOException;
import java.net.URL;
import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;

class Test extends JFrame{

    //when posting code make resources available
    URL url = new URL("http://www.digitalphotoartistry.com/rose1.jpg");

    public Test() throws IOException  {

        //You clearly have three different areas in your design, so start by making:
        JPanel left = new JPanel();
        JPanel center = new JPanel();
        JPanel right = new JPanel();

        //left and right panels holds 4 buttons each. GridLayout will make
        //them occupy equal space. You could also use other layout managers like
        //Box
        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 border layout - This will go in the middle.
        center.setLayout ( new BorderLayout() );
        //Label - Welcome to my application
        center.add ( new JLabel ( "Welcome to my application"),BorderLayout.NORTH);
        //Image 1
        ImageIcon icon= new ImageIcon(ImageIO.read(url));
        center.add ( new JLabel(icon), BorderLayout.CENTER);

        //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 JPanel to content pane which uses Borderlayout by default
        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 ( 200, 200 ); //size set by layout
        pack();
        setVisible ( true );
    }

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM