GUI编程
组建
窗口
弹窗
面板
文本框
列表框
按钮
图片
监听事件
鼠标
键盘事件
破解工具
1、简介
GUI的核心技术:Swing AWT
为什么不流行?
界面不美观。
需要jre环境。(没必要为一个5M的小游戏下载几百M的jre)
但是学了java的GUI编程,有助于了解MVC架构,了解监听。
2、AWT
2.1、Awt介绍
包括很多类和接口
元素:窗口,按钮,文本框
java.awt
2.2、Frame
import java.awt.*;
//GUI的第一个程序
public class TestFrame {
public static void main(String[] args) {
// 不会看源码
Frame frame = new Frame("我的第一个java图形界面窗口");
// 需要设置可见性
frame.setVisible(true);
// 设置窗口大小
frame.setSize(400, 400);
// 设置背景颜色 Color
frame.setBackground(Color.yellow);
// 弹出的初始位置
frame.setLocation(300, 300);
// 设置大小固定
frame.setResizable(false);
}
}
进一步封装
import java.awt.*;
//GUI的第一个程序
public class TestFrame {
public static void main(String[] args) {
MyFrame myFrame1 = new MyFrame(100, 100, 200, 200, Color.BLUE);
MyFrame myFrame2 = new MyFrame(300, 100, 200, 200, Color.yellow);
MyFrame myFrame3 = new MyFrame(100, 300, 200, 200, Color.red);
MyFrame myFrame4 = new MyFrame(300, 300, 200, 200, Color.green);
}
}
class MyFrame extends Frame{
static int id = 0;
public MyFrame(int x, int y, int w, int h, Color color){
super("MyFrame"+(++id));
setBackground(color);
setBounds(x, y, w, h);
setVisible(true);
}
}
2.3、面板Panel
import java.awt.*;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
public abstract class TestPanel {
public static void main(String[] args) {
Frame frame = new Frame();
Panel panel = new Panel();
// 设置布局
frame.setLayout(null);
// 坐标
frame.setBounds(300, 300, 200, 200);
frame.setBackground(new Color(168, 123, 82));
// panel设置坐标,相对于Frame
panel.setBounds(50,50,100,100 );
panel.setBackground(new Color(68, 52, 134));
frame.add(panel);
frame.setVisible(true);
// 监听事件,监听窗口关闭事件 System.exit(0)
// 适配器模式
frame.addWindowListener(new WindowAdapter() {
// 窗口点击关闭时需要做的事情
@Override
public void windowClosing(WindowEvent e) {
// 结束程序
System.exit(0);
}
});
}
}
2.4、布局管理器
流式布局
import java.awt.*;
public class TestFlowLayout {
public static void main(String[] args) {
Frame frame = new Frame();
// 组建-按钮
Button button1 = new Button("button1");
Button button2 = new Button("button2");
Button button3 = new Button("button3");
// 设置为流式布局
//frame.setLayout(new FlowLayout());
//frame.setLayout(new FlowLayout(FlowLayout.LEFT));
frame.setLayout(new FlowLayout(FlowLayout.RIGHT));
frame.setSize(200,200);
// 把按钮添加上去
frame.add(button1);
frame.add(button2);
frame.add(button3);
frame.setVisible(true);
}
}
东西南北中
import java.awt.*;
public class TestBorderLayout {
public static void main(String[] args) {
Frame frame = new Frame("TestBorderLayout");
Button east = new Button("East");
Button west = new Button("West");
Button south = new Button("South");
Button north = new Button("North");
Button center = new Button("Center");
frame.add(east, BorderLayout.EAST);
frame.add(west, BorderLayout.WEST);
frame.add(south, BorderLayout.SOUTH);
frame.add(north, BorderLayout.NORTH);
frame.add(center, BorderLayout.CENTER);
frame.setSize(200,200);
frame.setVisible(true);
}
}
表格布局
import java.awt.*;
public class TestGridLayout {
public static void main(String[] args) {
Frame frame = new Frame("TestGridLayout");
Button btn1 = new Button("btn1");
Button btn2 = new Button("btn2");
Button btn3 = new Button("btn3");
Button btn4 = new Button("btn4");
Button btn5 = new Button("btn5");
Button btn6 = new Button("btn6");
frame.setLayout(new GridLayout(3,2));
frame.add(btn1);
frame.add(btn2);
frame.add(btn3);
frame.add(btn4);
frame.add(btn5);
frame.add(btn6);
frame.pack();
frame.setVisible(true);
}
}
总结:
Frame是一个*窗口。
Panel无法单独显示,必须添加到某个容器中。
-
布局管理器
-
流式
东西南北中
表格
-
大小,定位,背景颜色,定位,监听。
2.5、事件监听
事件监听:当某个事情发生的时候,干什么?
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
public class TestActionEvent {
public static void main(String[] args) {
// 按下按钮,触发一些事件
Frame frame = new Frame();
Button button = new Button();
// 因为,addActionListener()需要一个ActionListener,所以我们需要构造一个ActionListener
MyActionListener myActionListener = new MyActionListener();
button.addActionListener(myActionListener);
frame.add(button,BorderLayout.CENTER);
frame.pack();
windowClose(frame);
frame.setVisible(true);
}
// 关闭窗体事件
private static void windowClose(Frame frame){
frame.addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
}
}
//事件监听
class MyActionListener implements ActionListener{
@Override
public void actionPerformed(ActionEvent e) {
System.out.println("aaa");
}
}
多个按钮共用一个监听器
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class TestActionTwo {
public static void main(String[] args) {
// 两个按钮,实现一个监听
// 开始 停止
Frame frame = new Frame("开始-停止");
Button button1 = new Button("start");
Button button2 = new Button("stop");
// 可以显示的定义触发会返回的命令,如果不显示定义,则会走默认的值
// 可以多个按钮只写一个监听类
button2.setActionCommand("button2_stop");
MyMonitor myMonitor = new MyMonitor();
button1.addActionListener(myMonitor);
button2.addActionListener(myMonitor);
frame.add(button1, BorderLayout.NORTH);
frame.add(button2, BorderLayout.SOUTH);
frame.pack();
frame.setVisible(true);
}
}
class MyMonitor implements ActionListener{
@Override
public void actionPerformed(ActionEvent e) {
if(e.getActionCommand().equals("start")){
System.out.println("开始按钮监听");
}else{
System.out.println("停止按钮监听");
}
}
}
2.6、输入框 TextField 监听
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class TestTest01 {
public static void main(String[] args) {
new MyFrame();
}
}
class MyFrame extends Frame{
public MyFrame(){
TextField textField = new TextField();
add(textField);
// 监听这个文本框输入的文字
MyActionListener2 myActionListener2 = new MyActionListener2();
// 按下enter 就会触发这个输入框的事件
textField.addActionListener(myActionListener2);
// 设置替换编码
textField.setEchoChar('*');
setVisible(true);
pack();
}
}
class MyActionListener2 implements ActionListener{
@Override
public void actionPerformed(ActionEvent e) {
TextField filed = (TextField) e.getSource(); //获得的一些资源,返回的一个对象
System.out.println(filed.getText()); //获得输入框的文本
filed.setText(""); //回车清空
}
}
2.7、简易计算器,组合+内部类回顾复习
oop原则:组合,大于继承!
目前代码:
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class TextText02 {
public static void main(String[] args) {
new Calculator();
}
}
class Calculator extends Frame{
public Calculator(){
// 3个文本框
TextField num1 = new TextField(10);
TextField num2 = new TextField(10);
TextField num3 = new TextField(20);
// 1个按钮
Button button = new Button("=");
button.addActionListener(new MyCalculatorListener(num1, num2, num3));
// 一个标签
Label label = new Label("+");
// 布局
setLayout(new FlowLayout());
add(num1);
add(label);
add(num2);
add(button);
add(button);
add(num3);
pack();
setVisible(true);
}
}
//监听器类
class MyCalculatorListener implements ActionListener{
// 获取三个变量
private TextField num1, num2, num3;
public MyCalculatorListener(TextField num1, TextField num2, TextField num3){
this.num1 = num1;
this.num2 = num2;
this.num3 = num3;
}
@Override
public void actionPerformed(ActionEvent e) {
// 1.获得加数和被加数
int n1 = Integer.parseInt(num1.getText());
int n2 = Integer.parseInt(num2.getText());
// 2.将这个值 + 运算后,放到第三个框
num3.setText(""+(n1+n2));
// 3.清除前两个框
num1.setText("");
num2.setText("");
}
}
完全改造为面向对象写法:
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class TextText02 {
public static void main(String[] args) {
new Calculator().loading();
}
}
class Calculator extends Frame{
// 属性
TextField num1, num2, num3;
// 方法
public void loading(){
num1 = new TextField(10);//字符数
num2 = new TextField(10);//字符数
num3 = new TextField(20);//字符数
Button button = new Button("=");
button.addActionListener(new MyCalculatorListener(this));
Label label = new Label("+");
// 布局
setLayout(new FlowLayout());
add(num1);
add(label);
add(num2);
add(button);
add(button);
add(num3);
pack();
setVisible(true);
}
}
//监听器类
class MyCalculatorListener implements ActionListener{
Calculator calculator = null;
public MyCalculatorListener(Calculator calculator){
this.calculator = calculator;
}
@Override
public void actionPerformed(ActionEvent e) {
// 1.获得加数和被加数
int n1 = Integer.parseInt(calculator.num1.getText());
int n2 = Integer.parseInt(calculator.num2.getText());
// 2.将这个值 + 运算后,放到第三个框
calculator.num3.setText(""+(n1+n2));
// 3.清除前两个框
calculator.num1.setText("");
calculator.num2.setText("");
}
}
内部类:
更好的包装
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class TextText02 {
public static void main(String[] args) {
new Calculator().loading();
}
}
class Calculator extends Frame{
// 属性
TextField num1, num2, num3;
// 方法
public void loading(){
num1 = new TextField(10);//字符数
num2 = new TextField(10);//字符数
num3 = new TextField(20);//字符数
Button button = new Button("=");
button.addActionListener(new MyCalculatorListener());
Label label = new Label("+");
// 布局
setLayout(new FlowLayout());
add(num1);
add(label);
add(num2);
add(button);
add(button);
add(num3);
pack();
setVisible(true);
}
// 监听器类
// 内部类最大的好处,就是可以畅通无阻的访问外部的属性和方法!
class MyCalculatorListener implements ActionListener{
@Override
public void actionPerformed(ActionEvent e) {
// 1.获得加数和被加数
int n1 = Integer.parseInt(num1.getText());
int n2 = Integer.parseInt(num2.getText());
// 2.将这个值 + 运算后,放到第三个框
num3.setText(""+(n1+n2));
// 3.清除前两个框
num1.setText("");
num2.setText("");
}
}
}
2.8、画笔
import java.awt.*;
public class TestPaint {
public static void main(String[] args) {
new MyPaint().loading();
}
}
class MyPaint extends Frame{
public void loading(){
setBounds(200,200,600,600);
setVisible(true);
}
// 画笔
@Override
public void paint(Graphics g) {
// 画笔,画笔需要颜色,画笔可以画画
g.setColor(Color.red);
g.drawOval(100,100,100,100);
// 养成习惯,画笔用完,将他还原为最初的颜色
}
}
2.9、鼠标监听
import java.awt.*;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.ArrayList;
import java.util.Iterator;
public class TestMouseListener {
public static void main(String[] args) {
new MyFrame("画点");
}
}
//自己的类
class MyFrame extends Frame {
// 用一个集合存点
ArrayList points;
public MyFrame(String title){
super(title);
setBounds(200,200,500,500);
points = new ArrayList<>();
// 鼠标监听器,正对这个窗口
this.addMouseListener(new MyMouseListener());
setVisible(true);
}
@Override
public void paint(Graphics g) {
Iterator iterator = points.iterator();
while(iterator.hasNext()){
Point point = (Point) iterator.next();
g.setColor(Color.BLACK);
g.fillOval(point.x, point.y,10,10);
}
}
// 添加一个点到界面上
public void addPaint(Point point){
points.add(point);
}
// 适配器模式
private class MyMouseListener extends MouseAdapter{
// 鼠标按下,弹起,按住不放
@Override
public void mousePressed(MouseEvent e) {
MyFrame frame = (MyFrame) e.getSource();
// 这个点就是鼠标的点
frame.addPaint(new Point(e.getX(), e.getY()));
frame.repaint();//刷新
}
}
}
2.10、窗口监听
import java.awt.*;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
public class TestWindowListener {
public static void main(String[] args) {
new WindowFrame();
}
}
class WindowFrame extends Frame{
public WindowFrame(){
setBounds(100,100,400,400);
setVisible(true);
addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
System.out.println("关闭窗口");
}
@Override
public void windowActivated(WindowEvent e) {
System.out.println("激活窗口");
}
});
}
}
2.11、键盘监听
import java.awt.*;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
public class TestKeyListener {
public static void main(String[] args) {
new KeyFrame();
}
}
class KeyFrame extends Frame{
public KeyFrame(){
setBounds(1,2,300,400);
setVisible(true);
this.addKeyListener(new KeyAdapter() {
@Override
public void keyPressed(KeyEvent e) {
int keyCode = e.getKeyCode();
if (keyCode == KeyEvent.VK_UP) {
System.out.println("你按了上建");
}
}
});
}
}
3、Swing
3.1、窗口、面板
import javax.swing.*;
import java.awt.*; public class JFrameDemo {
// init()
public void init(){
// *窗口
JFrame jf = new JFrame("这是一个JFrame窗口");
jf.setVisible(true);
jf.setBounds(100,100,200,200); // 设置文字
JLabel label = new JLabel("这是一段话");
jf.add(label); label.setHorizontalAlignment(SwingConstants.CENTER); // 获得一个容器
Container contentPane = jf.getContentPane();
contentPane.setBackground(Color.yellow); // 关闭窗口
jf.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
} public static void main(String[] args) {
new JFrameDemo().init();
}
}
3.2、弹窗
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener; public class DialogDemo extends JFrame { public DialogDemo(){
this.setVisible(true);
this.setBounds(100,100,600,600); Container contentPane = this.getContentPane();
// 绝对布局
contentPane.setLayout(null);
contentPane.setBackground(Color.red); JButton button = new JButton("点击弹出一个对话框");
button.setBounds(50,50,50,50); // 点击这个按钮时,弹出一个窗口
button.addActionListener(new ActionListener() { //监听器
@Override
public void actionPerformed(ActionEvent e) {
new MyDialogDemo();
}
}); contentPane.add(button); this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
} public static void main(String[] args) {
new DialogDemo();
}
} class MyDialogDemo extends JDialog{
public MyDialogDemo(){
this.setVisible(true);
this.setBounds(500,500,500,500); Container contentPane = this.getContentPane();
contentPane.setLayout(null); contentPane.add(new JLabel("哈哈哈"));
}
}
3.3、标签
label
new JLabel("xxx");
图标icon
import javax.swing.*;
import java.awt.*; public class IconDemo extends JFrame implements Icon {
private int width;
private int heigth; public IconDemo(){}
public IconDemo(int width, int heigth){
this.width = width;
this.heigth = heigth;
} public void init(){
IconDemo iconDemo = new IconDemo(15, 15);
JLabel label = new JLabel("icontest", iconDemo, SwingConstants.CENTER); Container contentPane = this.getContentPane();
contentPane.add(label); this.setVisible(true);
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
} public static void main(String[] args) {
new IconDemo().init();
} @Override
public void paintIcon(Component c, Graphics g, int x, int y) {
g.fillOval(x,y,width,heigth);
} @Override
public int getIconWidth() {
return width;
} @Override
public int getIconHeight() {
return heigth;
}
}
图片icon
import javax.swing.*;
import java.awt.*;
import java.net.URL; public class ImageIconDemo extends JFrame { public ImageIconDemo() {
// 获取图片的地址
JLabel label = new JLabel("ImageIcon");
URL url = ImageIconDemo.class.getResource("tx.jpg"); ImageIcon imageIcon = new ImageIcon(url);
label.setIcon(imageIcon);
label.setHorizontalAlignment(SwingConstants.CENTER); Container contentPane = getContentPane();
contentPane.add(label);
}
}
3.4、面板
JPanel
import javax.swing.*;
import java.awt.*; public class JPanelDemo extends JFrame {
public JPanelDemo(){
Container contentPane = this.getContentPane(); contentPane.setLayout(new GridLayout(2,2,10,10)); JPanel panel1 = new JPanel(new GridLayout(1, 3));
JPanel panel2 = new JPanel(new GridLayout(1, 2));
JPanel panel3 = new JPanel(new GridLayout(2, 1));
JPanel panel4 = new JPanel(new GridLayout(3, 2)); panel1.add(new JButton("1"));
panel1.add(new JButton("1"));
panel1.add(new JButton("1"));
panel2.add(new JButton("2"));
panel2.add(new JButton("2"));
panel3.add(new JButton("3"));
panel3.add(new JButton("3"));
panel4.add(new JButton("4"));
panel4.add(new JButton("4"));
panel4.add(new JButton("4"));
panel4.add(new JButton("4"));
panel4.add(new JButton("4"));
panel4.add(new JButton("4")); contentPane.add(panel1);
contentPane.add(panel2);
contentPane.add(panel3);
contentPane.add(panel4); this.setBounds(200,200,200,200);
this.setVisible(true);
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); } public static void main(String[] args) {
new JPanelDemo();
}
}
JScrollPanel
import javax.swing.*;
import java.awt.*; public class JScrollDemo extends JFrame { public JScrollDemo(){
Container contentPane = this.getContentPane(); JTextArea jTextArea = new JTextArea();
jTextArea.setText("这是一个文本框"); // Scroll面板
JScrollPane jScrollPane = new JScrollPane(jTextArea);
contentPane.add(jScrollPane); this.setVisible(true);
this.setBounds(200,200,200,200);
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
} public static void main(String[] args) {
new JScrollDemo();
}
}
3.5、按钮
图片按钮
import javax.swing.*;
import java.awt.*;
import java.net.URL; public class JButtonDemo01 extends JFrame { public JButtonDemo01(){
Container contentPane = this.getContentPane();
// 将一个图片变为图标
URL resource = JButtonDemo01.class.getResource("tx.jpg");
Icon icon = new ImageIcon(resource); // 把这个图标放到按钮上
JButton button = new JButton();
button.setIcon(icon);
button.setToolTipText("图片按钮"); contentPane.add(button); this.setVisible(true);
this.setBounds(200,200,200,200);
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
} public static void main(String[] args) {
new JButtonDemo01();
}
}
单选按钮
import javax.swing.*;
import java.awt.*;
import java.net.URL; public class JButtonDemo02 extends JFrame { public JButtonDemo02(){
Container contentPane = this.getContentPane();
// 将一个图片变为图标
URL resource = JButtonDemo01.class.getResource("tx.jpg");
Icon icon = new ImageIcon(resource); // 单选框
JRadioButton jRadioButton1 = new JRadioButton("JRadioButton1");
JRadioButton jRadioButton2 = new JRadioButton("JRadioButton2");
JRadioButton jRadioButton3 = new JRadioButton("JRadioButton3"); // 由于单选框只能选择一个,分组,一组中只能选择一个
ButtonGroup group = new ButtonGroup();
group.add(jRadioButton1);
group.add(jRadioButton2);
group.add(jRadioButton3); contentPane.add(jRadioButton1, BorderLayout.NORTH);
contentPane.add(jRadioButton2, BorderLayout.CENTER);
contentPane.add(jRadioButton3, BorderLayout.SOUTH); this.setVisible(true);
this.setBounds(200,200,200,200);
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
} public static void main(String[] args) {
new JButtonDemo02();
}
}
复选按钮
import javax.swing.*;
import java.awt.*;
import java.net.URL; public class JButtonDemo03 extends JFrame { public JButtonDemo03(){
Container contentPane = this.getContentPane();
// 将一个图片变为图标
URL resource = JButtonDemo01.class.getResource("tx.jpg");
Icon icon = new ImageIcon(resource); // 多选框
JCheckBox jCheckBox1 = new JCheckBox("jCheckBox1");
JCheckBox jCheckBox2 = new JCheckBox("jCheckBox2"); contentPane.add(jCheckBox1, BorderLayout.NORTH);
contentPane.add(jCheckBox2, BorderLayout.CENTER); this.setVisible(true);
this.setBounds(200,200,200,200);
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
} public static void main(String[] args) {
new JButtonDemo03();
}
}
3.6、列表
下拉框
import javax.swing.*;
import java.awt.*; public class TestComboboxDemo01 extends JFrame {
public TestComboboxDemo01(){ Container contentPane = this.getContentPane(); JComboBox<Object> status = new JComboBox(); status.addItem(null);
status.addItem("正在上映");
status.addItem("已下架");
status.addItem("即将上映"); contentPane.add(status); this.setVisible(true);
this.setBounds(200,200,200,200);
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
} public static void main(String[] args) {
new TestComboboxDemo01();
}
}
列表框
import javax.swing.*;
import java.awt.*;
import java.util.Vector; public class TestComboboxDemo02 extends JFrame { public TestComboboxDemo02(){ Container contentPane = this.getContentPane(); // 生成列表的内容
Vector objects = new Vector();
objects.add("zhangsan");
objects.add("lisi");
objects.add("wangwu"); JList jList = new JList(objects);
contentPane.add(jList); this.setVisible(true);
this.setBounds(200,200,200,200);
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
} public static void main(String[] args) {
new TestComboboxDemo02();
}
}
-
应用场景
-
选择地区,或者一些单个选项。
列表,展示信息,一般是动态扩容。
-
3.7、文本框
文本框
import javax.swing.*;
import java.awt.*; public class TestTestDemo01 extends JFrame {
public TestTestDemo01(){ Container contentPane = this.getContentPane(); JTextField jTextField01 = new JTextField("hellow");
JTextField jTextField02 = new JTextField("world"); contentPane.add(jTextField01, BorderLayout.NORTH);
contentPane.add(jTextField02, BorderLayout.SOUTH); this.setVisible(true);
this.setBounds(200,200,200,200);
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
} public static void main(String[] args) {
new TestTestDemo01();
}
}
密码框
import javax.swing.*;
import java.awt.*; public class TestTestDemo02 extends JFrame {
public TestTestDemo02(){ Container contentPane = this.getContentPane(); JPasswordField jPasswordField = new JPasswordField(); //默认已经是***了
jPasswordField.setEchoChar('*'); contentPane.add(jPasswordField); this.setVisible(true);
this.setBounds(200,200,200,200);
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
} public static void main(String[] args) {
new TestTestDemo02();
}
}
文本域
import javax.swing.*;
import java.awt.*; public class JScrollDemo extends JFrame { public JScrollDemo(){
Container contentPane = this.getContentPane(); JTextArea jTextArea = new JTextArea();
jTextArea.setText("这是一个文本框"); // Scroll面板
JScrollPane jScrollPane = new JScrollPane(jTextArea);
contentPane.add(jScrollPane); this.setVisible(true);
this.setBounds(200,200,200,200);
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
} public static void main(String[] args) {
new JScrollDemo();
}
}