• GUI编程入门


    GUI编程

    1.简介

    Gui的核心技术:Swing,AWT

    URL:统一资源管理器

    2.AWT

    2.1.Awt介绍

    抽象的窗口工具

    1.包含了很多类和接口

    2.元素:窗口,按钮,文本框

    3.java.awt

    2.2.组件和容器

    2.1.frame

    2.2.面板Panel

    3.布局管理器

    2.3流式布局

    1. package com.qyx.www.lesson01;
    2. import java.awt.*;
    3. public class TestFlowLayout {
    4. public static void main(String[] args) {
    5. Frame frame = new Frame();
    6. //组件-按钮
    7. Button button1 = new Button("button1");
    8. Button button2 = new Button("button2");
    9. Button button3 = new Button("button3");
    10. //设置为流式布局
    11. //frame.setLayout(new FlowLayout());
    12. // frame.setLayout(new FlowLayout(FlowLayout.LEFT));
    13. frame.setLayout(new FlowLayout(FlowLayout.RIGHT));
    14. frame.setSize(200,200);
    15. //把按钮添加上去
    16. frame.add(button1);
    17. frame.add(button2);
    18. frame.add(button3);
    19. frame.setVisible(true);
    20. }
    21. }

    2.4东西南北中

    1. package com.qyx.www.lesson01;
    2. import java.awt.*;
    3. public class TestBorderLayout {
    4. public static void main(String[] args) {
    5. Frame frame = new Frame("TestBordLayout");
    6. Button east = new Button("East");
    7. Button west = new Button("West");
    8. Button south = new Button("South");
    9. Button north = new Button("North");
    10. Button center = new Button("Center");
    11. frame.add(east,BorderLayout.EAST);
    12. frame.add(west,BorderLayout.WEST);
    13. frame.add(south,BorderLayout.SOUTH);
    14. frame.add(north,BorderLayout.NORTH);
    15. frame.add(center,BorderLayout.CENTER);
    16. frame.setSize(200,200);
    17. frame.setVisible(true);
    18. }
    19. }

    2.3.表格布局

    1. package com.qyx.www.lesson01;
    2. import java.awt.*;
    3. public class TestGridLayout {
    4. public static void main(String[] args) {
    5. Frame frame = new Frame("TestGridLayout");
    6. Button btn1 = new Button("btn1");
    7. Button btn2 = new Button("btn2");
    8. Button btn3 = new Button("btn3");
    9. Button btn4 = new Button("btn4");
    10. Button btn5 = new Button("btn5");
    11. Button btn6 = new Button("btn6");
    12. frame.setLayout(new GridLayout(3,2));
    13. frame.add(btn1);
    14. frame.add(btn2);
    15. frame.add(btn3);
    16. frame.add(btn4);
    17. frame.add(btn5);
    18. frame.add(btn6);
    19. frame.pack();//java函数
    20. frame.setVisible(true);
    21. }
    22. }

    3.1作业题:

    1. package com.qyx.www.lesson01;
    2. import java.awt.*;
    3. public class TestButton {
    4. public static void main(String[] args) {
    5. Frame frame = new Frame();
    6. frame.setSize(400,300);
    7. frame.setLocation(300,400);
    8. frame.setBackground(Color.BLACK);
    9. frame.setVisible(true);
    10. frame.setLayout(new GridLayout(2,1));
    11. //4个面板
    12. Panel p1 = new Panel(new BorderLayout());
    13. Panel p2 = new Panel(new GridLayout(2, 1));
    14. Panel p3 = new Panel(new BorderLayout());
    15. Panel p4 = new Panel(new GridLayout(2, 2));
    16. //上面OK
    17. p1.add(new Button("East-1"),BorderLayout.EAST);
    18. p1.add(new Button("West-1"),BorderLayout.WEST);
    19. p2.add(new Button("p2-btn-1"));
    20. p2.add(new Button("p2-btn-2"));
    21. p1.add(p2,BorderLayout.CENTER);
    22. //下面
    23. p3.add(new Button("East-2"),BorderLayout.EAST);
    24. p3.add(new Button("West-2"),BorderLayout.WEST);
    25. //中间四个
    26. for (int i = 0; i < 4; i++) {
    27. p4.add(new Button("for-"+i));
    28. }
    29. p3.add(p4,BorderLayout.CENTER);
    30. frame.add(p1);
    31. frame.add(p3);
    32. }
    33. }

    3.2.总结:

    1.Frame是一个顶级窗口

    2.Panel无法单独显示,必须添加到某个容器中

    3.布局管理器

    1.流式

    2.东西南北中

    3.表格

    4.大小,定位,背景颜色,可见性,监听!

    2.4.事件监听

    事件监听:当某个事情发生的时候,干什么?

    1. package com.qyx.www.lesson02;
    2. import java.awt.*;
    3. import java.awt.event.ActionEvent;
    4. import java.awt.event.ActionListener;
    5. import java.awt.event.WindowAdapter;
    6. import java.awt.event.WindowEvent;
    7. public class TestActionEvent {
    8. public static void main(String[] args) {
    9. //按下按钮,触发一些事件
    10. Frame frame = new Frame();
    11. Button button = new Button();
    12. //因为:addActionListener()需要一个ActionListener,所以我们需要构造一个ActionListener
    13. MyActionListener myActionListener = new MyActionListener();
    14. button.addActionListener(myActionListener);
    15. frame.add(button,BorderLayout.CENTER);
    16. frame.pack();
    17. windowClose(frame); //关闭窗口
    18. frame.setVisible(true);
    19. }
    20. //关闭窗口的事件
    21. private static void windowClose(Frame frame){
    22. frame.addWindowListener(new WindowAdapter() {
    23. @Override
    24. public void windowClosing(WindowEvent e) {
    25. System.exit(0);
    26. }
    27. });
    28. }
    29. }
    30. //事件监听
    31. class MyActionListener implements ActionListener{
    32. @Override
    33. public void actionPerformed(ActionEvent e) {
    34. System.out.println("666");
    35. }
    36. }

    多个按钮,共享一个事件

    1. package com.qyx.www.lesson02;
    2. import java.awt.*;
    3. import java.awt.event.ActionEvent;
    4. import java.awt.event.ActionListener;
    5. public class TestActionTwo {
    6. public static void main(String[] args) {
    7. //两个按钮 实现同一个监听
    8. //开始 停止
    9. Frame frame = new Frame("开始-停止");
    10. Button button1 = new Button("start");
    11. Button button2 = new Button("stop");
    12. //可以显示的定义出发会返回的命令,如果不显示定义,则会走默认的值
    13. //可以多个按钮只写一个监听类
    14. button2.setActionCommand("button2-stop");
    15. MyMonitor myMonitor = new MyMonitor();
    16. button1.addActionListener(myMonitor);
    17. button2.addActionListener(myMonitor);
    18. frame.add(button1,BorderLayout.NORTH);
    19. frame.add(button2,BorderLayout.SOUTH);
    20. frame.pack();
    21. frame.setVisible(true);
    22. }
    23. }
    24. class MyMonitor implements ActionListener{
    25. @Override
    26. public void actionPerformed(ActionEvent e) {
    27. //e.getActioncommand()获得按钮的信息
    28. System.out.println("按钮被点击了: msg=>"+e.getActionCommand());
    29. if (e.getActionCommand().equals("start")){
    30. }
    31. }
    32. }

    2.5.输入框.testfield监听

    1. package com.qyx.www.lesson02;
    2. import java.awt.*;
    3. import java.awt.event.ActionEvent;
    4. import java.awt.event.ActionListener;
    5. public class TestText01 {
    6. public static void main(String[] args) {
    7. //启动!
    8. new Myframe();
    9. }
    10. }
    11. class Myframe extends Frame{
    12. public Myframe(){
    13. TextField textField = new TextField();
    14. add(textField);
    15. //监听这个文本框输入的文字
    16. MyActionListener2 myActionListener2 = new MyActionListener2();
    17. textField.addActionListener(myActionListener2);
    18. //设置替换编码
    19. textField.setEchoChar('*');
    20. setVisible(true);
    21. pack();
    22. }
    23. }
    24. class MyActionListener2 implements ActionListener{
    25. @Override
    26. public void actionPerformed(ActionEvent e) {
    27. TextField field =(TextField) e.getSource();//获得一些资源,返回的一个对象
    28. System.out.println(field.getText());
    29. field.setText("");//null
    30. }
    31. }

    2.6.简易计算器,组合+内部类回顾复习

    OOP原则:组合,大于继承!

    1. package com.qyx.www.lesson02;
    2. import java.awt.*;
    3. import java.awt.event.ActionEvent;
    4. import java.awt.event.ActionListener;
    5. public class TestCalc {
    6. public static void main(String[] args) {
    7. new Calculator();
    8. }
    9. }
    10. //计算器类
    11. class Calculator extends Frame{
    12. public Calculator(){
    13. //3个文本框
    14. TextField num1 = new TextField(10);//字符数
    15. TextField num2 = new TextField(10);//字符数
    16. TextField num3 = new TextField(20);//字符数
    17. //1个按钮
    18. Button button = new Button("=");
    19. button.addActionListener(new MyCalculatorListener(num1,num2,num3));
    20. //1个标签
    21. Label label = new Label("+");
    22. //布局
    23. setLayout(new FlowLayout());
    24. add(num1);
    25. add(label);
    26. add(num2);
    27. add(button);
    28. add(num3);
    29. pack();
    30. setVisible(true);
    31. }
    32. }
    33. //监听器类
    34. class MyCalculatorListener implements ActionListener{
    35. //获取三个变量
    36. private TextField num1,num2,num3;
    37. public MyCalculatorListener(TextField num1,TextField num2,TextField num3){
    38. this.num1 = num1;
    39. this.num2 = num2;
    40. this.num3 = num3;
    41. }
    42. @Override
    43. public void actionPerformed(ActionEvent e) {
    44. //1.获得加数和被加数
    45. int n1 = Integer.parseInt(num1.getText());
    46. int n2 = Integer.parseInt(num2.getText());
    47. //2.将这个值 + 法运算后,放到第三个框
    48. num3.setText(""+(n1+n2));
    49. //3.清除前两个框
    50. num1.setText("");
    51. num2.setText("");
    52. }
    53. }

    2.7.画笔

    1. package com.qyx.www.lesson03;
    2. import java.awt.*;
    3. public class TestPaint {
    4. public static void main(String[] args) {
    5. new MyPaint().loadFrame();
    6. }
    7. }
    8. class MyPaint extends Frame{
    9. public void loadFrame(){
    10. setBounds(200,200,600,500);
    11. setVisible(true);
    12. }
    13. //画笔
    14. @Override
    15. public void paint(Graphics g){
    16. //super.paint(g);
    17. //画笔需要又颜色,画笔可以画画
    18. //g.setColor(Color.red);
    19. //g.drawOval(100,100,100,100);
    20. g.fillOval(100,100,100,100);//实心的圆
    21. //g.setColor(Color.GREEN);
    22. g.fillRect(150,200,150,200);
    23. //养成习惯,画笔用完,将他还原到最初的颜色
    24. }
    25. }

    2.8.鼠标监听

    目的:想要实现鼠标画画

    1. package com.qyx.www.lesson03;
    2. import java.awt.*;
    3. import java.awt.event.MouseAdapter;
    4. import java.awt.event.MouseEvent;
    5. import java.util.ArrayList;
    6. import java.util.Iterator;
    7. public class TestMouseListener {
    8. //鼠标监听事件
    9. public static void main(String[] args) {
    10. new MyFrame("画图");
    11. }
    12. }
    13. //自己的类
    14. class MyFrame extends Frame{
    15. //画画需要画笔,需要监听鼠标当前的位置,需要集合来存储这个点
    16. ArrayList points;
    17. public MyFrame(String title) {
    18. super(title);
    19. setBounds(200,200,400,300);
    20. //存鼠标的点击的点
    21. points = new ArrayList<>();
    22. setVisible(true);
    23. //鼠标监听器,正对这个窗口
    24. this.addMouseListener(new MyMouseListener());
    25. }
    26. @Override
    27. public void paint(Graphics g){
    28. //画画,监听鼠标的事件
    29. Iterator iterator = points.iterator();
    30. while (iterator.hasNext()){
    31. Point point =(Point) iterator.next();
    32. g.setColor(Color.BLUE);
    33. g.fillOval(point.x,point.y,10,10);
    34. }
    35. }
    36. //添加一个点到界面上
    37. public void addPaint(Point point){
    38. points.add(point);
    39. }
    40. //适配器模式
    41. private class MyMouseListener extends MouseAdapter{
    42. //鼠标 按下,弹起,按住不放
    43. @Override
    44. public void mousePressed(MouseEvent e){
    45. MyFrame myFrame =(MyFrame) e.getSource();
    46. //这个我们点击的时候,就会再界面上产生一个点:画
    47. //这个点就说鼠标的点;
    48. myFrame.addPaint(new Point(e.getX(),e.getY()));
    49. //每次点击鼠标都需要重新画一遍
    50. myFrame.repaint();//刷新 30帧 60帧
    51. }
    52. }
    53. }

    2.9.窗口监听

    1. package com.qyx.www.lesson03;
    2. import java.awt.*;
    3. import java.awt.event.WindowAdapter;
    4. import java.awt.event.WindowEvent;
    5. public class TestWindow {
    6. public static void main(String[] args) {
    7. new WindowsFrame();
    8. }
    9. }
    10. class WindowsFrame extends Frame{
    11. public WindowsFrame(){
    12. setBackground(Color.blue);
    13. setBounds(100,100,200,200);
    14. setVisible(true);
    15. //addWindowListener(new MyWindowListener());
    16. this.addWindowListener(
    17. //匿名内部类
    18. new WindowAdapter() {
    19. //关闭窗口
    20. @Override
    21. public void windowClosing(WindowEvent e) {
    22. System.out.println("windowClosing");
    23. System.exit(0);
    24. }
    25. //激活窗口
    26. @Override
    27. public void windowActivated(WindowEvent e) {
    28. WindowsFrame source = (WindowsFrame)e.getSource();
    29. source.setTitle("被激活了");
    30. System.out.println("windowActivated");
    31. }
    32. }
    33. );
    34. }
    35. }

    2.10.键盘监听

    1. package com.qyx.www.lesson03;
    2. import java.awt.*;
    3. import java.awt.event.KeyAdapter;
    4. import java.awt.event.KeyEvent;
    5. public class TestKeyListener {
    6. public static void main(String[] args) {
    7. new KeyFrame();
    8. }
    9. }
    10. class KeyFrame extends Frame{
    11. public KeyFrame(){
    12. setBounds(1,2,300,400);
    13. setVisible(true);
    14. this.addKeyListener(new KeyAdapter() {
    15. @Override
    16. public void keyPressed(KeyEvent e) {
    17. //获得键盘下的键是哪一个,当前的码
    18. int keyCode = e.getKeyCode();
    19. System.out.println(keyCode);
    20. if (keyCode == KeyEvent.VK_UP){
    21. System.out.println("你按下了上键");
    22. }
    23. }
    24. });
    25. }
    26. }

    3.Swing

    3.1.窗口。面板

    1. package com.qyx.www.lesson04;
    2. import javax.swing.*;
    3. import java.awt.*;
    4. public class JFrameDemo {
    5. public static void main(String[] args) {
    6. new MyJframe2().init();
    7. }
    8. }
    9. class MyJframe2 extends JFrame{
    10. public void init(){
    11. this.setBounds(10,10,200,300);
    12. this.setVisible(true);
    13. JLabel label = new JLabel("欢迎来到我的世界!");
    14. this.add(label);
    15. //让文本标签居中 设置水平对齐
    16. label.setHorizontalAlignment(SwingConstants.CENTER);
    17. //获得一个容器
    18. Container container = this.getContentPane();
    19. container.setBackground(Color.YELLOW);
    20. }
    21. }

    3.2弹窗

    JDialog,用来被弹出,默认就有关闭事件

    1. package com.qyx.www.lesson04;
    2. import javax.swing.*;
    3. import java.awt.*;
    4. import java.awt.event.ActionEvent;
    5. import java.awt.event.ActionListener;
    6. //主窗口
    7. public class DialogDemo extends JFrame{
    8. public DialogDemo(){
    9. this.setVisible(true);
    10. this.setSize(700,500);
    11. this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    12. //JFrame 放东西,容器
    13. Container container = this.getContentPane();
    14. //绝对布局
    15. container.setLayout(null);
    16. //按钮
    17. JButton button = new JButton("点击弹出一个对话框"); //创建
    18. button.setBounds(30,30,200,50);
    19. //点击这个按钮的时候,弹出一个弹窗
    20. button.addActionListener(new ActionListener() {
    21. @Override
    22. public void actionPerformed(ActionEvent e) {
    23. //弹窗
    24. new MyDialogDemo();
    25. }
    26. });
    27. container.add(button);
    28. }
    29. public static void main(String[] args) {
    30. new DialogDemo();
    31. }
    32. }
    33. //弹窗的窗口
    34. class MyDialogDemo extends JDialog{
    35. public MyDialogDemo(){
    36. this.setVisible(true);
    37. this.setBounds(100,100,500,500);
    38. Container container = this.getContentPane();
    39. container.setLayout(null);
    40. container.add(new Label("老师带你学java"));
    41. }
    42. }

    3.3标签

    label

    new JLabel("xxx");
    

    图标ICON

    1. package com.qyx.www.lesson04;
    2. import javax.swing.*;
    3. import java.awt.*;
    4. //图标,需要实现类,Frame继承
    5. public class IconDemo extends JFrame implements Icon {
    6. private int width;
    7. private int height;
    8. public IconDemo(){} //无参构造
    9. public IconDemo(int width,int height){
    10. this.width=width;
    11. this.height=height;
    12. }
    13. public void init(){
    14. IconDemo iconDemo = new IconDemo(15, 15);
    15. //图标放在标签,也可以放在按钮上1
    16. JLabel label = new JLabel("icontest", iconDemo, SwingConstants.CENTER);
    17. Container container = getContentPane();
    18. container.add(label);
    19. this.setVisible(true);
    20. this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    21. }
    22. public static void main(String[] args) {
    23. new IconDemo().init();
    24. }
    25. @Override
    26. public void paintIcon(Component c, Graphics g, int x, int y) {
    27. }
    28. @Override
    29. public int getIconWidth() {
    30. return 0;
    31. }
    32. @Override
    33. public int getIconHeight() {
    34. return 0;
    35. }
    36. }

    图片ICON

    1. package com.qyx.www.lesson04;
    2. import javax.swing.*;
    3. import java.awt.*;
    4. import java.net.URL;
    5. public class ImageIconDemo extends JFrame {
    6. public ImageIconDemo() {
    7. //获取图片的地址
    8. JLabel label = new JLabel("ImageIcon");
    9. URL url = ImageIconDemo.class.getResource("tx.jpg");
    10. ImageIcon imageIcon = new ImageIcon(url); //命名不要冲突了
    11. label.setIcon(imageIcon);
    12. label.setHorizontalAlignment(SwingConstants.CENTER);
    13. Container container = getContentPane();
    14. container.add(label);
    15. setVisible(true);
    16. setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    17. setBounds(100, 100, 200, 200);
    18. }
    19. public static void main(String[] args) {
    20. new ImageIconDemo();
    21. }
    22. }

    3.4面板

    Jpanel

    1. package com.qyx.www.lesson05;
    2. import javax.swing.*;
    3. import java.awt.*;
    4. public class JPanelDemo extends JFrame {
    5. public JPanelDemo(){
    6. Container container = this.getContentPane();
    7. container.setLayout(new GridLayout(2,1,10,10));//后面两个参数是间距的意思
    8. JPanel panel1 = new JPanel(new GridLayout(1, 3));
    9. JPanel panel2 = new JPanel(new GridLayout(1, 2));
    10. JPanel panel3 = new JPanel(new GridLayout(2, 1));
    11. JPanel panel4 = new JPanel(new GridLayout(3, 2));
    12. panel1.add(new JButton("1"));
    13. panel1.add(new JButton("1"));
    14. panel1.add(new JButton("1"));
    15. panel2.add(new JButton("2"));
    16. panel2.add(new JButton("2"));
    17. panel3.add(new JButton("3"));
    18. panel3.add(new JButton("3"));
    19. panel4.add(new JButton("4"));
    20. panel4.add(new JButton("4"));
    21. panel4.add(new JButton("4"));
    22. panel4.add(new JButton("4"));
    23. panel4.add(new JButton("4"));
    24. panel4.add(new JButton("4"));
    25. container.add(panel1);
    26. container.add(panel2);
    27. container.add(panel3);
    28. container.add(panel4);
    29. this.setVisible(true);
    30. this.setSize(500,500);
    31. this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    32. }
    33. public static void main(String[] args) {
    34. new JPanelDemo();
    35. }
    36. }

    JScrollPanel

    1. package com.qyx.www.lesson05;
    2. import javax.swing.*;
    3. import java.awt.*;
    4. public class JScrollDemo extends JFrame {
    5. public JScrollDemo(){
    6. Container container = this.getContentPane();
    7. //文本域
    8. JTextArea textArea = new JTextArea(20, 50);
    9. textArea.setText("欢迎学习狂神说Java");
    10. //Scroll面板
    11. JScrollPane scrollpane = new JScrollPane(textArea);
    12. container.add(scrollpane);
    13. this.setVisible(true);
    14. this.setBounds(100,100,300,350);
    15. this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    16. }
    17. public static void main(String[] args) {
    18. new JScrollDemo();
    19. }
    20. }

    3.5按钮

    (1)单选按钮

    (2)复选按钮

    3.6列表

    下拉框

    1. package com.qyx.www.lesson05;
    2. import javax.swing.*;
    3. import java.awt.*;
    4. public class TestComboboxDemo01 extends JFrame {
    5. public TestComboboxDemo01() {
    6. Container container = this.getContentPane();
    7. JComboBox status = new JComboBox();
    8. status.addItem(null);
    9. status.addItem("正在热映");
    10. status.addItem("已下架");
    11. status.addItem("即将上映");
    12. container.add(status);
    13. this.setVisible(true);
    14. this.setSize(500,350);
    15. this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    16. }
    17. public static void main(String[] args) {
    18. new TestComboboxDemo01();
    19. }
    20. }

    列表框

    1. package com.qyx.www.lesson05;
    2. import javax.swing.*;
    3. import java.awt.*;
    4. public class TestComboboxDemo02 extends JFrame {
    5. public TestComboboxDemo02() {
    6. Container container = this.getContentPane();
    7. //生成列表的内容
    8. String[] contents = {"1","2","3"};
    9. //列表中需要放入内容
    10. JList jList = new JList(contents);
    11. container.add(jList);
    12. this.setVisible(true);
    13. this.setSize(500,350);
    14. this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    15. }
    16. public static void main(String[] args) {
    17. new TestComboboxDemo02();
    18. }
    19. }

    3.7文本框

    文本框

    1. package com.qyx.www.lesson05;
    2. import javax.swing.*;
    3. import java.awt.*;
    4. public class TestTextDemo01 extends JFrame {
    5. public TestTextDemo01() {
    6. Container container = this.getContentPane();
    7. JTextField textField = new JTextField("hello");
    8. JTextField textField2 = new JTextField("hello",20);
    9. container.add(textField,BorderLayout.NORTH);
    10. container.add(textField2,BorderLayout.SOUTH);
    11. this.setVisible(true);
    12. this.setSize(500,350);
    13. this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    14. }
    15. public static void main(String[] args) {
    16. new TestTextDemo01();
    17. }
    18. }

    密码框

    1. package com.qyx.www.lesson05;
    2. import javax.swing.*;
    3. import java.awt.*;
    4. public class TestTextDemo03 extends JFrame {
    5. public TestTextDemo03() {
    6. Container container = this.getContentPane();
    7. JPasswordField passwordField = new JPasswordField();//***
    8. passwordField.setEchoChar('*');
    9. container.add(passwordField);
    10. this.setVisible(true);
    11. this.setSize(500,350);
    12. this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    13. }
    14. public static void main(String[] args) {
    15. new TestTextDemo03();
    16. }
    17. }

    文本域

    1. package com.qyx.www.lesson05;
    2. import javax.swing.*;
    3. import java.awt.*;
    4. public class JScrollDemo extends JFrame {
    5. public JScrollDemo(){
    6. Container container = this.getContentPane();
    7. //文本域
    8. JTextArea textArea = new JTextArea(20, 50);
    9. textArea.setText("欢迎学习狂神说Java");
    10. //Scroll面板
    11. JScrollPane scrollpane = new JScrollPane(textArea);
    12. container.add(scrollpane);
    13. this.setVisible(true);
    14. this.setBounds(100,100,300,350);
    15. this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    16. }
    17. public static void main(String[] args) {
    18. new JScrollDemo();
    19. }
    20. }

    内容较多,但覆盖了GUI的所有方面,后续可以利用GUI实现一个简单贪吃蛇小游戏。

  • 相关阅读:
    新一批光学好书已上架
    Tomcat8 JVM参数配置
    云服务器利用Docker搭建sqli-labs靶场环境
    01背包、完全背包进阶理解(全网最详细)
    工作中何如来合理分配核心线程数?
    《计算机组成原理/CSAPP》网课总结(二)——编译原理基础
    POE 利用区块链挖掘协同执行遗传算法
    详谈操作系统中的内核态和用户态
    龙蜥社区开源 coolbpf,BPF 程序开发效率提升百倍
    AI智能视频技术在考古工作中的安防应用
  • 原文地址:https://blog.csdn.net/qq_60154877/article/details/126339702