JavaGUI 13 JPanel&&JSocll&&JTextArea

6.1 JPanel&&JSocll&&JTextArea


6.1.1 JPanel 面板

package com.muquanyu.lesson05;

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

public class JpanelDemo extends JFrame {
    public JpanelDemo()
    {
        Container contentPane = getContentPane();
        contentPane.setLayout(new GridLayout(2,1,10,10));

        JPanel jpanel = new JPanel(new GridLayout(1,3));
        contentPane.add(jpanel);
        jpanel.add(new JButton("1"));
        jpanel.add(new JButton("1"));
        jpanel.add(new JButton("1"));

        this.setVisible(true);
        this.setSize(500,500);
        this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);

    }

    public static void main(String[] args) {
        JpanelDemo jpanelDemo = new JpanelDemo();
    }

}

面板呀,这还不熟悉吗,用来分组的,我们也把它叫做分组框!提示:JFrame 也是可以用 Layout 布局的。
在这里插入图片描述


6.1.2 JTextArea 文本域

package com.muquanyu.lesson05;

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

public class JScrollDemo extends JFrame {
    public JScrollDemo() {
        Container contentPane = getContentPane();

        JTextArea textArea = new JTextArea(20, 50);
        textArea.setText("GUI编程");
        contentPane.add(textArea);

        this.setVisible(true);
        this.setBounds(100, 100, 300, 350);
        this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    }

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

文本域嘛,之前就讲过,不就是现在 加了个 J嘛。JFrame 的组件 多了一些特性而已。比如说在创建的时候 可以选择对齐方式,可以绝对定位,可以携带图标,等等。只是增加了一些 新的特性而已!!


JTextArea(行数,列数)
在这里插入图片描述


6.1.3 JScrollPane 面板

JScrollPane 它相当于一个面板!!但它不是一个容器!它是一个组件!在它创建的时候,可以写入一个 TextArea 文本域 来让它进行滚动操作。


有滚动条!在编辑框领域是很重要的存在!Java 的GUI编程 必须 创建这个 滚动面板,然后 再去绑定控制对象,才能实现 有滚动条的编辑框!

package com.muquanyu.lesson05;

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

public class JScrollDemo extends JFrame {
    public JScrollDemo() {
        Container contentPane = getContentPane();

        JTextArea textArea = new JTextArea(20, 50);
        textArea.setText("GUI编程");
        JScrollPane jScrollPane = new JScrollPane(textArea);
        contentPane.add(jScrollPane);



        this.setVisible(true);
        this.setBounds(100, 100, 300, 350);
        this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    }

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

语法格式:JScrollPane jScrollPane = new JScrollPane(textArea/绑定的对象/);
contentPane.add(jScrollPane);
在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值