阿猛学习笔记java十四GUI_qq_40298829的博客-程序员秘密

技术标签: java  

十九GUI

图形用户界面

1.AWT

abstract Windowing Toolkit 抽象窗口工具包

用来建立和设置java的图形用户界面的基本工具

AWT所有工具类都在awt包中,用来建立与平台无关的GUI类,这些类被称为组件

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-7iPwKMVE-1603897518226)(C:\Users\lgm\AppData\Roaming\Typora\typora-user-images\1603846286463.png)]

1.组件Conponent

在图形界面中,按钮、标签、菜单等,这些组件都会在一个窗体上显示

组件类(例如:按钮、文本框等)都是从 Component 和
MenuComponent 扩展而来的,这些类会继承这两个类的公共操作。

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-o8hkbrnd-1603897518231)(C:\Users\lgm\AppData\Roaming\Typora\typora-user-images\1603846370224.png)]

2.容器Container

所有的组件都放在容器之中

所有容器都是component的子类

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-k5E7tsfr-1603897518234)(C:\Users\lgm\AppData\Roaming\Typora\typora-user-images\1603846567659.png)]

3.布局管理器LayoutManger

使容器中的组件按照指定的位置进行摆放,保证版面不会混乱

所有布局管理器都是LanyoutManager的子类

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-vTHungPl-1603897518237)(C:\Users\lgm\AppData\Roaming\Typora\typora-user-images\1603847272748.png)]

2.Swing

AWT大量的引入Window函数,所以被称为重量级组件,在java2中提供了轻量级的图形界面组件–Swing

java中所有Swing都保存在javax.swing包中,是一个扩展包,所有组件时从JComponent扩展出来的

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-ZsecTX4i-1603897518239)(C:\Users\lgm\AppData\Roaming\Typora\typora-user-images\1603847867834.png)]

3.基本容器

1.JFrame

构造方法

public JFrame() 创建一个窗体对象

public JFrame(String title) 创建一个窗体对象并指定标题

常用方法

public void setSize(int width,int height) 设置窗体大小
public void setSize(Dimension d) 通过 Dimension 设置窗体大小
public void setLocation(int x,int y) 设置组件的显示位置
public void setLocation(Point p) 通过 Point 来设置组件的显示位置

public void setBounds(int x,int y,int width,int height)

public void setBackground(Color c) 设置窗体的背景颜色

public void setVisible(boolean b) 显示或隐藏组件
public Component add(Component comp) 向容器中增加组件
public void setLayout(LayoutManager mgr) 设置布局管理器,如果设置为 null表示不使用
public void pack() 调整窗口大小,以适合其子组件的首选大小和布局
public Container getContentPane() 返回此窗体的容器对象

2.Dimension

在JFrame设置窗体的大小,也可使用Dimension类,封装组件的宽度和高度

常用方法

public Dimension() 创建一个 Dimension 实例
public voidsetSize(double width,double height) 设置显示的宽和

设置显示的宽和高
public double getWidth() 返回组件的宽
public double getHeight() 返回组件的高

3.Point

设置窗体的X,Y坐标,对组件的X,Y坐标进行了封装

常用方法

public Point() 在坐标原点创建对象
public Point(int x,int y) 在指定的坐标点创建对象
public void setLocation(double x,doubley) 设置 X、Y 坐标
public void setLocation(int x,int y) 设置 X、Y 坐标
public void move(int x,int y) 将此组件移动到指定坐标位置,此方法与 setLocation(int, int)相
public void translate(int dx,int dy) 平移(x, y)位置的点,沿 x 轴平移dx,沿 y 轴平移 dy,移动后得到点
(x + dx, y + dy)

