⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 boxlayoutdemo.java

📁 此文件能系统的介绍Java初级知识
💻 JAVA
字号:
package swing.layout.boxlayout;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Component;
import java.awt.Container;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;

import swing.WindowDestroyer;



/***************************************************************
 *Simple demonstration of BoxLayout manager class and the use of
 *struts to separate components (in this case buttons). (For an 
 *alternative implementation see BoxClassDemo in Display 14.14.)
 ***************************************************************/
public class BoxLayoutDemo extends JFrame 
                           implements ActionListener
{
    public static final int WIDTH = 300;
    public static final int HEIGHT = 200;
    public static final int HORIZONTAL_STRUT_SIZE = 15; 
    public static final int VERTICAL_STRUT_SIZE = 10;

    private JPanel colorPanel;

    public BoxLayoutDemo()
    {
        setSize(WIDTH, HEIGHT);
        addWindowListener(new WindowDestroyer());
        setTitle("Box Demonstration");
        Container content = getContentPane();
        content.setLayout(new BorderLayout());

        colorPanel = new JPanel();
        colorPanel.setBackground(Color.blue);
        content.add(colorPanel, BorderLayout.CENTER);

        //Horizontal buttons at bottom of frame:
        JPanel horizontalPanel = new JPanel();
        horizontalPanel.setLayout(
              new BoxLayout(horizontalPanel, BoxLayout.X_AXIS));
        //X_AXIS 从左向右排列
        Component horizontalStrut = Box.createHorizontalStrut(HORIZONTAL_STRUT_SIZE);
        horizontalPanel.add(horizontalStrut);
       //具有固定尺寸的不可见对象,可以改变宽度大小,但是不能改变高度
        JButton hStopButton = new JButton("Red");
        hStopButton.addActionListener(this);
        horizontalPanel.add(hStopButton);
      
        Component horizontalStrut2 = Box.createHorizontalStrut(HORIZONTAL_STRUT_SIZE);
        horizontalPanel.add(horizontalStrut2);

        JButton hGoButton = new JButton("Green");
        hGoButton.addActionListener(this);
        horizontalPanel.add(hGoButton);

        content.add(horizontalPanel, BorderLayout.SOUTH);

        //Vertical buttons on right side of frame:
        JPanel verticalPanel = new JPanel();
        verticalPanel.setLayout(
              new BoxLayout(verticalPanel, BoxLayout.Y_AXIS));
        //Y_AXIS从上向下排列
        Component verticalStrut =
              Box.createVerticalStrut(VERTICAL_STRUT_SIZE);
        //具有固定水平尺寸的不可见对象,可以改变垂直大小,但是不能改变宽度
        verticalPanel.add(verticalStrut);

        JButton vStopButton = new JButton("Red");
        vStopButton.addActionListener(this);
        verticalPanel.add(vStopButton);

        Component verticalStrut2 =
              Box.createVerticalStrut(VERTICAL_STRUT_SIZE); 
        verticalPanel.add(verticalStrut2);

        JButton vGoButton = new JButton("Green");
        vGoButton.addActionListener(this); 
        verticalPanel.add(vGoButton);

        content.add(verticalPanel, BorderLayout.EAST);
    }

    public void actionPerformed(ActionEvent e) 
    {
       if (e.getActionCommand().equals("Red"))
            colorPanel.setBackground(Color.red);
        else if (e.getActionCommand().equals("Green"))
           colorPanel.setBackground(Color.green);
        else
            System.out.println("Error in button interface.");
    }

    public static void main(String[] args)
    {
        BoxLayoutDemo gui = new BoxLayoutDemo();
        gui.setVisible(true);
    }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -