Swing的布局管理器 --转载

2023-11-03

 

11月24日
Swing的布局管理器 --转载
1.先说说Swing中的不一般的组件
JFrame,JDialog,JWindow,JApplet这四个组件是重量级组件,因为要在操作系统中显示窗口画面,必须使用操作系统的窗口资源,所以JFrame 是继承自AWT里面的Frame的,有本地C代码.
JFrame,JDialog,JWindow,JApplet这四个组件是最上层组件,其余的Swing组件都必须依附在这四个组件之上才能显示出来.这四个组件实现了RootPaneContainer接口,RootPaneContainer接口定义了各种容器取得与设置的方法,这里的各种容器指的是JRootPane(虚拟的容器),GlassPane,ContentPane,我们要在最上层组件上加入任何组件只能在GlassPane和ContentPane上面增加,也就是在Layered Pane上面或者在Layered Pane的ContentPane上面增加.
2.RootPaneContainer接口的定义
查看Java的doc,可以知道的(有问题第一反映应该是看JDK的文档,要学会学习)
通过查看文档我们可以发现:
(1).共有五个类实现了RootPaneContainer接口,他们是:JFrame,JApplet,JWindow,JDialog,JInternalFrame.值得注意的是其中的JInternalFrame是轻量级组件,
不能单独显示的
(2).RootPanelContainer有如下的几种方法
Container         getContentPane();
Component      getGlassPane();
JLayeredPane   getLayeredPane();
JRootPane       getRootPane();
void setContentPane( Container  contentPane);
void setGlassPane( Component  glassPane);
void setLayeredPane( JLayeredPane  layeredPane);
 
3.JRootPane的介绍
请大家自己查看JDK的文档,不要偷懒.
我就大概谈一下:
JRootPane是由Glass Pane与LayeredPane组成的,是虚拟的容器,不能在上面加任何组件.
有好同志会问不能加任何组件那用来干嘛的??
因为它是用来让最上层组件能加入别的组件的。几个最上层组件里面都有JRootPane组件。
4.Layered Pane是什么东东??
很多写过Swing程序的新手都没有接触到Layered Pane,和他们接触最多的就是Content Pane了,那么Layered Pane是什么东东呢??Layered Pane就像家中放鞋子的鞋架,有很多层,而ContetPane则是其中的一层,一般我们只要对这一层进行操作就行了!!
 
 5.Layout Manager的版面管理器
(1).BorderLayout

 使用可以去看JDK文档,

package MyJava.Base;

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

public class FrameBorderLayout extends JFrame

 private JPanel jPanel;
 private BorderLayout bl;
 private JButton button1=new JButton("Center");
 private JButton button2=new JButton("East");
 private JButton button3=new JButton("West");
 private JButton button4=new JButton("South");
 private JButton button5=new JButton("North");
 
 FrameBorderLayout()
 { 
  super("This is an example of FrameBorderLayout");
  setSize(400,300);
  
  bl=new BorderLayout();
  
  jPanel=new JPanel();
  jPanel.setLayout(bl);
  jPanel.add(button1,BorderLayout.CENTER);
  jPanel.add(button2,BorderLayout.EAST);
  jPanel.add(button3,BorderLayout.WEST);
  jPanel.add(button4,BorderLayout.SOUTH);
  jPanel.add(button5,BorderLayout.NORTH);
 
  this.setContentPane(jPanel);
 
 }
 public static void main(String[] args)
 { 
  FrameBorderLayout fr=new FrameBorderLayout();
  fr.show();
  fr.addWindowListener(new WindowAdapter()
     {
      public void windowClosing(WindowEvent e)
      { 
       System.exit(0); 
       
      } 
     }
        );
 
 }

 

}
程序运行的结果:

 

(2).FlowLayout

package MyJava.Base;

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

public class FrameFlowLayout extends JFrame
{
    private JButton JButton1=new JButton("Flow1");
    private JButton JButton2=new JButton("Flow2");
    private JButton JButton3=new JButton("Flow3");
    private JButton JButton4=new JButton("Flow4");
    private JButton JButton5=new JButton("Flow5");
   