public class T01Frame {
    
public static void main(String[] args) {
    
	Frame f=new Frame("窗口");
	f.setSize(300, 200);
	f.setLocation(100, 100);
	f.setBackground(Color.BLUE);
	f.show();
	
	JFrame jf=new JFrame();
	jf.setTitle("JFrame--jf");
	jf.setSize(300, 250);//设置窗口大小
	jf.setLocation(500, 200);//设置窗口位置
	jf.getContentPane().setBackground(Color.BLUE);//设置窗口颜色
	jf.show();
	
	jf.setBounds(400, 300, 120, 432);//同时设置窗口大小和位置
	
	//通过Dimension设置窗口大小
	Dimension d=new Dimension();
	d.setSize(400, 700);
	jf.setSize(d);
	//通过Point对象设置坐标
	Point p=new Point();
	p.setLocation(100, 200);
	f.setLocation(p);
	}
}

4.标签组件

1.Jlabel

用于显示信息,不能直接修改显示内容,可通过Container的add()方法加入容器之中

常用常量

public static final int LEFT 常量 标签文本左对齐
public static final int CENTER 常量 标签文本居中对齐
public static final int RIGHT 常量 标签文本右对齐

构造方法

public JLabel() 创建一个 JLabel 对象
public JLabel(String text) 创建一个标签并指定文本内容,默认为左对齐
public Label(String text,int alignment) 创建一个标签并指定文本内容和对齐方式JLable.LEFT/RIGHT/CENTER
public JLabel(String text,Icon icon,int horizontalAlignment) 创建具有指定文本,图像和水平对齐,方式

public JLabel(Icon image,int horizontalAlignment) 创建具有指定图像和水平对齐方式 JLabel 实例

常用方法

public void setText(String text) 设置标签的文本
public String getText() 取得标签的文本
public void setAlignment(int alignment) 设置标签的对齐方式
public void setIcon(Icon icon) 设置指定的图象

public void setFont(Font font) 设置指定的图象

Icon

将一个图像设置到JLabel中,直接使用Icon和ImageIcon子类即可

构造方法

public ImageIcon(byte[] imageData) 将保存图片信息的byte数组设置到ImageIcon 中
public ImageIcon(String filename) 通过文件名称创建 ImageIcon对象
public ImageIcon(String filename,String description) 设置图片路径以及图片的简单描述

Font

操作字体相关属性,可直接使用public void setFont(Font f)

常用常量

public static final int BOLD 常量 文字显示为粗体
public static final int ITALIC常量 文字显示风格为斜体
public static final int PLAIN常量 文字显示风格为普通样式

构造方法

public Font(String name,int style,int size)实例化对象,指定显示风格及大小

常用方法

public String getFontName() 普通 得到字体的名称

public static void main(String[] args) {
    
		//容器属性设置
		JFrame jf=new JFrame("窗口");
		jf.setBounds(100, 100, 600, 1000);
		jf.setTitle("JLable");
		jf.getContentPane().setBackground(Color.RED);
	
		//设置标签属性
		JLabel jl=new JLabel();
		jl.setText("姓名:");
		jf.add(jl);//将标签加入组件中
		//设置标签图片并放入构造方法中
		Icon i=new ImageIcon("C:\\Users\\admin\\Pictures\\Camera Roll\\preview (2).jpg");
		JLabel jl1=new JLabel("图片", i, JLabel.RIGHT);
		jl1.setSize(100, 200);
		jf.add(jl1);
		
		JLabel jl2=new JLabel("多个样式");
		jf.add(jl2);
		Font f=new Font("font", Font.BOLD+Font.ITALIC+Font.LAYOUT_LEFT_TO_RIGHT, 54);
		jl2.setFont(f);
		jf.show();
	}
2.JButton

构造方法

public JButton() 创建一个 Button 对象
public JButton(String label) 创建一个 Button 对象,同时指定其显示内容
public JButton(Icon icon) 创建一个带图片的按钮
public JButton(String text,Icon icon) 创建一个带图片和文字的按钮

常用方法

public void setLabel(String label) 设置 Button 的显示内容
public String getLabel() 普通 得到 Button 的显示内容
public void setBounds(int x,int y,int width,int height) 设置组件的大小及显示方式
public void setMnemonic(int mnemonic) 设置按钮的快捷键

public class T03Jbutton {
    
