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);