    public FrameFlowLayout()
    {
        super("This is an example of FrameFlowLayout");
        setSize(400,300);
       
        FlowLayout f1=new FlowLayout(FlowLayout.TRAILING,10,10);
        JPanel jpanel1=new JPanel();
        jpanel1.setLayout(f1);
        jpanel1.add(JButton1);
        jpanel1.add(JButton2);
        jpanel1.add(JButton3);
        jpanel1.add(JButton4);
        jpanel1.add(JButton5);
        this.setContentPane(jpanel1);
        }
    public static void main(String args[]){
        FrameFlowLayout ff1=new FrameFlowLayout();
        ff1.show();
        ff1.addWindowListener(new WindowAdapter(){
        public void windowClosing(WindowEvent e){
            System.exit(0);
            }
        });
        }
    }

 

运行结果:

 

(3).GridLayout

 

package MyJava.Base;

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


public class FrameGridLayout extends JFrame
{
    private JPanel jpanel1;
    private GridLayout g1;
    private JButton JButton1=new JButton("Flow1");
    private JButton JButton2=new JButton("Flow2");
    private JButton JButton3=new JButton("Flow3");
    private JButton JButton4=new JButton("Flow4");
    private JButton JButton5=new JButton("Flow5");
    private JButton JButton6=new JButton("Flow6");
    public FrameGridLayout()
    {
        super("FrameGridLayout");
        setSize(300,120);
        g1=new GridLayout(2,3,10,10);
        jpanel1=new JPanel();
        jpanel1.setLayout(g1);
        jpanel1.add(JButton1);
        jpanel1.add(JButton2);
        jpanel1.add(JButton3);
        jpanel1.add(JButton4);
        jpanel1.add(JButton5);
        jpanel1.add(JButton6);
        this.setContentPane(jpanel1);
        }
    public static void main(String args[])
    {
        FrameGridLayout fg1=new FrameGridLayout();
        fg1.show();
        fg1.addWindowListener(new WindowAdapter(){
        public void windowClosing(WindowEvent e){
            System.exit(0);
            }
        });
        }
    }

 

(4).GridBagLayout

package MyJava.Base;

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

public class FrameGridBagLayout extends JFrame
{
    private JPanel jpanel1=new JPanel();
    private GridLayout g1;
    private JButton JButton1=new JButton("First");
    private JButton JButton2=new JButton("Second");
    private JButton JButton3=new JButton("Third");
    private JButton JButton4=new JButton("Fourth");
    private JButton JButton5=new JButton("Fifth");
    private JButton JButton6=new JButton("Sixth");
    private JButton JButton7=new JButton("Seventh");
 
    public FrameGridBagLayout()
    {
        super("FrameGridBagLayout");
        setSize(300,150);
        GridBagLayout gb1=new GridBagLayout();
        jpanel1.setLayout(gb1);
        GridBagConstraints gbc=new GridBagConstraints();
        gbc.fill=GridBagConstraints.BOTH;
        gbc.gridwidth=1;
        gbc.gridheight=1;
        gbc.gridx=0;
        gbc.gridy=0;
        jpanel1.add(JButton1,gbc);
        gbc.gridx=1;
        jpanel1.add(JButton2,gbc);
        gbc.gridx=2;
        jpanel1.add(JButton3,gbc);
        gbc.gridx=0;
        gbc.gridy=1;
        gbc.gridwidth=3;
        jpanel1.add(JButton4,gbc);
        gbc.gridy=2;
        gbc.gridwidth=1;
        jpanel1.add(JButton5,gbc);
        gbc.gridx=1;
        gbc.gridwidth=2;
        gbc.gridheight=2;
        jpanel1.add(JButton6,gbc);
        gbc.gridx=0;
        gbc.gridy=3;
        gbc.gridwidth=1;
        gbc.gridheight=1;
        jpanel1.add(JButton7,gbc);
        this.setContentPane(jpanel1);
        }
    public static void main(String args[]){
        FrameGridBagLayout fg1=new FrameGridBagLayout();
        fg1.show();
        fg1.addWindowListener(new WindowAdapter(){
        public void windowClosing(WindowEvent e){
            System.exit(0);
            }
        });
        }
    }

 

