阿猛学习笔记java十四GUI

十九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适配器类,可根据需要覆盖重写方法

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值