	public static void main(String[] args) {
    
		JFrame jf=new JFrame();
		jf.setBounds(100, 100, 200, 300);
		jf.setBackground(Color.GREEN);
		
	//普通按钮
		
		JButton jb1=new JButton("jbutton");
		jb1.setBackground(Color.RED);
		jb1.setSize(100, 20);
		jf.add(jb1);
	//图片按钮
		Icon i=new ImageIcon("C:\\Users\\admin\\Pictures\\Camera Roll\\preview (2).jpg");
		JButton jb2=new JButton("图片按钮",i);
		jb2.setBounds(120, 120, 100, 50);
		jf.add(jb2);
		jf.show();	
	}
3.JtextComponent

JtextComponent 类的常用方法
public String getText() 返回文本框的所有内容
public String getSelectedText() 返回文本框中选定的内容
public int getSelectionStart() 返回文本框选定内容的开始点
public int getSelectionEnd() 返回文本框选定内容的结束点
public void selectAll() 选择此文本框的所有内容
public void setText(String t) 设置此文本框的内容
public void select(intselectionStart,intselectionEnd) 将指定开始点和结束点之间的内容选定
public void setEditable(boolean b) 设置此文本框是否可编辑

单行文本框JTextField

实现一个单行的输入文本

构造方法

public JTextField() 构造一个默认的文本框
public JTextField(String text) 构造构造一个指定文本内容的文本框

常用方法

public voidsetColumns(int columns) 普通设置显示长度

密码为本框JPasswordField

如果现在需要将回显的内容设置成其他字符

构造方法

public JPasswordField()
public JPasswordField(Stringtext) 构 造 指 定 内 容 PasswordField对象
public charsetEchoChar() 构造设置回显的字符,默认为“*”
public chargetEchoChar() 得到回显的字符
public char[]getPassword() 得到此文本框的所有内容

多行文本框JTextArea

构造方法

public JTextArea() 构造构造文本域,行数和列数为 0
public JTextArea(introws,int columns) 构造文本域,指定文本域的行数和列数
public JTextArea(Stringtext,int rows,intcolumns) 指定构造文本域的内容、行数和列数

常用方法

public void append(String str) 在文本域中追加内容
public void replaceRange(Stringstr,int start,int end) 替换文本域中指定范围的内容
public voidinsert(String str,intpos) 在指定位置插入文本

public voidsetLineWrap(booleanwrap) 设置换行策略

public class T04JText {
    
	public static void main(String[] args) {
    
		
		JFrame jf=new JFrame("容器组件");
		jf.setBounds(10,10, 400, 400);
		jf.setBackground(Color.CYAN);
		
		JTextField jtf=new JTextField("普通文本框");
		jtf.setEditable(true);
		jtf.setColumns(300);
		jtf.setLocation(300, 300);
		jf.add(jtf);
		
		JPasswordField jpf=new JPasswordField("请输入密码");
		
		jpf.setSize(100, 60);
		jpf.setLocation(200, 200);
		jf.add(jpf);
		
		JTextArea jta=new JTextArea("多行文本框",10,10);
		jta.append("append");
		jf.add(jta);
		jta.setLocation(100,100);
		jf.show();
	}
}

5.布局管理器

对应接口LayoutManager

可以管理组件的显示位置

1.流式布局FlowLayout

所有组件像流水一样依次进行排列

常用常量

public static final intCENTER居中对齐

public static final intLEFT左对齐

public static inal intRIGHT右对齐

public static final intLEADING与容器的开始端对齐方式一样

public static final intTRAILING与容器的结束端对齐方式一样

构造方法

public FlowLayout() 构造一个新的 FlowLayout,居中对齐,默认的水平和垂直间距是 5 个单位
public FlowLayout(int align) 构造一个 FlowLayout,并指定对齐方式
public FlowLayout(int align,int hgap,intvgap) 构造 指定对齐方式、水平、垂直间距

public class T05FlowLayout extends JFrame {
    
