JavaNote 3.4容器

本文介绍了Java图形用户界面的基本组成元素Component,并详细解析了几种常用的布局管理器,包括BorderLayout、FlowLayout、CardLayout和GridLayout的工作原理及使用方法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

一、图解

说明:

1、Java的图形用户界面的最基本组成部分是Component,一般的Component对象不能独立地显示出来,必须放在某一Container对象中才可以显示出来。

2、Container是Component子类,Container对象可以容纳别的Component对象。

3、Container也可以当成Component添加至其他Container中。

4、Windows其对象表示自由停泊的顶级窗口。Panel可以容纳其他的Component,但不能独立存在,必须添加到其他的Container中,如Windows或Applet

二、布局管理器

1、BorderLayout实例

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

public class Test35 {
    static JFrame jFrame;
    public static void main(String args[]){
        jFrame = new JFrame("登录");
        /*
        Container container = jFrame.getContentPane();
        container.setVisible(true);
        构造Frame对象时,会默认有个Pane,Jframe对象的setLayout其实是将默认的Pane划分为5部分,add方法
        其实是将组建添加至默认的Pane中,所以代码下方使用jframe.setBackground后无法看到颜色,其实是被默认的
        Pane挡住了
         */
        JPanel panel =(JPanel) jFrame.getContentPane();
        panel.setOpaque(true);//设置默认面板为透明
        panel.setSize(20,20);
        panel.setVisible(true);
        panel.setBackground(Color.green);
        jFrame.setLayout(new BorderLayout(5,10));
       // jFrame.add(new JButton("东"),BorderLayout.EAST);
        JPanel jPanel = new JPanel();
        jPanel.setLayout(new BorderLayout());
        jPanel.add(new JButton("dong"),BorderLayout.EAST);
        jPanel.add(new JButton("xi"),BorderLayout.WEST);
        jPanel.add(new JButton("nan"),BorderLayout.SOUTH);
        jPanel.add(new JButton("bei"),BorderLayout.NORTH);
        panel.add(jPanel,BorderLayout.EAST);
        jFrame.add(new JButton("西"),BorderLayout.WEST);
        jFrame.add(new JButton("南"),BorderLayout.SOUTH);
        jFrame.add(new JButton("北"),BorderLayout.NORTH);
        jFrame.add(new JButton("中"),BorderLayout.CENTER);
        Toolkit toolkit = Toolkit.getDefaultToolkit();//获取默认工具包
        Dimension dimension = toolkit.getScreenSize();//获取默认屏幕尺寸
        jFrame.setVisible(true);//设置容器是否可见
        jFrame.setLocation(dimension.width/4,dimension.height/4);//设定容器位置
        jFrame.setSize(dimension.width/2,dimension.height/2);//设定容器大小
        jFrame.setBackground(Color.blue);//设定容器背景
        jFrame.setResizable(true);//设定容器是否可以改变大小
        //应用程序关闭后,其实在进程中并没有完全关闭,需要此设置后才可完全关闭
        jFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    }
}

2、FlowLayout

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

public class Test36 {
    static JFrame jFrame;
    static JPanel jPanel;
    static JButton jButton,jButton1,jButton2;
    public static void main(String args[]){
        Toolkit toolkit = Toolkit.getDefaultToolkit();//获取默认工具包
        Dimension dimension = toolkit.getScreenSize()   ;//获取默认屏幕尺寸
        jPanel = new JPanel();
        jFrame = new JFrame("测试");
        jButton = new JButton("按钮1");
        jButton1 = new JButton("按钮2");
        jButton2 = new JButton("按钮3");
        jPanel.add(jButton);
        jPanel.add(jButton1);
        jPanel.add(jButton2);
        jFrame.add(jPanel);
      //  jPanel.setVisible(false);jPanel默认的布局是FlowLayout,把panl加入到Frame中不用再设定panl是否可见了
        jFrame.setVisible(true);//设置容器是否可见
        jFrame.setLocation(dimension.width/4,dimension.height/4);//设定容器位置
        jFrame.setSize(dimension.width/2,dimension.height/2);//设定容器大小
        jFrame.setDefaultCloseOperation(jFrame.EXIT_ON_CLOSE);
    }
}

3、CardLayout

import javax.swing.*;
import java.awt.*;
/*
CardLayout是卡片布局管理器,只按顺序显示第一个组件,CardLyout封装了一些方法,可以显示指定的卡片
 */
