• JFrame,JPanel,JLabel详解


    2016年05月24日 14:06:52  阅读数 22059

    JFrame是一个顶层的框架类,好比一个窗户的框子。也是一个容器类。这个框子可以嵌入几个玻璃窗。

    JPanel是一个容器类,相当于一大玻璃窗。

    JLabel等是一些基础组件,它必须置于某个容器里,类似于窗花、剪纸,必须置于窗户的表面。

    JFrame自带了一个玻璃窗。可以通过语句 Container c=getContentPane();  获得该内容窗格。

    也可以定制一个新的玻璃面板替换原来的自带的玻璃窗,代码如下:

    JPanel jp=new JPanel();

    this.setContentPane(jp);

    JFrame

    java的GUI程序的基本思路是以JFrame为基础,它是屏幕上window的对象,能够最大化、最小化、关闭。

    Swing 的三个基本构造块:标签、按钮和文本字段;但是需要个地方安放它们,并希望用户知道如何处理它们。JFrame 类就是解决这个问题的——它是一个容器,允许程序员把其他组件添加到它里面,把它们组织起来,并把它们呈现给用户。 JFrame 实际上不仅仅让程序员把组件放入其中并呈现给用户。比起它表面上的简单性,它实际上是 Swing 包中最复杂的组件。为了最大程度地简化组件,在独立于操作系统的 Swing 组件与实际运行这些组件的操作系统之间,JFrame 起着桥梁的作用。JFrame 在本机操作系统中是以窗口的形式注册的,这么做之后,就可以得到许多熟悉的操作系统窗口的特性:最小化/最大化、改变大小、移动。

    构造方法

    JFrame()
    构造一个初始时不可见的新窗体。
    JFrame(GraphicsConfiguration gc)
    以屏幕设备的指定 GraphicsConfiguration 和空白标题创建一个 Frame。
    JFrame(String title)
    创建一个新的、初始不可见的、具有指定标题的 Frame。
    JFrame(String title, GraphicsConfiguration gc)
    创建一个具有指定标题和指定屏幕设备的 GraphicsConfiguration 的 JFrame。

    常用方法

    protected void
    addImpl(Component comp, Object constraints, int index)
    添加指定的子 Component。
    protected JRootPane
    createRootPane()
    由构造方法调用,以创建默认的 rootPane。
    protected void
    frameInit()
    由构造方法调用,以适当地初始化 JFrame。
    AccessibleContext
    getAccessibleContext()
    获得与此 JFrame 关联的 AccessibleContext。
    getContentPane()
    返回此窗体的 contentPane 对象
    int
    getDefaultCloseOperation()
    返回用户在此窗体上发起 "close" 时执行的操作。
    getGlassPane()
    返回此窗体的 glassPane 对象。
    getGraphics()
    为组件创建一个图形上下文。
    JMenuBar
    getJMenuBar()
    返回此窗体上设置的菜单栏。
    JLayeredPane
    getLayeredPane()
    返回此窗体的 layeredPane 对象。
    JRootPane
    getRootPane()
    返回此窗体的 rootPane 对象。
    TransferHandler
    getTransferHandler()
    获取 transferHandler 属性。
    static boolean
    isDefaultLookAndFeelDecorated()
    如果新创建的 JFrame 应该由当前外观为其提供 Window 装饰,则返回 true。
    protected boolean
    isRootPaneCheckingEnabled()
    返回是否将对 add 和 setLayout 的调用转发到 contentPane。
    protected String
    paramString()
    返回此 JFrame 的字符串表示形式。
    protected void
    processWindowEvent(WindowEvent e)
    处理此组件上发生的窗口事件。
    void
    从该容器中移除指定组件。
    void
    repaint(long time, int x, int y, int width, int height)
    在 time 毫秒内重绘此组件的指定矩形区域。
    void
    setContentPane(Container contentPane)
    设置 contentPane 属性。
    void
    setDefaultCloseOperation(int operation)
    设置用户在此窗体上发起 "close" 时默认执行的操作。
    static void
    setDefaultLookAndFeelDecorated(boolean defaultLookAndFeelDecorated)
    提供一个关于新创建的 JFrame 是否应该具有当前外观为其提供的 Window 装饰(如边框、关闭窗口的小部件、标题等等)的提示。
    void
    setGlassPane(Component glassPane)
    设置 glassPane 属性。
    void
    setIconImage(Image image)
    设置要作为此窗口图标显示的图像。
    void
    setJMenuBar(JMenuBar menubar)
    设置此窗体的菜单栏。
    void
    setLayeredPane(JLayeredPane layeredPane)
    设置 layeredPane 属性。
    void
    setLayout(LayoutManager manager)
    设置 LayoutManager。
    protected void
    setRootPane(JRootPane root)
    设置 rootPane 属性。
    protected void
    setRootPaneCheckingEnabled(boolean enabled)
    设置是否将对 add 和 setLayout 的调用转发到 contentPane。
    void
    setTransferHandler(TransferHandler newHandler)
    设置 transferHandler 属性,该属性是支持向此组件传输数据的机制。
    void
    只是调用 paint(g)。

    JPanel

    用途

    组件是构成应用程序界面的基本元素,按钮、文本框、进度条等,都是组件。可视化组件又可以分为容器组件和非容器组件,可以在JPanel中放置按钮、文本框等非容器组件,甚至可。以在JPanel中再放置若干个JPanel组件,顶层容器也是容器组件,每一个窗口应用程序中有且只能有一个顶层容器组件。

    构造方法

     

    创建

    JPanel myPanel = new JPanel();

     

    构造方法

    JPanel()               创建具有双缓冲和流布局的新Jpanel

    JPanel(Boolean isDoubleBuffered)    创建具有FlowLayout和指定缓冲策略的新Jpanel

    JPanel(LayoutManager layout)       创建具有指定布局管理器的新缓冲Jpanel

    JPanel(LayoutManager layout, boolean isDoubleBuffered)   创建具有指定布局管理器和缓冲策略的新Jpanel

     

     

    常用方法

     

     

     

    注意点

     

     

    使用示例

    (下列程序来自网上示例程序,JPanel是可以嵌入在JFrame中的)

     

    import javax.swing.*; 
    import java.awt.*; 
    import java.awt.event.*; 
    class Test extends JFrame 

    JPanel contPanel = new JPanel(); 
    JPanel pane1 = new JPanel(); 
    JPanel pane2 = new JPanel(); 
    JButton next = new JButton("下一个"); 
    JButton pre = new JButton("上一个"); 
    public Test() 

    setBounds(0,0,200,300); 
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 

    contPanel.add(pane1); 

    pane1.add(next); 
    pane2.add(pre); 
    next.addActionListener(new ActionListener(){ 
    public void actionPerformed(ActionEvent e) 

    contPanel.remove(pane1); 
    contPanel.add(pane2); 
    contPanel.revalidate(); 
    contPanel.repaint(); 

    }); 
    pre.addActionListener(new ActionListener(){ 
    public void actionPerformed(ActionEvent e) 

    contPanel.remove(pane2); 
    contPanel.add(pane1); 
    contPanel.revalidate(); 
    contPanel.repaint(); 

    }); 

    setContentPane(contPanel); 
    setVisible(true); 

    public static void main(String [] args) 

    new Test(); 

    }

     

     

    Jlabel

    JLabel类中常用的方法

    1.
    public JLabel(String text, Icon icon, int horizontalAlignment)
    构造器。Label标记元件配置在显示区的中央部位,钮面文字为text图标为icon,水平对齐方式为horizontalAlignment,其值可为SwingConstants.LEFT、SwingConstants.CENTER及SwingConstants.RIGHT。
    2.
    public JLable(String text, int horizontalAlignment)
    构造器。Label标记元件配置在显示区的中央部位,钮面文字为text,水平对齐方式为horizontalAlignment。
    3.
    public JLabel(String text)
    构造器。Label标记元件配置在显示区的中央部分,钮面文字为text,左边对齐。
    4.
    public JLabel(Icon image, int horizontalAlignment)
    构造器。Label标记元件配置在显示区的中央部位,钮面图标为icon,水平对齐方式为horizontalAlignment。
    5.
    public JLabel(Icon image)
    构造器。Label标记元件配置在显示区的中央部位,钮面图标为icon,左边对齐。
    6.
    public JLabel()
    构造器。Label标记元件配置在显示萄中央部位,左边对齐。
    7.
    public Icon getIcon()
    返回图标Icon对象。
    8.
    public String getText()
    返回钮面文字字符串。
    9.
    public void setIcon(Icon icon)
    设置图标Icon对象为icon。
    10.
    public void  setText(String text)
     
     
    GUI界面开发演示

    /**

     * 功能:gui界面开发演示

     */

    package com.test1;

    import java.awt.*;

    import javax.swing.*;

    public class Swing_1 extends JFrame{

    //定义按钮组件

    JButton jb1 = null;

    public static void main(String[] args) {

    Swing_1 swing = new Swing_1();

    }

    //构造函数

    public Swing_1(){

    //JFrame是一个顶层容器

    jb1 = new JButton("我是按钮");

    //设置窗体的标题

    this.setTitle("MyFirstSwing");

    //设置大小,按像素

    this.setSize(200,200);

    //添加JButton组建

    this.add(jb1);

    //设置窗体位置

    this.setLocation(400, 200);

    //设置当关闭窗口时,保证jvm也退出

    this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    //设置窗体可见性

    this.setVisible(true);

    }

    }

    /*

     * 边界布局管理器

     * BorderLayout演示

     * 1.继承JFrame

     * 2.定义你需要的各个组建

     * 3.创建组件

     * 4.添加组建

     * 5.对窗体设这

     * 6.显示窗体

     */

    package com.test1;

    import java.awt.BorderLayout;

    import javax.swing.*;

    import javax.swing.*;

    public class Border extends JFrame{

    JButton jb1,jb2,jb3,jb4,jb5;

    public static void main(String[] args) {

    Border border = new Border();

    }

    //构造函数

    public Border(){

    //创建组件

    jb1 = new JButton("中");

    jb2 = new JButton("北");

    jb3 = new JButton("东");

    jb4 = new JButton("南");

    jb5 = new JButton("西");

    //添加各个组件

    //this.add(jb1)没有指定位置的话,会被覆盖

    this.add(jb1, BorderLayout.CENTER);

    this.add(jb2, BorderLayout.NORTH);

    this.add(jb3, BorderLayout.EAST);

    this.add(jb4, BorderLayout.SOUTH);

    this.add(jb5, BorderLayout.WEST);

    //设置窗体属性

    this.setTitle("边界布局");

    this.setSize(300,200);

    this.setLocation(400,200);

    this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    this.setVisible(true);

    }

    }

     

    /**

     * 功能:流式布局案例

     */

    package com.test1;

    import javax.swing.*;

    import java.awt.*;

    public class Flow extends JFrame{

    //定义需要的组件

    JButton jb1,jb2,jb3,jb4,jb5,jb6;

    public static void main(String[] args) {

    Flow flow = new Flow();

    }

    //构造函数

    public Flow(){

    //创建组件

    jb1=new JButton("关羽");

    jb2=new JButton("张飞");

    jb3=new JButton("赵云");

    jb4=new JButton("马超");

    jb5=new JButton("黄忠");

    jb6=new JButton("魏延");

    //添加组件

    this.add(jb1);

    this.add(jb2);

    this.add(jb3);

    this.add(jb4);

    this.add(jb5);

    this.add(jb6);

    //设置布局管理器

    this.setLayout(new FlowLayout(FlowLayout.LEFT));

    //this.setLayout(new FlowLayout(FlowLayout.TRAILING));

    //this.setLayout(new FlowLayout(FlowLayout.RIGHT) );

    //this.setLayout(new FlowLayout(FlowLayout.CENTER));

    //this.setLayout(new FlowLayout(FlowLayout.L));

    //设置窗体属性

    this.setTitle("边界布局");

    this.setSize(300,200);

    this.setLocation(400,200);

    //禁止用户改变窗体大小

    this.setResizable(false);

    this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    //显示

    this.setVisible(true);

    }

    }

     

    /**

     * 功能:网格布局

     */

    package com.test1;

    import java.awt.*;

    import javax.swing.*;

    public class Grid extends JFrame{

    //定义组件

    int size = 9 ;

    JButton jbs[] = new JButton[size];

    public static void main(String[] args) {

    //创建实例

    Grid grid = new Grid();

    }

    public Grid(){

    //创建组件

    for(int i = 0 ; i<9 ; i++){

    jbs[i] = new JButton (String.valueOf(i));

    }

    //设置网格布局(3,3)表示3行3列

    this.setLayout(new GridLayout(3,3,10,10));

    //添加组件

    for(int i = 0 ; i < 9 ; i++){

    this.add(jbs[i]);

    }

    //设置窗体属性

    this.setTitle("网格布局");

    this.setSize(300,200);

    this.setLocation(400,200);

    this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    //显示窗体

    this.setVisible(true);

    }

    }

  • 相关阅读:
    linux shell 获取文件夹全文绝对路径
    Selenium自动化获取WebSocket信息
    Automatically generating nice graphs at end of your Load Test with Apache JMeter and JMeter-Plugins
    卡夫卡与风暴:卡夫卡和风暴的明智比较
    Apache Kafka安全| Kafka的需求和组成部分
    Kafka Schema Registry | 学习Avro Schema
    QT 内部使用函数列表
    QT图标添加
    QT快捷键
    QT软件上架以及自启动
  • 原文地址:https://www.cnblogs.com/yangf428/p/11070706.html
Copyright © 2020-2023  润新知