简介

GUI的核心技术:AWT(是Swing 的前身) Swing

  • 不流行的原因

    • 界面不美观
    • 运行需要jre环境(可能一个项目的大小比jre还要大)
  • 为什么我们需要学习
    • 了解MVC架构和监听

AWT

介绍

全名:abstract windows tools

awt包含了很多类和接口

里面有各种各样的元素 eg:窗口按钮文本框

用到java.awt.*

组件和容器(核心类)

Frame

窗口是我们日常使用软件最常看到的

import java.awt.*;

Frame frame=new Frame("JAVA WINDOWS");
//需要设置可见性 w h
frame.setVisible(true);
//设置窗口大小
frame.setSize(400,900);
Color color = new Color(128,128,128);
frame.setBackground(color);
//弹出的初始位置
frame.setLocation(new Point(200,300));
//设置大小固定,窗口不能拉伸
frame.setResizable(false);

Panel

可以看成一个空间,但是不能单独存在,必须添加到某个容器中

public static void main(String[] args) {
Frame frame = new Frame();
Panel panel = new Panel();
//设置布局
frame.setLayout(null);
//坐标
frame.setBounds(300,300,500,500);
frame.setBackground(Color.yellow);
//panel设置坐标,相对于frame
panel.setBounds(50,50,400,400);
panel.setBackground(Color.blue);
frame.add(panel);
frame.setVisible(true);
//监听事件,解决点击窗口关闭
//WindowAdapter 适配器模式,只需写需要的override
//WindowListener 实现所有的监听,很多个override
//匿名内部类
frame.addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
//先隐藏再关闭
setVisible(false);
//结束程序
System.exit(0);
}
});
}

备注:如果是JFrame,关闭窗口可以不用写一个监听事件,一行代码直接搞定

布局管理器

  • ​ 流式布局

    public static void main(String[] args) {
    Frame frame = new Frame();
    //组件 按钮
    Button button01 = new Button("button01");
    Button button02 = new Button("button02");
    Button button03 = new Button("button03");
    frame.setBounds(300,300,500,500);
    frame.setVisible(true);
    //设置为流式布局
    frame.setLayout(new FlowLayout(FlowLayout.LEFT));
    frame.add(button01);
    frame.add(button02);
    frame.add(button03);
    }

  • ​ 东西南北中

    public static void main(String[] args) {
    Frame frame = new Frame();
    Button button1 = new Button("East");
    Button button2 = new Button("West");
    Button button3 = new Button("South");
    Button button4 = new Button("North");
    Button button5 = new Button("Center");
    frame.add(button1,BorderLayout.EAST);
    frame.add(button2,BorderLayout.WEST);
    frame.add(button3,BorderLayout.SOUTH);
    frame.add(button4,BorderLayout.NORTH);
    frame.add(button5,BorderLayout.CENTER);
    frame.setVisible(true);
    frame.setBounds(500,500,500,500);
    }

  • ​ 表格布局 Grid

    public static void main(String[] args) {
    Frame frame = new Frame();
    Button button1 = new Button("1");
    Button button2 = new Button("2");
    Button button3 = new Button("3");
    Button button4 = new Button("4");
    Button button5 = new Button("5");
    Button button6 = new Button("6");
    frame.setVisible(true);
    frame.setBounds(500,500,500,500);
    frame.setLayout(new GridLayout(3,2));
    frame.add(button1);
    frame.add(button2);
    frame.add(button3);
    frame.add(button4);
    frame.add(button5);
    frame.add(button6);
    }

  • 综合练习

public static void main(String[] args) {
Frame frame = new Frame();
frame.setSize(400,300);
frame.setLocation(300,400);
frame.setVisible(true);
Panel p1 = new Panel(new BorderLayout());
Panel p2 = new Panel(new GridLayout(2,1));
p1.add(new Button("east"),BorderLayout.EAST);
p1.add(new Button("west"),BorderLayout.WEST);
p2.add(new Button("center1"));
p2.add(new Button("center2"));
p1.add(p2,BorderLayout.CENTER);
frame.add(p1);
}

事件监听

简单的例子

public class TestFrame {
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.setVisible(true);
frame.pack();//窗口自适应
}
}
class MyActionListener implements ActionListener{
@Override
public void actionPerformed(ActionEvent e) {
System.out.println("点击了一下,在控制台输出");
}
}

输入框监听

public class TestFrame {
public static void main(String[] args) {
new MyFrame();
}
}
class MyFrame extends Frame{
public MyFrame(){
TextField textField = new TextField();
add(textField); //监听文本输入框的文字
MyActionListener myActionListener = new MyActionListener();
textField.addActionListener(myActionListener);
//设置替换编码 密码处理
textField.setEchoChar('*');
setVisible(true);
pack();
}
}
class MyActionListener implements ActionListener{
@Override
public void actionPerformed(ActionEvent e) {
//获得一些资源,返回一个对象
TextField field = (TextField)e.getSource();
//获得输入框中的文本
System.out.println(field.getText());
field.setText("");
}
}