(5).CardLayout

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class FrameCardLayout extends JFrame{
    private JPanel jpanel1;
    private CardLayout c1;
    private JButton jButton1=new JButton("Layer1");
    private JButton jButton2=new JButton("Layer2");
    private JButton jButton3=new JButton("Layer3");
    public FrameCardLayout(){
        super("frameCardlayout");
        setSize(300,120);
        jpanel1=new JPanel();
        c1=new CardLayout();
        jpanel1.setLayout(c1);
        jpanel1.add(jButton1,"First Layer");
        jpanel1.add(jButton2,"Second Layer");
        jpanel1.add(jButton3,"Third Layer");
        c1.show(jpanel1,"Second Layer");
        this.setContentPane(jpanel1);
       
        }
    public static void main(String arg[]){
        FrameCardLayout fg1=new FrameCardLayout();
        fg1.show();
         fg1.addWindowListener(new WindowAdapter(){
        public void windowClosing(WindowEvent e){
            System.exit(0);
            }
        });
        }
    }

 

(6).BoxLayout

 

这个程序以后补上

 

6.如果觉得版面管理器学不了可以不使用,直接指定组件的绝对位置和大小

package MyJava.Base;

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


class NonLayoutPane extends JPanel

 public NonLayoutPane()
 { 
  
  this.setLayout(null);
  
  JButton b1=new JButton("one");
  b1.setBounds(15,10,80,30);
  add(b1);
  
  JButton b2=new JButton("two");
  b1.setBounds(80,50,90,40);
  add(b2);
  
  
  
 
 
 
 }

 


}

class NonLayoutFrame extends JFrame

 public NonLayoutFrame()
 { 
  this.setTitle("NonLayoutTest");
  setSize(200,130);
  this.getContentPane().add(new NonLayoutPane());
  this.addWindowListener(new WindowAdapter()
        { 
         public void windowClosing(WindowEvent evt)
          { 
          
           System.exit(0);
          }
        
        }   
        );
 
 
 }

 

}
public class NonLayoutTest

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

 

}

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

Swing的布局管理器 --转载 的相关文章

  • 将 Excel 读取到从第 5 行开始并包括标题的 Python 数据框

    我有一个 Excel 工作簿 它在打开时运行一些 vba 刷新数据透视表并执行其他一些操作 然后我希望将数据透视表刷新的结果导入到 python 中的数据框中以进行进一步分析 import xlrd wb xlrd open workboo
  • Java JScrollBar设计

    我想自定义 JScrollBar 设计 我使用 Mac 使用 eclipse 开发应用程序 我已经尝试过scrollPane getVerticalScrollBar setBackground Color BLACK 但什么也没发生 我的
  • std::setw 如何处理字符串输出?

    我正在尝试使用设置宽度setw但是 对于将字符串输出到输出文件 我无法使其工作 我有下面的例子 setw example include
  • 获取对 JOptionPane 静态方法创建的对象的引用

    我想知道是否可以获取对由 JOptionPane 的静态方法之一 例如 showMessageDialog 创建的 JDialog 对象的引用 我打算修改对话框在屏幕上出现的位置 更具体地说 我希望对话框默认显示在主应用程序窗口的左上角 而
  • 如何将字典转换为字符串

    我正在尝试使用提供的解决方案here https stackoverflow com questions 5192753 how to get the number of occurrences of each character usin
  • 字符串替换多个值

    我有一个看起来像这样的字符串 布拉布拉 亚达亚达 布拉布拉 亚达亚达 有没有办法只替换前两个 或最后两个 以便我可以获得下一个输出 Bla bla a href link1 yada yada a bla bla yada yada 如有必
  • 如何在Python中的字符串中插入变量值

    这是一个简单的例子 amount1 input Insert your value amount2 input Insert your value print Your first value is amount1 your second
  • 设置滚动条粗细

    有没有办法调整滚动条的粗细JScrollPane 默认值有点笨拙 一个快速但又肮脏的解决方案是将宽度 高度明确设置为例如10 像素通过 jScrollPane getVerticalScrollBar setPreferredSize ne
  • 如何单击网络浏览器控件中的按钮?

    例如 使用代码并且没有用户输入 我如何让我的程序点击 google 上的 搜索 按钮 假设我已经填写了搜索框并且位于 google com webBrowser1 Navigate http www google com 如果你有一个ID用
  • 如何在Python字符串中替换括号及其中的文本

    我有两个这样的字符串 string1 Today I went to the market to pick up some fruit string2 Today I went to school to learn algebra and
  • python中的StringIO实际用途是什么?

    StringIO到底是用来做什么的 我一直在互联网上寻找一些例子 然而 几乎所有的例子都非常抽象 他们只是展示 如何 使用它 但它们都没有表明 为什么 和 在什么情况下 应该 将使用它 附注不要与 stackoverflow 上的这个问题混
  • 类返回语句不打印任何输出

    我正在学习课程 但遇到了问题return语句 它是语句吗 我希望如此 程序什么也没有打印出来 它只是结束而不做任何事情 class className def createName self name self name name def
  • 找不到符号assertEquals

    我正在尝试为计算器编写第一个单元测试 但 NetBeans 说它找不到该符号assertEquals和注释 Test 我应该包括一些东西吗 我正在使用 NetBeans 7 3 1 和 W7 package calculator impor
  • 如何在 Swift 中将文件名与文件扩展名分开?

    给定包中文件的名称 我想将该文件加载到我的 Swift 应用程序中 所以我需要使用这个方法 let soundURL NSBundle mainBundle URLForResource fname withExtension ext 无论
  • 如何使用剪辑来减少绘画时间?

    我正在尝试使用 Clip 来减少 CPU 负载 但剪辑在屏幕上留下了一些我似乎无法摆脱的垃圾 另外 打开和关闭剪辑似乎对 CPU 负载没有影响 在任一情况下 大部分时间似乎都花在重绘管理器和绘制缓冲图像上 import static jav
  • 如何从父类函数访问子类中定义的常量?

    我从 php net 看到这个例子 但 c MY CONST 仅在 5 3
  • 如何在读取文件期间从每一行中删除换行符? [复制]

    这个问题在这里已经有答案了 我正在从包含一个 字 行的文件中读取行 例如 dog cat person tree 每个单词还包含一个换行符 n特点 我想将它们读入列表并丢弃换行符 我设计的方法是阅读readlines 然后将列表处理为str
  • 如何包含字符串标头?

    我正在尝试了解strings 但不同的来源告诉我要包含不同的标头 有人说用
  • R 中的字符串作为函数参数

    数据框chocolates列出了糖果的类型以及每种糖果的一组评级 ID sweetness filling crash snickers 0 67 0 55 0 40 milky way 0 81 0 53 0 56 我正在编写一个函数 它
  • 如何在button.addTarget操作中发送多个按钮?斯威夫特3

    如何将button和button2发送到我的pressButton2函数中 当用户触摸按钮2时 我需要更改按钮和按钮2的颜色 当我的 button2 addTarget 看起来像这样时 我收到错误 表达式列表中存在预期表达式 import

