GUI编程入门
GUI编程
1. 简介
Gui的核心技术:Swing AWT
为什么现在被淘汰:1.界面不美观 2.需要jre环境
为什么我们要学习:
1.可以写出自己心中想要的一些小工具
2.工作时候。也有可能遇到需要维护swing界面(概率极小)
3.了解MVC架构,了解监听
2. AWT
2.1 Awt介绍
-
包含了很多类和接口
-
元素:窗口、按钮、文本框......
-
java.awt
2.2 组件和容器
1. Frame
单个窗口
package com.gui.lesson01;
import java.awt.*;
public class FirstFrame {
public static void main(String[] args) {
//new出Frame对象 通过看源码看方法
Frame frame = new Frame();
//设置窗口的标题
frame.setTitle("这是我的第一个java图形界面窗口");
//设置可见性
frame.setVisible(true);
//设置窗口大小
frame.setSize(400, 400);
//设置背景颜色
frame.setBackground(Color.white);
//设置窗口弹出的具体位置
frame.setLocation(200, 200);
//设置大小是固定(默认不固定,可扩展)
frame.setResizable(false);
}
}
多个窗口
package com.gui.lesson01;
import java.awt.*;
public class Frames {
public static void main(String[] args) {
ManyFrame frame01 = new ManyFrame(200, 200, 100, 100, Color.BLUE);
ManyFrame frame02 = new ManyFrame(200, 200, 100, 300, Color.white);
ManyFrame frame03 = new ManyFrame(200, 200, 300, 100, Color.yellow);
ManyFrame frame04 = new ManyFrame(200, 200, 300, 300, Color.red);
}
static class ManyFrame extends Frame{
static int id=0;//区分多个窗口
public ManyFrame(int w,int h,int x,int y,Color color){
super();
setTitle("ManyFrame:"+(++id));
setVisible(true);
setBackground(color);
setBounds(x, y, w, h);
setResizable(false);
}
}
}
2. Panel
package com.gui.lesson01;
import java.awt.*;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
//panel(面板) 是在frame(窗口)上来说的
public class TestPanel {
public static void main(String[] args) {
Frame frame = new Frame();
Panel panel = new Panel();
//设置布局
frame.setLayout(null);
//设置窗口的坐标,背景颜色
frame.setBounds(200, 200, 500, 500);
frame.setBackground(new Color(37, 37, 199));
//设置面板的坐标,背景颜色(坐标是相对于窗口来说的)
panel.setBounds(100, 100, 200, 200);
panel.setBackground(new Color(26, 234, 31));
//将panel置于frame中,关联起来
frame.add(panel);
//设置可见性
frame.setVisible(true);
//监听事件,监听窗口关闭事件 System.exit(0);
//用适配器模式
frame.addWindowListener(new WindowAdapter() {
2.3 布局管理器
-
流式布局
package com.gui.lesson01;
import java.awt.*;
public class TestFlowLayout {
public static void main(String[] args) {
Frame frame = new Frame();
//设置按钮
Button button1 = new Button();
Button button2 = new Button();
Button button3 = new Button();
//设置为流式布局,靠左
frame.setLayout(new FlowLayout(FlowLayout.LEFT));
frame.setSize(500, 500);
//把按钮添加上去
frame.add(button1);
frame.add(button2);
frame.add(button3);
frame.setVisible(true);
}
} -
东西南北中
package com.gui.lesson01;
import java.awt.*;
public class TestBorderLayout {
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.setSize(500, 500);
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);
}
}
-
表格(grid)布局
package com.gui.lesson01;
import java.awt.*;
public class TestGridLayout {
public static void main(String[] args) {
Frame frame = new Frame();
Button gird1 = new Button("gird1");
Button gird2 = new Button("gird2");
Button gird3 = new Button("gird3");
Button gird4 = new Button("gird4");
Button gird5 = new Button("gird5");
Button gird6 = new Button("gird6");
frame.setSize(500, 500);
frame.setLayout(new GridLayout(3,2));
frame.add(gird1);
frame.add(gird2);
frame.add(gird3);
frame.add(gird4);
frame.add(gird5);
frame.add(gird6);
frame.setVisible(true);
}
}
作业:打印出这样的:
package com.gui.lesson03;
import java.awt.*;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
public class task {
public static void main(String[] args) {
Frame frame = new Frame();
frame.setVisible(true);
frame.setBounds(100, 100, 400, 400);
frame.setLayout(new GridLayout(2, 1));
Panel p1 = new Panel(new BorderLayout());
Panel p2 = new Panel(new BorderLayout());
Panel p3 = new Panel(new GridLayout(2,1));
Panel p4 = new Panel(new GridLayout(2,2));
//上面
p1.add(new Button("00"),BorderLayout.EAST);
p1.add(new Button("00"),BorderLayout.WEST);
p3.add(new Button("00"));
p3.add(new Button("00"));
p1.add(p3,BorderLayout.CENTER);
frame.add(p1);
//下面
p2.add(new Button("00"),BorderLayout.EAST);
p2.add(new Button("00"),BorderLayout.WEST);
for (int i = 0; i <4 ; i++) {
p4.add(new Button("00"));
}
p2.add(p4,BorderLayout.CENTER);
frame.add(p2);
//监听 控制关闭
frame.addWindowListener(new WindowAdapter() {
2.4 事件监听
事件监听:当某个事件发生的时候,干什么?
一个按钮的监听事件:
package com.gui.lesson04;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
//按下按钮,触发一些事件
public class TestActionEvent {
public static void main(String[] args) {
Frame frame = new Frame("123");
frame.setSize(400, 400);
frame.setVisible(true);
Button button = new Button("aaa");
frame.add(button,new BorderLayout().CENTER);
//因为addActionListener() 需要一个ActionListener,所以我们需要构造一个ActionListener
MyActionListener myActionListener = new MyActionListener();
button.addActionListener(myActionListener);
}
static class MyActionListener implements ActionListener{
多个按钮可以使用一个监听:
package com.gui.lesson04;
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();
frame.setSize(400, 400);
frame.setVisible(true);
Button button01 = new Button("01");
Button button02 = new Button("02");
frame.add(button01,BorderLayout.NORTH);
frame.add(button02,BorderLayout.SOUTH);
MyActionListener myActionListener = new MyActionListener();
button01.addActionListener(myActionListener);
button02.addActionListener(myActionListener);
}
static class MyActionListener implements ActionListener{
//e.getActionCommand() 获得按钮上的信息
2.5 输入框 TextField 监听
package com.gui.lesson05;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class TestTextField {
public static void main(String[] args) {
new TextField1();
}
static class TextField1 extends Frame{
public TextField1(){
setSize(400, 400);
setVisible(true);
TextField textField = new TextField();
add(textField);
//监听文本框输入的文字
MyActionListener1 myActionListener1 = new MyActionListener1();
textField.addActionListener(myActionListener1);
}
}
static class MyActionListener1 implements ActionListener{
2.6 简易计算器 组合+内部类回顾复习
初步形成代码:
package com.gui.lesson06;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
//简易加法计算器
public class TestCalculator {
public static void main(String[] args) {
new Calculator();
}
}
//计算器类
class Calculator extends Frame{
public Calculator() {
//窗口
pack(); //自定义窗口大小
setVisible(true);
//3个文本框
TextField field1 = new TextField(10);
TextField field2 = new TextField(10);
TextField field3 = new TextField(15);
//1个 + 号标签
Label label = new Label("+");
//1个 = 号按钮
Button button = new Button("=");
button.addActionListener(new MyActionListener(field1,field2,field3));
//布局
setLayout(new FlowLayout());
add(field1); add(label); add(field2); add(button); add(field3);
}
}
//监听器类
class MyActionListener implements ActionListener{
//获取3个变量
private TextField field1,field2,field3;
public MyActionListener(TextField field1, TextField field2, TextField field3) {
this.field1=field1;
this.field2=field2;
this.field3=field3;
}
优化后代码(添加组合)
package com.gui.lesson07;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class TextCalculator02 {
public static void main(String[] args) {
new Calculator02().calculator02();
}
}
//计算器类
class Calculator02 extends Frame{
//属性
TextField field01,field02,field03;
//方法
public void calculator02(){
pack();
setVisible(true);
field01= new TextField(10);
field02= new TextField(10);
field03= new TextField(20);
Label label = new Label("+");
Button button = new Button("=");
button.addActionListener(new MyActionListener02(this));
setLayout(new FlowLayout());
add(field01);
add(label);
add(field02);
add(button);
add(field03);
}
}
//监听器类
class MyActionListener02 implements ActionListener{
//获取计算器类这个对象,在一个类中组合另一个类
Calculator02 calculator02=null;
public MyActionListener02(Calculator02 calculator02) {
this.calculator02=calculator02;
}
内部类实现:
package com.gui.lesson08;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class TestCalculator03 {
public static void main(String[] args) {
new Calculator03().calculator02();
}
}
//计算器类
class Calculator03 extends Frame{
//属性
TextField field01,field02,field03;
//方法
public void calculator02(){
pack();
setVisible(true);
field01= new TextField(10);
field02= new TextField(10);
field03= new TextField(20);
Label label = new Label("+");
Button button = new Button("=");
button.addActionListener(new MyActionListener03());
setLayout(new FlowLayout());
add(field01);
add(label);
add(field02);
add(button);
add(field03);
}
//监听器类 内部类:可以直接调用外部类的方法和属性,减少代码的编写
class MyActionListener03 implements ActionListener{
2.7 画笔 paint
package com.gui.lesson09;
import java.awt.*;
public class TestPaint {
public static void main(String[] args) {
new MyPaint().loadFrame();
}
}
class MyPaint extends Frame{
public void loadFrame(){
setSize(500,500);
setVisible(true);
}
2.8 鼠标监听
package com.gui.lesson10;
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 TestPaints {
public static void main(String[] args) {
new MyFrames();
}
}
//自己的类
class MyFrames extends Frame{
//画画需要画笔,需要监听鼠标当前的位置,需要用集合来存储这个点
ArrayList points;
public MyFrames() {
setSize(500, 500);
setVisible(true);
//存鼠标点击的点 集合
points=new ArrayList<>();
//鼠标监听器,针对于窗口而言
this.addMouseListener(new MyMouseListener());
}
2.9 窗口监听
package com.gui.lesson11;
import java.awt.*;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
public class TestWindow {
public static void main(String[] args) {
new MyWindowFrame();
}
}
class MyWindowFrame extends Frame{
public MyWindowFrame(){
setSize(500,500);
setVisible(true);
addWindowListener(
new WindowAdapter() {
2.10 键盘监听
package com.gui.lesson12;
import java.awt.*;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
public class TestKey {
public static void main(String[] args) {
new MyKeyFrame();
}
}
class MyKeyFrame extends Frame{
public MyKeyFrame(){
setSize(500, 500);
setVisible(true);
this.addKeyListener(
new KeyAdapter() {
3. Swing
3.1 窗口、面板
package com.gui.lesson13;
import javax.swing.*;
import java.awt.*;
public class TestJFrame {
public static void main(String[] args) {
new MyJFrame().init();
}
}
class MyJFrame extends JFrame{
//init(); 初始化
public void init(){
setSize(500, 500);
setVisible(true);
//获得一个容器 颜色需要在容器中设置
Container contentPane = this.getContentPane();
contentPane.setBackground(Color.yellow);
//设置文字
JLabel label = new JLabel("大家好");
add(label);
//让文本标签居中,设置水平对齐
label.setHorizontalAlignment(SwingConstants.CENTER);
}
}
3.2 弹窗 dialog
主窗口:
package com.gui.lesson14;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class MainDialog extends JFrame {
public MainDialog(){
this.setVisible(true);
this.setSize(500, 500);
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); //关闭窗口
Container container = this.getContentPane(); //容器
container.setLayout(null); //绝对布局(写固定的坐标)
JButton jButton = new JButton("按下弹出另外一个窗口");
jButton.setBounds(100,100,200, 200);
container.add(jButton);
//按下这个按钮,弹出弹窗 需要一个监听器
jButton.addActionListener(
new ActionListener() {
弹窗:
package com.gui.lesson14;
import javax.swing.*;
import java.awt.*;
public class Dialog extends JDialog {
public Dialog() {
this.setVisible(true);
this.setBounds(300, 300, 400, 400);
Container container = this.getContentPane();
container.setLayout(null);
Label label = new Label("+");
label.setBounds(100, 100, 200, 200);
container.add(label);
}
}
主函数:
package com.gui.lesson14;
public class Test {
public static void main(String[] args) {
new MainDialog();
}
}
3.3 标签
label
new label("***");
图标:ICON
package com.gui.lesson15;
import javax.swing.*;
import java.awt.*;
public class TestIcon {
public static void main(String[] args) {
new MyIcon().init();
}
}
class MyIcon extends JFrame implements Icon{
public void init(){
this.setSize(500,500);
this.setVisible(true);
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
MyIcon myIcon = new MyIcon(20, 20);
//图标放在标签,也可以放在按钮上
JLabel jLabel = new JLabel("+++",myIcon,SwingConstants.CENTER);
Container container = this.getContentPane();
container.add(jLabel);
}
private int width;
private int height;
public MyIcon(){}; //无参构造
public MyIcon(int width, int height){
this.width = width;
this.height = height;
}
图片:ImageIcon
package com.gui.lesson15;
import javax.swing.*;
import java.awt.*;
import java.net.URL;
public class TestImageIcon {
public static void main(String[] args) {
new MyImageIcon();
}
}
class MyImageIcon extends JFrame{
public MyImageIcon() {
this.setSize(500,500);
this.setVisible(true);
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
JLabel jLabel = new JLabel("+++");
//获取图片的url
URL resource = MyImageIcon.class.getResource("/1.jpg");
jLabel.setIcon(new ImageIcon(resource));
jLabel.setHorizontalAlignment(SwingConstants.CENTER); //标签居中
Container container = this.getContentPane();
container.add(jLabel);
}
}
3.4 面板
JPanel
package com.gui.lesson16;
import javax.swing.*;
import java.awt.*;
public class TestJPanel {
public static void main(String[] args) {
new MyJPanel();
}
}
class MyJPanel extends JFrame{
public MyJPanel(){
this.setSize(500,500);
this.setVisible(true);
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
Container container = this.getContentPane();
container.setLayout(new GridLayout(2, 1));
JPanel jPanel = new JPanel(new GridLayout(1,2));
jPanel.add(new JButton("A"));
jPanel.add(new JButton("B"));
jPanel.add(new JButton("C"));
container.add(jPanel);
}
}
能滑动的面板(Scroll):加文本域
package com.gui.lesson16;
import javax.swing.*;
import java.awt.*;
public class TestJScroll {
public static void main(String[] args) {
new MyJScroll();
}
}
class MyJScroll extends JFrame{
public MyJScroll() {
this.setSize(500, 500);
this.setVisible(true);
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
Container container = this.getContentPane();
//文本域
JTextArea jTextArea = new JTextArea(10, 50);
jTextArea.setText("你好啊");
//Scroll面板
JScrollPane jScrollPane = new JScrollPane(jTextArea);
container.add(jScrollPane);
}
}
//将文本域添加到scroll面板,然后将面板添加到容器
3.5 按钮
图片按钮 ImageIcon
package com.gui.lesson17;
import javax.swing.*;
import java.awt.*;
import java.net.URL;
public class TestPicture {
public static void main(String[] args) {
new MyPictureButton();
}
}
class MyPictureButton extends JFrame{
public MyPictureButton(){
Container container = this.getContentPane();
//把图片变成图标,添加到按钮上
URL url = MyPictureButton.class.getResource("/1.jpg");
JButton jButton = new JButton();
jButton.setIcon(new ImageIcon(url));
jButton.setToolTipText("图片按钮");
//把按钮add到容器
container.add(jButton);
this.setVisible(true);
this.setSize(300, 300);
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
}
单选按钮 radio
package com.gui.lesson17;
import javax.swing.*;
import java.awt.*;
public class TestRadio {
public static void main(String[] args) {
new MyRadioButton();
}
}
class MyRadioButton extends JFrame {
public MyRadioButton(){
Container container = this.getContentPane();
//单选按钮
JRadioButton jRadioButton1 = new JRadioButton("jRadioButton1");
JRadioButton jRadioButton2 = new JRadioButton("jRadioButton2");
JRadioButton jRadioButton3 = new JRadioButton("jRadioButton3");
//由于单选框只能选择一个,要进行分组
ButtonGroup buttonGroup = new ButtonGroup();
buttonGroup.add(jRadioButton1);
buttonGroup.add(jRadioButton2);
buttonGroup.add(jRadioButton3);
container.add(jRadioButton1,BorderLayout.SOUTH);
container.add(jRadioButton2,BorderLayout.CENTER);
container.add(jRadioButton3,BorderLayout.NORTH);
this.setVisible(true);
this.setSize(300, 300);
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
}
复选按钮 checkbox
package com.gui.lesson17;
import javax.swing.*;
import java.awt.*;
public class TestCheckBox {
public static void main(String[] args) {
new MyCheckBoxButton();
}
}
class MyCheckBoxButton extends JFrame {
public MyCheckBoxButton(){
Container container = this.getContentPane();
//多选按钮
JCheckBox jCheckBox1 = new JCheckBox("jCheckBox1");
JCheckBox jCheckBox2 = new JCheckBox("jCheckBox2");
JCheckBox jCheckBox3 = new JCheckBox("jCheckBox3");
container.add(jCheckBox1,BorderLayout.SOUTH);
container.add(jCheckBox2,BorderLayout.CENTER);
container.add(jCheckBox3,BorderLayout.NORTH);
this.setVisible(true);
this.setSize(300, 300);
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
}
3.6 列表
下拉框 ComBoBox
package com.gui.lesson18;
import javax.swing.*;
import java.awt.*;
public class TestComBoBox {
public static void main(String[] args) {
new MyComBoBox();
}
}
class MyComBoBox extends JFrame{
public MyComBoBox() {
Container container = this.getContentPane();
//下拉框
JComboBox jComboBox = new JComboBox();
jComboBox.addItem("中国");
jComboBox.addItem("美国");
jComboBox.addItem("俄罗斯");
jComboBox.addItem("英国");
container.add(jComboBox);
this.setVisible(true);
this.setSize(500, 500);
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
}
列表 List
package com.gui.lesson18;
import javax.swing.*;
import java.awt.*;
public class TestList {
public static void main(String[] args) {
new MyList();
}
}
class MyList extends JFrame{
public MyList(){
Container container = this.getContentPane();
//生成列表的内容 静态数组
String[] contents ={"中国","美国","俄罗斯","英国"};
//列表中需要放入的内容
JList jList = new JList(contents);
container.add(jList);
this.setVisible(true);
this.setSize(500, 500);
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
}
3.7 文本框
文本框 TextField
package com.gui.lesson19;
import javax.swing.*;
import java.awt.*;
public class TestTextField {
public static void main(String[] args) {
new MyTextField();
}
}
class MyTextField extends JFrame {
public MyTextField() {
Container container = this.getContentPane();
//文本框
JTextField jTextField01 = new JTextField("你好,中国!");
JTextField jTextField02 = new JTextField("你好,世界!");
container.add(jTextField01,BorderLayout.NORTH);
container.add(jTextField02,BorderLayout.SOUTH);
this.setVisible(true);
this.setSize(500, 500);
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
}
密码框 PasswordField
package com.gui.lesson19;
import javax.swing.*;
import java.awt.*;
public class TestPasswordField {
public static void main(String[] args) {
new MyPasswordField();
}
}
class MyPasswordField extends JFrame {
public MyPasswordField() {
Container container = this.getContentPane();
//密码框
JPasswordField jPasswordField = new JPasswordField();
jPasswordField.setEchoChar('*');
container.add(jPasswordField,BorderLayout.CENTER);
this.setVisible(true);
this.setSize(500, 500);
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
}
文本域 TextArea
package com.gui.lesson19;
import javax.swing.*;
import java.awt.*;
public class TestTextArea{
public static void main(String[] args) {
new MyTextArea();
}
}
class MyTextArea extends JFrame {
public MyTextArea() {
Container container = this.getContentPane();
JTextArea jTextArea = new JTextArea(10, 50);
jTextArea.setText("你好,中国!");
//ScrollPane面板
JScrollPane jScrollPane = new JScrollPane(jTextArea);
container.add(jScrollPane);
this.setVisible(true);
this.setSize(500, 500);
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
}