简易计算器

方法一:组合写法

import java.awt.*;
import java.awt.event.*;
//简易计算
public class TestFrame {
public static void main(String[] args) {
new Calculator().loadFrame();
}
}
//计算类
class Calculator extends Frame{
TextField num1,num2,num3;
public void loadFrame(){
//3个文本框
num1 = new TextField(10);//10表示输入最大字符数,框的大小也会改变
num2 = new TextField(10);
num3 = new TextField(12);
num1.setText("");
num2.setText("");
num3.setText("");
//1个按钮
Button button = new Button("=");
button.addActionListener(new MyActionListener(this));
//1个标签
Label label = new Label("+");
//流式布局
setLayout(new FlowLayout());
add(num1);
add(label);
add(num2);
add(button);
add(num3);
pack();
setVisible(true);
}
}
//监听类
class MyActionListener implements ActionListener{
//获得计算器对象,在一个类中组合另外一个类
Calculator calculator = null; public MyActionListener(Calculator calculator){
this.calculator=calculator;
}
@Override
public void actionPerformed(ActionEvent e) {
//清空
if("".equals(calculator.num3.getText())!=true){
calculator.num1.setText("");
calculator.num2.setText("");
calculator.num3.setText("");
}else{
//获得加数和被加数
int a=Integer.parseInt(calculator.num1.getText());
int b=Integer.parseInt(calculator.num2.getText());
//将+结果放入第三个框
calculator.num3.setText(""+(a+b));
}
}
}

方法二:内部类写法(推荐)

内部类最大的好处就是畅通无阻访问外部类

//简易计算
public class TestFrame {
public static void main(String[] args) {
new Calculator().loadFrame();
}
}
//计算类
class Calculator extends Frame{
TextField num1,num2,num3;
public void loadFrame(){
//3个文本框
num1 = new TextField(10);//10表示输入最大字符数,框的大小也会改变
num2 = new TextField(10);
num3 = new TextField(12);
num1.setText("");
num2.setText("");
num3.setText("");
//1个按钮
Button button = new Button("=");
button.addActionListener(new MyActionListener());
//1个标签
Label label = new Label("+");
//流式布局
setLayout(new FlowLayout());
add(num1);
add(label);
add(num2);
add(button);
add(num3);
pack();
setVisible(true);
}
//监听类
private class MyActionListener implements ActionListener{
@Override
public void actionPerformed(ActionEvent e) {
//清空
if("".equals(num3.getText())!=true){
num1.setText("");
num2.setText("");
num3.setText("");
}else{
//获得加数和被加数
int a=Integer.parseInt(num1.getText());
int b=Integer.parseInt(num2.getText());
//将+结果放入第三个框
num3.setText(""+(a+b));
}
}
}
}

画笔

public class TestFrame {
public static void main(String[] args) {
new MyFrame().loadFrame();
}
}
class MyFrame extends Frame{
public void loadFrame(){
setBounds(200,200,600,500);
setVisible(true);
}
//画笔
@Override
public void paint(Graphics g) {
//设置画笔颜色
g.setColor(Color.blue);
g.fillOval(100,100,100,100);
//养成习惯,画笔用完,还原最初的颜色
g.setColor(Color.black);
}
}

鼠标监听

class MyFrame extends Frame{
//画画需要画笔
//需要监听鼠标当前的位置
//需要集合存储这个点
ArrayList<Point> points;
public MyFrame(String title) {
super(title);
setBounds(200,200,400,300);
points = new ArrayList<Point>();
this.addMouseListener(new MyMouseListener());
setVisible(true);
}
public class MyMouseListener extends MouseAdapter {
@Override
public void mousePressed(MouseEvent e) {
MyFrame myFrame =(MyFrame)e.getSource();
points.add(new Point(e.getX(),e.getY()));
//每次点击鼠标都重新画一遍
myFrame.repaint();
}
}
@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 class TestFrame {
public static void main(String[] args) {
new MyFrame(); }
}
class MyFrame extends Frame{
public MyFrame() {
setBounds(100,200,300,300);
setVisible(true);
this.addKeyListener(new KeyAdapter() {
//键盘按下
@Override
public void keyPressed(KeyEvent e) {
int keyCode = e.getKeyCode();
if(keyCode==KeyEvent.VK_UP){
System.out.println("你按下了上键");
}
}
});
}
}

Swing

窗口 JFrame

public class TestJFrame {
public void init(){
//顶级窗口
JFrame frame = new JFrame("JFrame窗口");
frame.setVisible(true);
frame.setBounds(100,100,200,200);
/*注意必须要在容器中加入颜色,单纯下面这样写,无法生效
frame.setBackground(Color.yellow);*/
//获得一个容器
Container container = frame.getContentPane();
container.setBackground(Color.yellow);
//设置文字Jlabel
JLabel label = new JLabel("欢迎你");
//文本标签居中
label.setHorizontalAlignment(SwingConstants.CENTER);
frame.add(label);
//关闭事件 相当于awt的窗口监听 代码少多了吧
frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
} public static void main(String[] args) {
new TestJFrame().init();
}
}

弹窗 JDialog

public class TestJFrame extends JFrame{
public TestJFrame() {
this.setVisible(true);
this.setSize(700,500);
//JFrame 放东西 容器
Container container = this.getContentPane();
//绝对布局
container.setLayout(null); JButton button = new JButton("点击弹出对话框");//创建把
button.setBounds(30,30,200,50);
container.add(button);
//点击按钮弹出弹窗
button.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
//弹窗
new MyJDialog();
}
});
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
} public static void main(String[] args) {
new TestJFrame();
}
}
class MyJDialog extends JDialog{
public MyJDialog(){
this.setVisible(true);
this.setBounds(100,100,500,500);
Container container = this.getContentPane();
container.add(new JLabel("我是一个弹窗"));
}
}