	public T05FlowLayout(){
    
		super.setBounds(10, 10, 800, 600);
		this.setLayout(new FlowLayout(FlowLayout.LEFT,3,4));
		for(int i=0;i<20;i++){
    
			this.add(new JButton("按钮_"+(i+1)));
		}
		this.setVisible(true);
	}
	public static void main(String[] args) {
    
		new T05FlowLayout();
	}
	
}
2.边框布局BorderLayout

将一个窗体划分成东、西、南、北、中五个区域

常用常量

public static final StringEAST将组件设置在东区域
public static final StringWEST将组件设置在西区域
public static final StringSOUTH将组件设置在南区域
public static final StringNORTH将组件设置在北区域
public static final StringCENTER将组件设置在中区域

构造方法

public BorderLayout() 构造没有间距的布局器
public BorderLayout(int hgap,int vgap)构造有水平和垂直间距的布局器

public class T06BorderLayout extends JFrame{
    
	public T06BorderLayout(){
    
		super.setBounds(10, 10, 800, 600);
		this.setLayout(new BorderLayout());
		this.add(new JButton("东"),BorderLayout.EAST);
		this.add(new JButton("西"),BorderLayout.WEST);
		this.add(new JButton("南"),BorderLayout.SOUTH);
		this.add(new JButton("北"),BorderLayout.NORTH);
		this.add(new JButton("中"),BorderLayout.CENTER);
		JPanel jp=new JPanel();//面板也是一个容器
		jp.setLayout(new FlowLayout(FlowLayout.LEFT,3,3));
		jp.setBackground(Color.gray);
		for(int i=0;i<6;i++){
    
			jp.add(new JButton("按钮_"+i));
		getContentPane().add(jp,BorderLayout.CENTER);//将面板容器加入外部大容器中
		}
		jp.setVisible(true);
		this.setVisible(true);
	}
	public static void main(String[] args) {
    
		// TODO Auto-generated method stub
		new T06BorderLayout();
	}
}
3.网格布局GridLayout

以表格的形式进行管理,必须设置显示的列数和行数

构造方法

public GridLayout(int rows,int cols)构造一个指定行和列的布局管理器
public GridLayout(int rows,int cols,int hgap,int vgap)构造时指定行和列、水平和垂直间距

public class T07GridLayout extends JFrame {
    
	public T07GridLayout() {
    
		super.setBounds(200, 200, 400, 300);
		this.setLayout(new GridLayout(3, 4));
		this.setBackground(Color.gray);
		for (int i = 0; i < 12; i++) {
    
			this.add(new JButton("按钮_" + i));
		}
		this.pack();
		this.setVisible(true);

	}
	public static void main(String[] args) {
    
		new T07GridLayout();
	}
}
4.卡片布局CardLayout

将一组组件笔记重叠进行布局,像一张张卡片,每次只会展现一个姐界面

构造方法

public CardLayout() 构造CardLayout 对象,各组件间距为 0
public CardLayout(int hgap,int vgap) 构造 CardLayout 对象,指定组件间距

常用方法

public void next(Container parent) 翻转到下一张卡片
public void previous(Container parent) 翻转到上一张卡片
public void first(Container parent) 翻转到第一张卡片
public void last(Container parent) 翻转到最后一张卡片
public void show(Container parent,String name) 显示具有指定组件名称的卡片

public class T08CardLayout extends JFrame{
    
	public T08CardLayout(){
    
		super.setBounds(100,100,800,600);
		this.setBackground(Color.gray);
		this.setTitle("卡片布局");
		CardLayout card=new CardLayout();
		this.setLayout(card);
		this.getContentPane().add(new JLabel("标签1",JLabel.CENTER),"first");
		this.getContentPane().add(new JLabel("标签2",JLabel.CENTER),"second");
		this.getContentPane().add(new JLabel("标签3",JLabel.CENTER),"third");
		this.getContentPane().add(new JLabel("标签4",JLabel.CENTER),"fourth");
		this.getContentPane().add(new JLabel("标签5",JLabel.CENTER),"fifth");
		this.pack();
		this.setVisible(true);
		card.show(super.getContentPane(), "second");
	}
	public static void main(String[] args) {
    
		new T08CardLayout();

	}
}
5.绝对定位布局

通过设置绝对坐标的方式完成,Component中提供了setBounds()方法

public class T09AbsoluteLayout {
    