public class Test37 {
    static JFrame jFrame;
    static JButton jButton,jButton1,jButton2;
    public static void main(String args[]){
        Toolkit toolkit = Toolkit.getDefaultToolkit();
        Dimension dimension = toolkit.getScreenSize();
        jFrame = new JFrame("测试");
        jButton = new JButton("按钮1");
        jButton1 = new JButton("按钮2");
        jButton2 = new JButton("按钮3");
        CardLayout cardLayout = new CardLayout();
        jFrame.setLayout(cardLayout);
        jFrame.add(jButton);
        jFrame.add(jButton1);
        jFrame.add(jButton2);
        cardLayout.next(jFrame.getContentPane());//显示下一个卡片
        jFrame.setSize(dimension.width/2,dimension.height/2);
        jFrame.setLocation(dimension.width/4,dimension.height/4);
        jFrame.setVisible(true);
        jFrame.setDefaultCloseOperation(jFrame.EXIT_ON_CLOSE);

    }
}

四、GridLayout

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

public class Test38 extends JFrame{
   static JFrame jFrame;
   static JButton jButton,jButton1,jButton2;
   public static void main(String args[]){
       Toolkit toolkit = Toolkit.getDefaultToolkit();
       Dimension dimension = toolkit.getScreenSize();
       jFrame = new JFrame("登录");
       jFrame.setLayout(new GridLayout());
       /*
       GridLayout 类是一个布局处理器,它以矩形网格形式对容器的组件进行布置。容器被分成大小相等的矩形,一个矩形中放置一个组件
       可以在参数中设定几行几列
        */
       jButton = new JButton("按钮1");
       jButton1 = new JButton("按钮2");
       jButton2 = new JButton("按钮3");

       jFrame.add(jButton);
       jFrame.add(jButton1);
       jFrame.add(jButton2);

       jFrame.setLocation(dimension.width/4,dimension.height/4);
       jFrame.setSize(dimension.width/2,dimension.height/2);
       jFrame.setVisible(true);
       jFrame.setResizable(true);
       jFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
   }
}

### Java 中 3.4F 的含义 在 Java 中,`3.4F` 表示一个单精度浮点数(`float` 类型)。Java 默认将带有小数点的数字解释为双精度浮点数(`double` 类型),因此为了明确指定该数值为 `float` 类型,需要在其后添加字母 `F` 或 `f`。这种标记方式告诉编译器将该值视为单精度浮点数而非双精度浮点数[^1]。 例如: ```java float num = 3.4F; // 正确,明确指定了 float 类型 ``` 如果省略 `F` 或 `f`,则会引发类型不匹配的问题,因为默认情况下 `3.4` 被视为 `double` 类型,而 `float` 类型变量无法直接赋值为 `double` 类型的值,除非进行显式的类型转换[^2]。 ### 浮点数定义 浮点数是一种用于表示实数的数据类型,支持小数部分的数值存储。在 Java 中,浮点数分为两种类型:`float` 和 `double`。`float` 是单精度浮点数,占用 4 字节(32 位),而 `double` 是双精度浮点数,占用 8 字节(64 位)。根据 IEEE 754 标准,`float` 类型使用 23 位表示有效倍数(小数部分),其余部分用于符号和指数。 ### 示例代码 以下是一个简单的示例,展示如何定义和使用 `float` 类型: ```java public class Main { public static void main(String[] args) { float singlePrecision = 3.4F; // 使用 F 指定为 float 类型 double doublePrecision = 3.4; // 默认为 double 类型 System.out.println("Single Precision: " + singlePrecision); System.out.println("Double Precision: " + doublePrecision); } } ``` 运行结果: ``` Single Precision: 3.4 Double Precision: 3.4 ``` 尽管输出相同,但内存中存储的方式不同,`singlePrecision` 占用 4 字节,而 `doublePrecision` 占用 8 字节[^3]。 ### 注意事项 当处理浮点数时,需要注意精度问题。由于浮点数的二进制表示方式,某些十进制小数无法精确表示,可能导致计算结果出现微小误差。例如: ```java float a = 0.1F; float b = 0.2F; System.out.println(a + b); // 输出可能不是精确的 0.3 ``` 这种现象是浮点数精度问题的一部分,可以通过使用 `BigDecimal` 类来解决高精度需求[^4]。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值