java Swing 图形化界面开发关于 JFrame的例子

这个博客展示了如何使用Java Swing库创建一个包含不同布局管理器(FlowLayout、GridLayout、BorderLayout)的JTabbedPane。通过示例代码,详细解释了如何在每个选项卡中添加组件,并调整它们的布局。
摘要由CSDN通过智能技术生成

结果图
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

代码

import javax.swing.*;
import java.awt.*;
public class test {
    static class Example11_4 extends JFrame{
        JTabbedPane pane; //定义属性 选项卡

        //构造方法
        public Example11_4(){
            setBounds(100,100,500,300);
            setVisible(true);
            pane = new JTabbedPane(JTabbedPane.LEFT);
            pane.add("观看FlowLayount",new FlowLayoutJPane1());
            pane.add("观看FridLayout",new GridLayoutJPane1());
            pane.add("观看BorderLayout",new BorderLayoutJPane1());
            pane.validate();//验证
            add(pane,BorderLayout.CENTER);
            validate();
            setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);


        }

    }

    public static class FlowLayoutJPane1 extends JPanel
    {
        FlowLayoutJPane1(){
            add(new JLabel("FlowLayout布局的面板"));
            add(new JButton(new ImageIcon("src/14.jpg")));
            add(new JScrollPane(new JTextArea(12,15)));
        }
    }

    static class  BorderLayoutJPane1 extends JPanel{
        JButton bSouth,bNorth,bEast,bWest;
        JTextArea bCenter;

        BorderLayoutJPane1(){
            setLayout(new BorderLayout());
            bSouth = new JButton("南");
            bNorth = new JButton("北");
            bEast = new JButton("东");
            bWest = new JButton("西");
            bCenter = new JTextArea("中心");
            add(bNorth,BorderLayout.NORTH);
            add(bSouth,BorderLayout.SOUTH);
            add(bWest,BorderLayout.WEST);
            add(bEast,BorderLayout.EAST);
            add(bCenter,BorderLayout.CENTER);
            validate();
        }
    }

    static class GridLayoutJPane1 extends JPanel{
        GridLayoutJPane1(){
            GridLayout gridLayout = new GridLayout(12,12);//网格布局
            setLayout(gridLayout);
            Label label[][]= new Label[12][12];
            for(int i =0;i<12;i++)
            {
                for(int j =0;j<12;j++)
                {
                    label[i][j] = new Label();//为什么?
                    if((i+j)%2 == 0)
                        label[i][j].setBackground(Color.black);
                    else
                        label[i][j].setBackground(Color.white);

                    add(label[i][j]);
                }
            }
        }
    }


    public static void main(String args[]){
        new Example11_4();
    }





}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值