	public static void main(String[] args) {
    
		JFrame jf=new JFrame("绝对布局");
		jf.setLayout(null);
		jf.setBounds(100, 100, 800, 600);
		jf.setBackground(Color.gray);
		jf.setVisible(true);
		
		JLabel jl=new JLabel("标签",JLabel.RIGHT);
		jl.setBounds(200, 200, 200, 200);
		jl.setBackground(Color.RED);
		jf.getContentPane().add(jl);
		
	}
}

6.事件

1.按钮事件ActionListener

处理按钮的动作事件

组件通过addActionListener(new addActionListener(){

重写ActionListener接口中的actionPerformed方法

})

常用方法

1.窗体事件WindowListener

WindowListener是专门处理窗体的事件监听接口,一个窗体的所有变化,例如:窗口打开,关闭等

添加窗口事件需要实现WindowListener,ActionListener接口

常用方法

void windowActivated(WindowEvent e) 将窗口变为活动窗口时触发
void windowDeactivated(WindowEvent e) 将窗口变为不活动窗口时触发
void windowClosed(WindowEvent e) 当窗口被关闭时触发
void windowClosing(WindowEvent e) 当窗口正在关闭时触发
void windowIconified(WindowEvent e) 窗口最小化时触发
void windowDeiconified(WindowEvent e) 窗口从最小化恢复到正常状态时触发
void windowOpened(WindowEvent e) 窗口打开时触发

public class T07WindowListener extends JFrame implements WindowListener, ActionListener {
    