图标

import javax.swing.*;
import java.awt.*; public class IconDemo extends JFrame implements Icon{
private int width;
private int height;
public static void main(String[] args) {
new IconDemo().init();
}
public void init(){
IconDemo iconDemo = new IconDemo(15,15);
//图标可以放在按钮上或者标签上
JLabel label = new JLabel("icontest",iconDemo,SwingConstants.CENTER);
Container container = getContentPane();
container.add(label); this.setVisible(true);
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
public IconDemo(){ }
public IconDemo(int width,int height){
this.width=width;
this.height=height;
}
@Override
public void paintIcon(Component c, Graphics g, int x, int y) {
g.fillOval(x,y,width,height);
} @Override
public int getIconWidth() {
return this.width;
} @Override
public int getIconHeight() {
return this.height;
}
}

图片

import javax.swing.*;
import java.awt.*;
import java.net.URL; public class ImageDemo extends JFrame{
private int width;
private int height;
public static void main(String[] args) {
new ImageDemo();
}
public ImageDemo(){ JLabel label = new JLabel("Imageicon");
//这个url需要注意 容易写错找不到
URL url= ImageDemo.class.getResource("icon.png"); ImageIcon imageIcon = new ImageIcon(url); label.setIcon(imageIcon);
label.setHorizontalAlignment(SwingConstants.CENTER); Container container = getContentPane();
container.add(label);
setVisible(true);
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
setBounds(0,0,200,200);
}
}

面板

public class TestJFrame extends JFrame{
public TestJFrame(){
//获得容器
Container container = this.getContentPane();
//后面的参数是间距
container.setLayout(new GridLayout(2,1,10,10));
JPanel panel1 = new JPanel(new GridLayout(1,3));
panel1.add(new Button("1"));
panel1.add(new Button("1"));
panel1.add(new Button("1"));
container.add(panel1);
this.setVisible(true);
this.setSize(500,500);
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
new TestJFrame();
}
}

下拉框

public TestJFrame(){
//获得容器
Container container = this.getContentPane();
JComboBox jComboBox = new JComboBox();
jComboBox.addItem("下拉选项一");
jComboBox.addItem("下拉选项二");
jComboBox.addItem("下拉选项三");
container.add(jComboBox);
// System.out.println(jComboBox.getSelectedIndex());
// System.out.println(jComboBox.getSelectedItem());
this.setVisible(true);
this.setSize(500,500);
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
new TestJFrame();
}

文本域/JscrollPan滚动条

public class TestJFrame extends JFrame{
public TestJFrame(){
//获得容器
Container container = this.getContentPane();
//文本域
JTextArea jTextArea = new JTextArea(20,50);
jTextArea.setText("大家好,你们的DJ同学又来喽");
JScrollPane jScrollPan = new JScrollPane(jTextArea);
container.add(jScrollPan);
this.setVisible(true);
this.setSize(500,500);
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
new TestJFrame();
}
}

文本框

JTextField

密码框

JPasswordField

GUI基础知识点的更多相关文章

  1. fastclick 源码注解及一些基础知识点

    在移动端,网页上的点击穿透问题导致了非常糟糕的用户体验.那么该如何解决这个问题呢? 问题产生的原因 移动端浏览器的点击事件存在300ms的延迟执行,这个延迟是由于移动端需要通过在这个时间段用户是否两次 ...

  2. .NET基础知识点

    .NET基础知识点   l  .Net平台  .Net FrameWork框架   l  .Net FrameWork框架提供了一个稳定的运行环境,:来保障我们.Net平台正常的运转   l  两种交 ...

  3. JavaScript 开发者经常忽略或误用的七个基础知识点(转)

    JavaScript 本身可以算是一门简单的语言,但我们也不断用智慧和灵活的模式来改进它.昨天我们将这些模式应用到了 JavaScript 框架中,今天这些框架又驱动了我们的 Web 应用程序.很多新 ...

  4. JavaScript 开发者经常忽略或误用的七个基础知识点

    JavaScript 本身可以算是一门简单的语言,但我们也不断用智慧和灵活的模式来改进它.昨天我们将这些模式应用到了 JavaScript 框架中,今天这些框架又驱动了我们的 Web 应用程序.很多新 ...

  5. Unity3D入门之GUI基础以及常用GUI控件使用(2)

    1.GUI基础 (1)GUI部分是每帧擦除重绘的,只应该在OnGUI中绘制GUI,按钮:GUILayout.Button(“Hello”); 只读标签:GUILayout.Label() (2)修改控 ...

  6. JavaScript开发者常忽略或误用的七个基础知识点

    JavaScript 本身可以算是一门简单的语言,但我们也不断用智慧和灵活的模式来改进它.昨天我们将这些模式应用到了 JavaScript 框架中,今天这些框架又驱动了我们的 Web 应用程序.很多新 ...

  7. JavaScript语言基础知识点图示(转)

    一位牛人归纳的JavaScript 语言基础知识点图示. 1.JavaScript 数据类型 2.JavaScript 变量 3.Javascript 运算符 4.JavaScript 数组 5.Ja ...

  8. JavaScript 语言基础知识点总结

    网上找到的一份JavaScript 语言基础知识点总结,还不错,挺全面的. (来自:http://t.cn/zjbXMmi @刘巍峰 分享 )  

  9. c语言学习之基础知识点介绍(三):scanf函数

    本节继续介绍c语言的基础知识点. scanf函数:用来接收用户输入的数据. 语法:scanf("格式化控制符",地址列表); 取地址要用到取地址符:&(shift+7) 例 ...

随机推荐

  1. 医学图像配准 | Voxelmorph 微分同胚 | MICCAI2019

    文章转载:微信公众号「机器学习炼丹术」 作者:炼丹兄(已授权) 联系方式:微信cyx645016617(欢迎交流) 论文题目:'Unsupervised Learning for Fast Proba ...

  2. android分析之Binder 02

    分析Java层的ServiceManager,看看Binder在Java层是如何实现的. public final class ServiceManager { private static fina ...

  3. Nodejs学习笔记(4) 文件操作 fs 及 express 上传

    目录 参考资料 1. fs 模块 1.1 读取文件fs.readFile 1.2 写入文件fs.writeFile 1.3 获取文件信息fs.stat 1.4 删除文件fs.unlink 1.5 读取 ...

  4. 王兴:为什么中国的 ToB 企业都活得这么惨?

    本文节选自美团创始人王兴的内部讲话.在讲话中,王兴罕见地分享了他对全球和中国宏观经济的理解,谈了他对 TO B 业务的深度思考. 我们今天讲一下餐饮生态业务部,以及对我们整个公司在整个业务发展过程中的 ...

  5. 《逆向工程核心原理》——通过调试方式hook Api

    1.附加目标进程, 2.CREATE_PROCESS_DEBUG_EVENT附加事件中将目标api处设置为0xcc(INT 3断点) 3.EXCEPTION_DEBUG_EVENT异常事件中,首先判断 ...

  6. 3、MyBatis教程之CURD操作

    4.CURD操作 1.查询 根据用户 Id查询用户 在UserMapper中添加对应方法 public interface UserMapper { List<User> getUserL ...

  7. 1、MyBatis教程之环境准备和简介

    1.环境准备 jdk 8 + MySQL 5.7.19 maven-3.6.1 IDEA 学习前需要掌握: JDBC MySQL Java 基础 Maven Junit Idea快捷键 一键格式化代碼 ...

  8. 搞懂 ZooKeeper 集群的数据同步

    本文作者:HelloGitHub-老荀 Hi,这里是 HelloGitHub 推出的 HelloZooKeeper 系列,免费开源.有趣.入门级的 ZooKeeper 教程,面向有编程基础的新手. 项 ...

  9. vue文本滚动组件

    看了好多网上的文本组件,发现好多都有这样那样的问题:特别是滚动的时候失真的感觉,今天整合了文本滚动的方式用CSS的 animation写出一套组件:VUE项目直接用.感觉有用的朋友关注下   效果图, ...

  10. PAT (Advanced Level) Practice 1031 Hello World for U (20 分) 凌宸1642

    PAT (Advanced Level) Practice 1031 Hello World for U (20 分) 凌宸1642 题目描述: Given any string of N (≥5) ...