GUI GUI全称Graphical User Interface,图形用户界面
组件
窗口
弹窗
面板
文本框
列表框
按钮
图片
监听事件
鼠标
键盘事件
简介 GUI核心技术:AWT、SWing
为什么不火
界面不美观
需要jre环境
为什么要学习GUI
MVC ,了解监听器
可以写出自己心中想要的工具
工作中可能维护到SWing界面
AWT AWT介绍 AWT(Abstract Windows Toolkit)抽象窗口工具
包含很多类和接口
元素:窗口、按钮、文本框
Java.awt包的层次结构如下
组件和容器Component and Container 容器
容器(Container)也是一个类,实际上是Component的子类,因此容器本身也是一个组件,具有组件的所有性质,但是它的主要功能是容纳其它组件和容器。容器可以简化图形化界面的设计,以整体结构来布置界面。所有的容器都可以通过add()方法向容器中添加组件。常用的容器3种:Panel, Frame, Applet。
来源:百度百科
框架Frame
构造方法:
1 Frame frame = new Frame("" );
要生成一个窗口,通常使用Window的子类Frame类进行实例化,而不是直接使用Window 类,框架的外观就像平常Windows系统下的窗口,有标题、边框、菜单和大小等。setSize()方法可以设置框架尺寸的大小,setVisibe()方法可以设置窗口的可见性。
来源:百度百科
示例:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 package cn.linsip.lesson01;import java.awt.*;public class TestFrame { public static void main (String[] args) { Frame frame = new Frame("我的第一个Java图形界面" ); frame.setVisible(true ); frame.setSize(400 , 400 ); frame.setBackground(new Color(1 , 1 , 1 )); frame.setLocation(0 , 0 ); frame.setResizable(false ); } }
窗口出现问题
窗口关闭不掉
解决方法:停止Java程序运行
回顾封装:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 package cn.linsip.lesson01;import java.awt.*;public class TestFrame02 { public static void main (String[] args) { MyFrame myFrame1 = new MyFrame(200 , 200 , 200 , 200 ); MyFrame myFrame2 = new MyFrame(400 , 200 , 200 , 200 ); MyFrame myFrame3 = new MyFrame(200 , 400 , 200 , 200 ); MyFrame myFrame4 = new MyFrame(400 , 400 , 200 , 200 ); } }class MyFrame extends Frame { static int id; public MyFrame (int x, int y, int w, int h) { super ("MyFrame" + (++id)); setVisible(true ); setBackground(Color.black); setBounds(x, y, w, h); setResizable(false ); } }
面板panel
框架一般用作Java应用程序的窗口,而Applet是Java小程序的窗口。与Frame不同,Applet是在网页中显示的,也可以通过添加Panel进行组件布局。
来源:百度百科
Panel一个空间,非独立存在
可以在panel中添加文本框、文本域、列表、单选按钮、复选按钮、画布、标签、字体大小、事件、滚动条
构造方法
1 Panel panel = new Panel();
示例
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 package cn.linsip.lesson01;import java.awt.*;import java.awt.event.WindowAdapter;import java.awt.event.WindowEvent;public class TestPanel { public static void main (String[] args) { Frame frame = new MyFrame(100 , 100 , 400 , 400 , Color.RED); Panel panel = new Panel(); frame.setLayout(null ); panel.setBounds(100 , 100 , 200 , 200 ); panel.setBackground(Color.white); frame.add(panel); frame.addWindowListener(new WindowAdapter() { @Override public void windowClosing (WindowEvent e) { System.exit(0 ); } }); } }
布局管理器
布局管理器(LayoutManager):每个容器都有一个布局管理器,当容器需要对某个组件进行定位或判断其大小、尺寸时,就会调用其对应的布局管理器。使用布局管理器可以实现跨平台的特性,并且获得动态的布局效果。布局管理器负责管理组件的排列顺序、大小和位置。不同的布局管理器使用不同的布局策略,容器可以通过选择不同的布局管理器来决定如何布局。
来源:百度百科
流式布局Flowlayout
FlowLayout是Panel 和 Applet 的默认布局管理器。在该布局管理器中,组件在容器中按照从上到下,从左到右的顺序进行排列,行满后则换行。
构造方法
1 2 3 4 5 6 FlowLayout(); FlowLayout(new FlowLayout.LEFT); FlowLayout(new FlowLayout.RIGHT, 20 , 20 );
示例
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 package cn.linsip.lesson01;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(FlowLayout.LEFT)); frame.setVisible(true ); frame.setBounds(100 ,100 ,200 ,200 ); frame.add(button1); frame.add(button2); frame.add(button3); } }
边界布局管理器BorderLayout
BorderLayout是Window、Frame和Dialog的默认布局管理器,其将容器分成North、South、East、West和Center 5个区域,每个区域只能放置一个组件。在使用add()方法添加组件到容器时,必须指定将其放置在哪个区域中。使用BorderLayout时,如果容器大小发生变换,组件的相对位置不变。
来源:百度百科
构造方法
1 BorderLayout bl = new BorderLayout();
示例
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 package cn.linsip.lesson01;import java.awt.*;public class TestBorderTest { public static void main (String[] args) { Frame frame = new Frame(); 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.setVisible(true ); frame.setBounds(100 ,100 ,200 ,200 ); } }
网格布局管理器GridLayout
GridLayout 可使容器中的各个组件呈网格状布局,平局占据容器的空间,即使容器的大小发生变化,每个组件还是平均占据容器的空间。和FlowLayout一样,GridLayout也是按照从上到下,从左到右的规律进行排列的。
来源:百度百科
构造方法
1 GridLayout = new GridLayout();
示例
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 package cn.linsip.lesson01;import java.awt.*;import java.awt.event.WindowAdapter;import java.awt.event.WindowEvent;public class TestGridLayout { public static void main (String[] args) { Frame frame = new Frame(); frame.setVisible(true ); Button button1 = new Button("butten1" ); Button button2 = new Button("button2" ); Button button3 = new Button("button3" ); Button button4 = new Button("button4" ); Button button5 = new Button("button5" ); Button button6 = new Button("button6" ); frame.setLayout(new GridLayout(3 ,2 )); frame.add(button1); frame.add(button2); frame.add(button3); frame.add(button4); frame.add(button5); frame.add(button6); frame.pack(); frame.addWindowListener(new WindowAdapter() { @Override public void windowClosing (WindowEvent e) { System.exit(0 ); } }); } }
练习
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 package cn.linsip.lesson01;import java.awt.*;import java.awt.event.WindowAdapter;import java.awt.event.WindowEvent;import java.awt.event.WindowListener;public class FrameDemo { public static void main (String[] args) { Frame frame = new Frame("练习" ); Panel panel1 = new Panel(new BorderLayout()); Panel panel2 = new Panel(new GridLayout(2 , 1 )); Panel panel3 = new Panel(new BorderLayout()); Panel panel4 = new Panel(new GridLayout(2 , 2 )); frame.setLayout(new GridLayout(2 , 1 )); frame.setSize(400 , 400 ); frame.setVisible(true ); panel1.add(new Button("east" ), BorderLayout.EAST); panel1.add(new Button("west" ), BorderLayout.WEST); panel2.add(new Button("p2-1" )); panel2.add(new Button("p2-2" )); panel1.add(panel2, BorderLayout.CENTER); frame.add(panel1); panel3.add(new Button("east" ), BorderLayout.EAST); panel3.add(new Button("west" ), BorderLayout.WEST); panel4.add(new Button("p4-1" )); panel4.add(new Button("p4-2" )); panel4.add(new Button("p4-3" )); panel4.add(new Button("p4-4" )); panel3.add(panel4, BorderLayout.CENTER); frame.add(panel3); frame.addWindowListener(new WindowAdapter() { @Override public void windowClosing (WindowEvent e) { System.exit(0 ); } }); } }
总结:
Frame是一个顶级窗口
Panel无法单独显示,必须添加其到某个容器中
掌握3种布局管理器
Frame设置大小、定位、背景颜色、可见性、监听。
事件监听
Java事件监听器是由事件类和监听接口组成,自定义一个事件前,必须提供一个事件的监听接口以及一个事件类。JAVA中监听接口是继承java.util.EventListener的类,事件类继承java.util.EventObject的类。
来源:百度百科
事件发生的时候,该干什么
示例:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 package cn.linsip.lesson02;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 TestActionEvent01 { public static void main (String[] args) { Frame frame = new Frame(); Button button = new Button("say hello" ); MyActionListener myActionListener = new MyActionListener(); button.addActionListener(myActionListener); frame.add(button, BorderLayout.CENTER); frame.pack(); frame.setVisible(true ); windowClosing(frame); } private static void windowClosing (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("Hello World" ); } }
两个按钮共用一个事件
示例
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 package cn.linsip.lesson02;import java.awt.*;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;public class TestActionEvent02 { public static void main (String[] args) { Frame frame = new Frame(); Button button1 = new Button("start" ); Button button2 = new Button("stop" ); MyMonitor myActionListener = new MyMonitor(); button1.addActionListener(myActionListener); button2.addActionListener(myActionListener); frame.add(button1, BorderLayout.NORTH); frame.add(button2, BorderLayout.SOUTH); frame.pack(); frame.setVisible(true ); windowClosing(frame); } private static void windowClosing (Frame frame) { frame.addWindowListener(new WindowAdapter() { @Override public void windowClosing (WindowEvent e) { System.exit(0 ); } }); } }class MyMonitor implements ActionListener { @Override public void actionPerformed (ActionEvent e) { System.out.println("按钮getCommand:" + e.getActionCommand()); } }
注:
优先用setActionCommand(),其次才是Button的标签
1 button1.setActionCommand("s" );
输入框监听TextField 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 package cn.linsip.lesson02;import cn.linsip.lesson01.TestFrame;import java.awt.*;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;public class TestText { public static void main (String[] args) { new MyFrame(); } }class MyFrame extends Frame { public MyFrame () { TextField textField = new TextField(); add(textField); MyMonitor2 myMonitor2 = new MyMonitor2(); textField.addActionListener(myMonitor2); pack(); setVisible(true ); window } private static void windowClosing (Frame frame) { frame.addWindowListener(new WindowAdapter() { @Override public void windowClosing (WindowEvent e) { System.exit(0 ); } }); } }class MyMonitor2 implements ActionListener { @Override public void actionPerformed (ActionEvent e) { TextField testField = (TextField) e.getSource(); System.out.println(testField.getText()); testField.setText("" ); } }
简易的计算器,组合+内部类回归复习
OOP原则:组合,大于继承
组合
1 2 3 4 5 6 7 public class A extends B { }public class A { private B b; }
练习
加法计算器
如图
思路:
根据拿到前两个数值,返回第三个数值
如何拿到前两个数值
如何返回第三个数值
进一步优化代码
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 package cn.linsip.lesson02;import java.awt.*;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;public class TestCalc { public static void main (String[] args) { Calc calc = new Calc(); } }class Calc extends Frame { public Calc () { TextField text1 = new TextField(20 ); TextField text2 = new TextField(20 ); TextField text3 = new TextField(20 ); Button button = new Button("=" ); button.addActionListener(new MyMonitor3(text1, text2, text3)); Label label = new Label("+" ); setLayout(new FlowLayout()); add(text1); add(label); add(text2); add(button); add(text3); pack(); setVisible(true ); } }class MyMonitor3 implements ActionListener { private TextField text1, text2, text3; public MyMonitor3 (TextField text1, TextField text2, TextField text3) { this .text1 = text1; this .text2 = text2; this .text3 = text3; } @Override public void actionPerformed (ActionEvent e) { int num1 = Integer.parseInt(text1.getText()); int num2 = Integer.parseInt(text2.getText()); text3.setText("" + (num1 + num2)); text1.setText("" ); text2.setText("" ); } }
完全面向对象的写法
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 package cn.linsip.lesson02;import java.awt.*;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;public class TestCalc2 { public static void main (String[] args) { new Calc2().loadCalc(); } }class Calc2 extends Frame { TextField text1, text2, text3; Button button; Label label; public void loadCalc () { text1 = new TextField(20 ); text2 = new TextField(20 ); text3 = new TextField(20 ); button = new Button("=" ); button.addActionListener(new MyMonitor4(this )); label = new Label("+" ); setLayout(new FlowLayout()); add(text1); add(label); add(text2); add(button); add(text3); pack(); setVisible(true ); } }class MyMonitor4 implements ActionListener { private Calc2 calc2 = null ; public MyMonitor4 (Calc2 calc2) { this .calc2 = calc2; } @Override public void actionPerformed (ActionEvent e) { int num1 = Integer.parseInt(calc2.text1.getText()); int num2 = Integer.parseInt(calc2.text2.getText()); calc2.text3.setText("" + (num1 + num2)); calc2.text1.setText("" ); calc2.text2.setText("" ); } }
内部类写法
内部类可以直接访问外部类的属性和方法
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 package cn.linsip.lesson02;import java.awt.*;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;public class TestCalc2 { public static void main (String[] args) { new Calc2().loadCalc(); } }class Calc2 extends Frame { TextField text1, text2, text3; Button button; Label label; public void loadCalc () { text1 = new TextField(20 ); text2 = new TextField(20 ); text3 = new TextField(20 ); button = new Button("=" ); button.addActionListener(new MyMonitor4()); label = new Label("+" ); setLayout(new FlowLayout()); add(text1); add(label); add(text2); add(button); add(text3); pack(); setVisible(true ); } private class MyMonitor4 implements ActionListener { @Override public void actionPerformed (ActionEvent e) { int num1 = Integer.parseInt(calc2.text1.getText()); int num2 = Integer.parseInt(calc2.text2.getText()); calc2.text3.setText("" + (num1 + num2)); calc2.text1.setText("" ); calc2.text2.setText("" ); } } }
画笔 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 package cn.linsip.lesson03;import java.awt.*;public class TestPaint { public static void main (String[] args) { new Paint().loadFrame(); } }class Paint extends Frame { public void loadFrame () { setBounds(200 , 200 , 400 , 400 ); setVisible(true ); } @Override public void paint (Graphics g) { g.drawOval(100 , 100 , 200 , 200 ); g.fillOval(100 , 100 , 200 , 200 ); } }
鼠标监听 目的:想要实现鼠标画画
思路:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 package cn.linsip.lesson03;import java.awt.*;import java.awt.event.MouseAdapter;import java.awt.event.MouseEvent;import java.awt.event.MouseListener;import java.util.ArrayList;import java.util.Iterator;public class TestMouseListener { public static void main (String[] args) { new Myframe("画图" ); } }class Myframe extends Frame { private ArrayList points; public Myframe (String title) { super (title); setBounds(100 , 100 , 400 , 400 ); points = new ArrayList<>(); setVisible(true ); addMouseListener(new MyMouseListener()); } @Override public void paint (Graphics g) { Iterator iterator = points.iterator(); while (iterator.hasNext()) { Point point = (Point) iterator.next(); g.setColor(Color.BLUE); g.fillOval(point.x, point.y, 10 , 10 ); } } public void addPoint (Point point) { points.add(point); } private class MyMouseListener extends MouseAdapter { @Override public void mousePressed (MouseEvent e) { Myframe myframe = (Myframe) e.getSource(); myframe.addPoint(new Point(e.getX(), e.getY())); myframe.repaint(); } } }
窗口监听 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 package cn.linsip.lesson03;import java.awt.*;import java.awt.event.WindowAdapter;import java.awt.event.WindowEvent;public class TestWindow { public static void main (String[] args) { new WindowFrame(); } }class WindowFrame extends Frame { public WindowFrame () { setBackground(Color.BLUE); setBounds(100 ,100 ,400 ,400 ); setVisible(true ); this .addWindowListener( new WindowAdapter() { @Override public void windowClosing (WindowEvent e) { System.out.println("正在关闭" ); System.exit(0 ); } @Override public void windowActivated (WindowEvent e) { System.out.println("已进入当前窗口" ); } } ); } }
键盘监听 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 package cn.linsip.lesson03;import java.awt.*;import java.awt.event.KeyAdapter;import java.awt.event.KeyEvent;public class TestKeyListener { public static void main (String[] args) { new MyFrame(); } }class MyFrame extends Frame { public MyFrame () { setBounds(0 , 0 ,400 ,400 ); setBackground(Color.yellow); setVisible(true ); this .addKeyListener(new KeyAdapter() { @Override public void keyPressed (KeyEvent e) { int keyCode = e.getKeyCode(); System.out.println(keyCode); if (keyCode == KeyEvent.VK_UP) { System.out.println("UP" ); } } }); } }
Swing 窗口、面板 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 package cn.linsip.lesson04;import javax.swing.*;import java.awt.*;public class JFrameDemo01 { public void init () { JFrame jFrame = new JFrame("第一个JFrame窗口" ); jFrame.setBounds(0 ,0 ,400 ,400 ); jFrame.setVisible(true ); JLabel jLabel = new JLabel("我的第一个JFrame窗口" ,SwingConstants.CENTER); jFrame.add(jLabel); jFrame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); jFrame.getContentPane().setBackground(Color.cyan); } public static void main (String[] args) { new JFrameDemo01().init(); } }
弹窗 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 package cn.linsip.lesson04;import javax.swing.*;import java.awt.*;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;public class DialogDemo01 extends JFrame { public DialogDemo01 () { this .setBounds(0 , 0 , 200 , 200 ); this .setVisible(true ); this .setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); Container contentPane = this .getContentPane(); contentPane.setLayout(null ); JButton jButton = new JButton("弹窗" ); jButton.setBounds(10 , 10 , 80 , 50 ); jButton.addActionListener(new ActionListener() { @Override public void actionPerformed (ActionEvent e) { new MyDialog(); } }); contentPane.add(jButton); } public static void main (String[] args) { new DialogDemo01(); } }class MyDialog extends JDialog { public MyDialog () { this .setVisible(true ); this .setBounds(100 , 100 , 200 , 200 ); Container contentPane = this .getContentPane(); contentPane.add(new JLabel("弹窗警告" )); } }
标签 构造方法
Icon 如何画一个标签
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 package cn.linsip.lesson04;import javax.swing.*;import java.awt.*;public class IconDemo01 extends JFrame implements Icon { private int width; private int height; public IconDemo01 () { } public IconDemo01 (int width, int height) { this .width = width; this .height = height; } public void init () { setBounds(0 , 0 , 800 , 600 ); IconDemo01 iconDemo01 = new IconDemo01(15 ,15 ); JLabel jLabel = new JLabel("图标" , iconDemo01 , SwingConstants.CENTER); Container container = getContentPane(); container.add(jLabel); setVisible(true ); setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); } public static void main (String[] args) { new IconDemo01().init(); } @Override public void paintIcon (Component c, Graphics g, int x, int y) { g.fillOval(x, y, width, height); } @Override public int getIconWidth () { return width; } @Override public int getIconHeight () { return height; } }
图片标签ImageIcon 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 package cn.linsip.lesson04;import javax.swing.*;import java.awt.*;import java.net.URL;public class ImageIconDemo extends JFrame { public ImageIconDemo () { URL url = ImageIconDemo.class.getResource("Hush a by little girl.jpg" ); ImageIcon imageIcon = new ImageIcon(url); JLabel jLabel = new JLabel(); jLabel.setIcon(imageIcon); jLabel.setHorizontalAlignment(SwingConstants.CENTER); Container container = getContentPane(); container.add(jLabel); pack(); setVisible(true ); setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); } public static void main (String[] args) { new ImageIconDemo(); } }
面板 JPanel
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 package cn.linsip.lesson05;import javax.swing.*;import java.awt.*;public class JPanelDemo extends JFrame { public JPanelDemo () { Container container = getContentPane(); container.setLayout(new GridLayout(2 ,2 , 10 , 10 )); JPanel jPanel1 = new JPanel(new GridLayout(1 , 3 )); JPanel jPanel2 = new JPanel(new GridLayout(1 , 2 )); JPanel jPanel3 = new JPanel(new GridLayout(2 , 1 )); JPanel jPanel4 = new JPanel(new GridLayout(3 , 2 )); jPanel1.add(new JButton("1" )); jPanel1.add(new JButton("1" )); jPanel1.add(new JButton("1" )); jPanel2.add(new JButton("2" )); jPanel2.add(new JButton("2" )); jPanel3.add(new JButton("3" )); jPanel3.add(new JButton("3" )); jPanel4.add(new JButton("4" )); jPanel4.add(new JButton("4" )); jPanel4.add(new JButton("4" )); jPanel4.add(new JButton("4" )); jPanel4.add(new JButton("4" )); jPanel4.add(new JButton("4" )); container.add(jPanel1); container.add(jPanel2); container.add(jPanel3); container.add(jPanel4); setBounds(0 , 0 , 400 , 400 ); setVisible(true ); setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); } public static void main (String[] args) { new JPanelDemo(); } }
JScroll面板
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 package cn.linsip.lesson01;import javax.swing.*;import java.awt.*;public class JScrollDemo extends JFrame { public JScrollDemo () { Container container = getContentPane(); JTextArea jTextArea = new JTextArea(20 , 50 ); jTextArea.setText("Hello World!" ); JScrollPane jScrollPane = new JScrollPane(jTextArea); container.add(jScrollPane); setBounds(0 , 0 , 400 , 400 ); setVisible(true ); setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); } public static void main (String[] args) { new JScrollDemo(); } }
按钮 图片按钮
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 package cn.linsip.lesson05;import javax.swing.*;import java.awt.*;import java.net.URL;public class JButtonDemo01 extends JFrame { public JButtonDemo01 () { Container container = getContentPane(); URL resource = JButtonDemo01.class.getResource("wechat.jpg" ); Icon icon = new ImageIcon(resource); JButton jButton = new JButton(); jButton.setIcon(icon); jButton.setToolTipText("图片按钮" ); container.add(jButton); setVisible(true ); setSize(400 ,400 ); setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); } public static void main (String[] args) { new JButtonDemo01(); } }
单选按钮
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 package cn.linsip.lesson05;import javax.swing.*;import java.awt.*;public class JButtonDemo02 extends JFrame { public JButtonDemo02 () { Container container = getContentPane(); JRadioButton jRadioButton1 = new JRadioButton("jRB1" ); JRadioButton jRadioButton2 = new JRadioButton("jRB2" ); JRadioButton jRadioButton3 = new JRadioButton("jRB3" ); ButtonGroup buttonGroup = new ButtonGroup(); buttonGroup.add(jRadioButton1); buttonGroup.add(jRadioButton2); buttonGroup.add(jRadioButton3); container.add(jRadioButton1,BorderLayout.CENTER); container.add(jRadioButton2,BorderLayout.NORTH); container.add(jRadioButton3,BorderLayout.SOUTH); setSize(400 ,400 ); setVisible(true ); setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); } public static void main (String[] args) { new JButtonDemo02(); } }
复选按钮
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 package cn.linsip.lesson05;import javax.swing.*;import java.awt.*;public class JButtonDemo03 extends JFrame { public JButtonDemo03 () { Container container = getContentPane(); JCheckBox jCheckBox1 = new JCheckBox("jCheckBox1" ); JCheckBox jCheckBox2 = new JCheckBox("jCheckBox2" ); JCheckBox jCheckBox3 = new JCheckBox("jCheckBox3" ); container.add(jCheckBox1,BorderLayout.NORTH); container.add(jCheckBox2,BorderLayout.CENTER); container.add(jCheckBox3,BorderLayout.SOUTH); setSize(400 ,400 ); setVisible(true ); setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); } public static void main (String[] args) { new JButtonDemo03(); } }
列表 下拉框
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 package cn.linsip.lesson06;import javax.swing.*;import java.awt.*;public class TestComboboxDemo01 extends JFrame { public TestComboboxDemo01 () { Container container = getContentPane(); JComboBox jComboBox = new JComboBox(); jComboBox.addItem("1" ); jComboBox.addItem("2" ); jComboBox.addItem("3" ); jComboBox.addItem(null ); container.add(jComboBox); setSize(100 ,100 ); setVisible(true ); setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); } public static void main (String[] args) { new TestComboboxDemo01(); } }
列表框
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 package cn.linsip.lesson06;import javax.swing.*;import java.awt.*;import java.util.Vector;public class TestComboboxDemo02 extends JFrame { public TestComboboxDemo02 () { Container container = getContentPane(); Vector contents = new Vector(); JList jList = new JList(contents); contents.add("111" ); contents.add("222" ); contents.add("333" ); container.add(jList); setSize(100 ,100 ); setVisible(true ); setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); } public static void main (String[] args) { new TestComboboxDemo02(); } }
应用场景
选择地区,一般是单个选项
列表展示信息,一般是动态扩容
文本框 文本框
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 package cn.linsip.lesson06;import javax.swing.*;import java.awt.*;public class TestTextDemo01 extends JFrame { public TestTextDemo01 () throws HeadlessException { Container container = getContentPane(); JTextField jTextField1 = new JTextField("jTextField1" ); JTextField jTextField2 = new JTextField("jTextField2" ); container.add(jTextField1, BorderLayout.NORTH); container.add(jTextField2, BorderLayout.SOUTH); setSize(100 , 100 ); setVisible(true ); setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); } public static void main (String[] args) { new TestTextDemo01(); } }
密码框
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 package cn.linsip.lesson06;import javax.swing.*;import java.awt.*;public class TestTextDemo02 extends JFrame { public TestTextDemo02 () { Container container = getContentPane(); JPasswordField jPasswordField = new JPasswordField(); jPasswordField.setEchoChar('*' ); container.add(jPasswordField); setSize(100 , 100 ); setVisible(true ); setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); } public static void main (String[] args) { new TestTextDemo02(); } }
文本域
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 package cn.linsip.lesson06;import javax.swing.*;import java.awt.*;public class TestTextDemo03 extends JFrame { public TestTextDemo03 () { Container container = getContentPane(); JTextArea jTextArea = new JTextArea(20 , 50 ); jTextArea.setText("Hello World!" ); JScrollPane jScrollPane = new JScrollPane(jTextArea); container.add(jScrollPane); setSize(100 , 100 ); setVisible(true ); setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); } public static void main (String[] args) { new TestTextDemo03(); } }