	public T07WindowListener(){
    

		this.setBounds(200, 200, 800, 600);
		this.setBackground(Color.gray);
		
		JTextArea jt=new JTextArea("sssssss",3,4);
		this.add(jt);
		
		JLabel jl=new JLabel("标签");
		this.add(jl);
		
		JButton jb=new JButton("按钮");
		this.add(jb);
		
		this.addWindowListener(this);
		jb.addActionListener(this);
		this.setVisible(true);
	}
	public static void main(String[] args) {
    
		new T07WindowListener();
	}
	public void actionPerformed(ActionEvent arg0) {
    
		// TODO Auto-generated method stub
		
	}
	public void windowActivated(WindowEvent arg0) {
    
		System.out.println("窗口变为活动窗口时触发");
		
	}
	public void windowClosed(WindowEvent arg0) {
    
		System.out.println("窗口关闭时触发");
		
	}
	public void windowClosing(WindowEvent arg0) {
    
		System.out.println("窗口正在关闭时触发");
		
	}
	public void windowDeactivated(WindowEvent arg0) {
    
		System.out.println("窗口变为不活动窗口时触发");
		
	}
	public void windowDeiconified(WindowEvent arg0) {
    
		System.out.println("窗口最小化到正常触发");
		
	}
	public void windowIconified(WindowEvent arg0) {
    
		System.out.println("窗口最小化触发");
		
	}
	public void windowOpened(WindowEvent arg0) {
    
		System.out.println("窗口打开时触发");
		
	}
}

2.适配器

WindowListener接口中所有抽象方法必须实现,为了提高效率,使用适配器类实现

通过继承WindowAdapter适配器类,可根据需要覆盖重写方法

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/qq_40298829/article/details/109347804

智能推荐

算法--差分_差分csdn_Tancy.的博客-程序员秘密

差分算法的概念及其相应练习题

Android登录界面的注册功能实现_android登录注册功能实现_DY.memory的博客-程序员秘密

注册一个登录界面在控制台将输入的信息文本选框展示出来xml界面设计(前面已发)&lt;?xml version="1.0" encoding="utf-8"?&gt;&lt;LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.and

canvas画球_Moses.2018的博客-程序员秘密

原文地址:https://www.w3cplus.com/canvas/create-3d-ball-with-canvas.html

基于不同影像时序的小麦信息提取----易康面向对象_菠萝味的凤梨s's's的博客-程序员秘密

小麦作为常规种植作物之一,一直是北方的重要农作物。本文中基于16米分辨率不同时相的GF-1影像进行提取小麦信息。1. 数据与软件介绍本文中采用的数据共分为三期:(1)2021年3月7日:该数据作为小麦提取的基准数据,研究区种植为冬小麦,该时节内小麦已经呈现比较明显的植被特征;(2)2021年5月22日:该数据作为中间数据,主要为了去除部分可能会出现的季节性蔬菜对小麦的提取的影响;(3)2021年6月14日:在该时间内小麦已基本完成收割,因此是作为对比去除常绿植被,提取小麦信息。本文中使用软件为

Linux用户及权限基础 4---- Linux下修改密码_从此醉的博客-程序员秘密

1 对于刚使用Linux的同学来说,最痛苦的一件事莫过于要使用root用户来执行某些命令的时候,却不知道root的密码2 我就就介绍一下怎么更改普通用户和root用户的密码1 更改root的密码 1 首先要跟大家申明的一点就是Linux系统的root用户默认是没有密码的 2 我们可以去查看/etc/shadow 下的root用户对应的密码为!,那就是默认没有密码 3 我...

php 导出word文件_小阿巳的博客-程序员秘密

ps:前段时间项目有这个需求,在网上看到的做法 这里整理记录下准备:composer 安装 PhpOffice/PhpWord一份word 模板(提前写好变量占位)原理:其实就是加载模板 替换变量 输出/保存例图:代码:use PhpOffice\PhpWord\TemplateProcessor;public function dayin(){ $id = $this-&gt;request-&gt;param('id', 0, 'intval');

随便推点

php处理url参数_weixin_33775582的博客-程序员秘密

为什么80%的码农都做不了架构师?&gt;&gt;&gt; ...

OpenGL安装(适用于VS2017)_vs c++ opengl_weiyang_tang的博客-程序员秘密

OpenGL安装(适用于VS2017)这个是参考网上的博客,感谢前人的贡献,文末加上了一些我个人踩的坑参考博客:在VS2017中安装OpenGL - 我有点帅哦 - 博客园利用VS2017下的包管理软件Nuget下载安装OpenGL库优点:安装很简单缺点:每次创建项目都要重新下载OpenGL库VS2017创建C++控制台程序,最好选择空项目(否则可能会有其他的错误)创...

视频行为识别检测综述_online action detection综述_梅津太郎的博客-程序员秘密

Video Analysis之Action Recognition(行为识别)行为识别就是对时域预先分割好的序列判定其所属行为动作的类型,即“读懂行为”。 1本文github地址博文末尾支持二维码赞赏哦 _[行为检测|论文解读]行为检测...

myclipse的git撤销commit(回滚到某一版本)_醋酸菌HaC的博客-程序员秘密

使用git 把代码提交到了远程仓库的,我现在的分支是hyw,假如我提交错了代码,或者我不想要这些提交的代码了,需要撤销commit。但是已经commit的代码已经在该分支,所以当你pull该分支的代码时,仍然会pull到上次提交的代码,尽管你已经删除了历史分支,所以想要删除commit的代码,先要pull下来,然后删除,再push上去,否则会有冲突。回滚: 有时候commit提交了错误的或者...

当面试官问你“你希望与什么样的上级共事?”如何回答?_qq_40121517的博客-程序员秘密

招聘者目的是:通过应聘者对上级的“希望”可以判断出应聘者对自我要求的意识,这既上一个陷阱,又上一次机会。  应聘者应该做到:1、最好回避对上级具体的希望,多谈对自己的要求。2、作为刚步入社会的新人,我应该多要求自己尽快熟悉环境、适应环境,而不应该对环境提出什么要求,只要能发挥我的专长就可以了。

带头结点的单链表实现链表的销毁与按位置删除结点_東木的博客-程序员秘密

前言本文主要介绍一下带头结点的单链表实现链表的销毁与按位置删除结点,两者实际上都是删除,最重要的是对链表的遍历。在本文,我们规定按位置删除结点中的位置 i &gt;= 1,也就是i从1开始。话不多说,开搞。链表的销毁1.示意图2.代码块void destroyList(node*&amp; head){ //定义两个node类型指针 node* p, * pTemp; //p...

推荐文章

热门文章

相关标签