随机推荐

  • 计算机组成原理与系统结构期末复习题(2)

    计算机组成原理与系统结构 选择题 1 冯 诺依曼机工作的基本方式的特点是 B A 多指令流单数据流 B 按地址访问并顺序执行指令 C 堆栈操作 D 存贮器按内容选择地址 2 完整的计算机应包括 D A 运算器 存储器 控制器 B 外部设备和
  • VS环境下Qt工程.UI文件不生成头文件的问题

    在VS环境下创建的Qt工程会出现 UI文件不生成头文件的问题 可以通过右击 ui文件 点击编译生成头文件 但是 我创建的工程的 ui文件不能编译 右键编译选项是灰的 这种情况下 我想到的办法是 重新添加一个带UI文件的GUI类 与工程同名
  • openmvg2.0编译与使用

    目录 写在前面 获取代码 github 网盘 编译 使用 稠密重建 参考 完 写在前面 1 openmvg是一个用于实现structure from motion的开源库 实现了完整的sfm pipeline 并有说明文档 https op
  • css文本换行加省略号

    overflow hidden text overflow ellipsis white space nowrap 可以显示的行数 超出部分用 表示 webkit box orient vertical 控制显示行数 webkit line
  • 某企业每月给其A、B、C 和D 四个门店一共发送6 个集装箱的某种货物,如果各门店出售该种货物的利润(万元)如下表:

    某企业每月给其A B C 和D 四个门店一共发送6 个集装箱的某种货物 如果各门店出售该种货物的利润 万元 如下表 试求这6 箱货物如何分配给各门店 才能获得最大总利润 解题思路 将问题按卖场分为四个阶段 将A B C D四个卖场分别编号为
  • Angular学习---filter过滤器(管道pipe)

    angular核心概念 过滤器 Filter 应用 如当后台传入数据用1 2 代表性别 可以用过滤器进行更改 如上 练习 创建员工信息列表 中 的员工性别 过滤器在angular2以后改名为 管道 pipe 详见新建的sex pipe ts
  • 【H.264/AVC视频编解码技术详解】二十二、熵编码(7):语法元素的CABAC解析

    H 264 AVC视频编解码技术详解 视频教程已经在 CSDN学院 上线 视频中详述了H 264的背景 标准协议和实现 并通过一个实战工程的形式对H 264的标准进行解析和实现 欢迎观看 纸上得来终觉浅 绝知此事要躬行 只有自己按照标准文档
  • 【Java基础】环境搭建+简介(一)

    JAVA语言背景介绍 Java 语言是美国Sun公司 Stanford University Network 在1995年推出的计算机语言 Java之父 詹姆斯高斯林 James Gosling Java语言的发展史 Sun公司于1995年
  • Qt信号和槽绑定实例,点击pushbutton按钮触发QLabel文本显示和关闭

    功能简介 点击qt界面中的open按钮在界面中显示文本 同时按钮变为close按钮 再点击close按钮可以关闭显示文本 同时按钮变为open按钮 继续点击open按钮 关键内容 关联按钮按事件和信号识别槽 connect this gt
  • 2.1.Perl运行方式

    Perl运行方式 B站视频教程 菜鸟学生信 课程参考书 小骆驼书 第六版 1 运行 Perl Perl 有不同的执行方式 1 1 交互式 perl e
  • 压测工具之Locust

    前言 说起压测 我就用过Jmeter 而且仅是简单使用 好用性能强大 最近接触了一个python提供的压测框架Locust 翻译为蝗虫 蝗虫过之 寸草不生 哈哈哈 我感觉很贴切 首先 我们分析一下市面上几种工具的特性 LoadRunner
  • 对FPKM/RPKM以及TPM的理解

    虽然一直在接触FPKM RPKM以及TPM 但是仅仅是知道它们是转录本定量的值 并未究其根本 最近看了几篇文献 对其深层次的含义有了进一步的理解 因而在这里记录下来 首先来看FPKM RPKM的起源 在RNA Seq中 最简单的定量基因表达
  • 现代后端开发者必备技能-2018版

    2019独角兽企业重金招聘Python工程师标准 gt gt gt 今天的Web开发与几年前完全不同 有很多不同的东西可以很容易地阻止任何人进入Web开发 这是我们决定制作这些循序渐进的视觉指南的原因之一 这些指南展示了更大的图景 并让任何
  • 「万向区块链专家观点」深度解析“区块链+物联网”与新基建

    本文从属于万向区块链 融合创新 系列行业研究报告 作者为万向区块链首席经济学家邹传伟博士 2020年10月27日至28日 万向区块链实验室将主办主题为 融合创新 的第六届区块链全球峰会 近期 万向区块链蜂巢学院B站直播间也将特别推出 融合创
  • 带你读源码:四大视角多维走读区块链源码

    引子 区块链作为 新基建 的重要组成部分 越来越受技术爱好者关注 区块链极客信奉 code is law 相信通过代码可以构筑一个可信的世界 而作为一门综合学科技术 区块链建立在数学 密码学 计算机原理 分布式网络和博弈论等众多基础学科之上
  • scp命令下载整个目录

    linux文件夹下载 1 压缩文件夹 tar cvzf chinese tar gz usr share fonts chinese 2 下载 sz chinese tar gz 从Linux服务器下载文件夹到本地 1 使用scp命令 sc
  • Python实现快速傅里叶变换(FFT)

    相关文章 傅立叶级数展开初探 Python 这里做一下记录 关于FFT就不做介绍了 直接贴上代码 有详细注释的了 import numpy as np from scipy fftpack import fft ifft import ma
  • 【存档】CSDN社区之星专访:我的蜕变之路

    最近要接受一家技术媒体的专访 想起 10年前CSDN也采访过我一次 于是网上搜了搜 遗憾的是 原文链接已经失效了 幸好有位叫 阿飞冲冲冲 的网友转载了 我再转过来 顺便也读一遍 找找 初心 摘要 社区之星第49期采访了爱奇艺研发总监陆其明
  • angular6学习(九):数据绑定到事件

    实现功能 将html页面中文本框的输入的内容传递到ts文件 然后在html文件中显示ts文件中的这个内容 html文件 一 绑定到事件 ts文件 显示结果 二 双向绑定 html app module ts
  • Swing的布局管理器 --转载

    11月24日 Swing的布局管理器 转载 1 先说说Swing中的不一般的组件 JFrame JDialog JWindow JApplet这四个组件是重量级组件 因为要在操作系统中显示窗口画面 必须使用操作系统的窗口资源 所以JFram