2020.5.11Java设计1

一、
JFrame的继承结构:
Java.lang.Object
±-java.awt.Component
±-java.awt.Container
±-java.awt.Window
±-java.awt.Frame
±-java.awt.JFrame
使用JFrame容器实例
import javax.swing.;
import java.awt.
;
class FrameTest
Public static void main (String [] args){
Jframe f=new JFrame(“窗体标题”);
f.setDefultCloseOperation(Jframe.EXIT_ON_CLOSE);
f.setVisible(true);
f.setSize(240,90);
}
继承方法:
package com.imau.gui;
/*

  • @author lyp
  • 1.导入包
    *2.创建组件对象
  • 3.初始化
  • /
    import java.awt.
    ;
    import javax.swing.*;

public class FrameDemo extends JFrame{
public FrameDemo(){

	//1.设置标题 图标 大小 可见性  窗口事件(设置窗体:是一个基本容器)
this.setTitle("这是一个窗口");

this.setSize(350,350);
this.setLocation(400,300);
this.setVisible(true);
this.setDefaultCloseOperation(EXIT_ON_CLOSE);


}

}
package com.imau.gui;

public class Text {

public static void main (String[] args){
	new FrameDemo();		
	
}

}
在这里插入图片描述
不用继承方法:
//2.用对象形式创建窗体
public void tset(){
JFrame frame =new JFrame();
frame.setSize(350,350);
frame.setLocation(400,300);
frame.setVisible(true);
frame.setDefaultCloseOperation(EXIT_ON_CLOSE);
}

二、
标签 Lable:是简单的一种组件,一般用来显示标识性的文本信息,常备房子于其他组件的旁边期提示作用
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

import java.awt.;
import javax.swing.
;

public class FrameDemo extends JFrame{

private JLabel label;

private JButton button;
 
private void init(){
	label=new JLabel("姓名");	
	label.setHorizontalAlignment(JLabel.CENTER);
	label.setForeground(Color.ORANGE);
	label.setBackground(Color.blue);
	
	button =new JButton("按钮");
}
public FrameDemo(){
	//调用init
	init();
	this.add(label);
	this.add(button);
	//1.设置标题 图标 大小 可见性  窗口事件(设置窗体:是一个基本容器)
this.setTitle("这是一个窗口");
this.setSize(350,350);
this.setLocation(400,300);
this.setVisible(true);
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
}
//2.用对象形式创建窗体
public void tset(){
	JFrame frame =new JFrame();
	frame.setSize(350,350);
	frame.setLocation(400,300);
	frame.setVisible(true);
	frame.setDefaultCloseOperation(EXIT_ON_CLOSE);	
}	

}

按钮:Button,他是从Component类直接继承而来
//按钮是否可点击
//button.setEnabled(false);

在这里插入图片描述

布局管理器:布局管理器(Layout Manager)是用来安排容器中多个组件的位置及大小,以确保GUI中各组件能安排在适当的位置。
每当需要重新调整屏幕大小或重新绘制屏幕上任一项目时,就要用到布局管理器。
AWT包提供一组用来进行布局管理的类,每个布局管理类对应一种布局策略。
Java中的布局类型包括以下几种:
FlowL ayout (流式布局)
BorderLayout(边界布局)
GridLayout (网格布局)
CardLayout(卡片布局)
BoxLayout (框布局)
GridBagL ayout (网格包布局)
每个容器(Container对象) 都有一个与它相关的缺省的布局管理器。
. Applet的缺省布局是FlowLayout,Frame的缺省布局是BorderLayout,Panel的缺省布局是FlowLayout。
1、FlowLayout:流式布局必Applet和面板的缺省布局心组件从左上角开始按从左到右、从上到下的方式排列。
FlowLayout的构造函数有:
FlowLayout( ):生成一个默认的流式布局(空)
FlowLayout(int alignment):可以设定每一行组件的对齐方式
FlowLayout(int alignment,int horz,int vert):可以设定组件间的水平和垂直距离
//2.设置布局
默认:setLayout(new FlowLayout());
居中对齐
左对齐:setLayout(new FlowLayout(FlowLayout.LEFT));

在这里插入图片描述
在这里插入图片描述
import java.awt.;
import javax.swing.
;

public class FrameDemo extends JFrame{

private JLabel label;

private JButton button;
private JButton bts[]=new JButton[10];
 
private void init(){
	label=new JLabel("姓名");	
	label.setHorizontalAlignment(JLabel.CENTER);
	label.setForeground(Color.ORANGE);
	label.setBackground(Color.blue);
	
	button =new JButton("按钮");
	//按钮是否可点击
	//button.setEnabled(false);
	
	for(int i=0;i<bts.length;++i){
		bts[i]=new JButton ("按钮"+i);
	}
}

public FrameDemo(){

	//调用init
	init();
	this.add(label);
	this.add(button);
	for(int i=0;i<bts.length;++i){
		this.add(bts[i]);

	}

	//2.设置布局 
	setLayout(new FlowLayout(FlowLayout.LEFT));或

setLayout(new FlowLayout(FlowLayout.LEFT,30,30));

	//1.设置标题 图标 大小 可见性  窗口事件(设置窗体:是一个基本容器)
this.setTitle("这是一个窗口");
this.setSize(350,350);
this.setLocation(400,300);
this.setVisible(true);
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
}
//2.用对象形式创建窗体
public void tset(){
	JFrame frame =new JFrame();
	frame.setSize(350,350);
	frame.setLocation(400,300);
	frame.setVisible(true);
	frame.setDefaultCloseOperation(EXIT_ON_CLOSE);

}}
在这里插入图片描述
在这里插入图片描述

2、BorderLayout边界布局: 给出位置
在这里插入图片描述
public FrameDemo(){
//调用init
init();
setLayot(new BorderLayout());
add(bts[0],BorderLayout.EAST);
add(bts[1],BorderLayout.WEST);
add(bts[2],BorderLayout.NORTH);
add(bts[3],BorderLayout.SOUTH);
add(bts[4],BorderLayout.CENTER);
}

	//2.设置布局 
	setLayout(new BorderLayout());

package com.imau.gui;
/*

  • @author lyp
  • 1.导入包
    *2.创建组件对象
  • 3.初始化
  • /
    import java.awt.
    ;
    import javax.swing.*;

public class FrameDemo extends JFrame{

private JLabel label;

private JButton button;
private JButton bts[]=new JButton[10];
 
private void init(){
	label=new JLabel("姓名");	
	label.setHorizontalAlignment(JLabel.CENTER);
	label.setForeground(Color.ORANGE);
	label.setBackground(Color.blue);
	
	button =new JButton("按钮");
	//按钮是否可点击
	//button.setEnabled(false);
	
	for(int i=0;i<bts.length;++i){
		bts[i]=new JButton ("按钮"+i);
		
	}
}
public FrameDemo(){

	//调用init
	init();

//2.设置布局

			setLayout(new BorderLayout());
			
			add(bts[0],BorderLayout.EAST);
			add(bts[1],BorderLayout.WEST);
			add(bts[2],BorderLayout.NORTH);
			add(bts[3],BorderLayout.SOUTH);
			add(bts[4],BorderLayout.CENTER);
			//1.设置标题 图标 大小 可见性  窗口事件(设置窗体:是一个基本容器)
			this.setTitle("这是一个窗口");
			this.setSize(350,350);
			this.setLocation(400,300);
			this.setVisible(true);
			this.setDefaultCloseOperation(EXIT_ON_CLOSE);

	
	}
//2.用对象形式创建窗体
public void tset(){
	JFrame frame =new JFrame();
	frame.setSize(350,350);
	frame.setLocation(400,300);
	frame.setVisible(true);
	frame.setDefaultCloseOperation(EXIT_ON_CLOSE);	
}	

}

3、GridLayout布局相当于计算器
在这里插入图片描述
public class FrameDemo extends JFrame{

private JLabel label;

private JButton button;
private JButton bts[]=new JButton[16];
 
private void init(){
	label=new JLabel("姓名");	
	label.setHorizontalAlignment(JLabel.CENTER);
	label.setForeground(Color.ORANGE);
	label.setBackground(Color.blue);
	
	button =new JButton("按钮");
	//按钮是否可点击
	//button.setEnabled(false);
	
	for(int i=0;i<bts.length;++i){
		bts[i]=new JButton ("按钮"+i);
		
	}
}

setLayout(new GridLayout(4,5));
public FrameDemo(){
//调用init
init();
//2.设置布局
setLayout(new GridLayout(4,4));
for(int i=0;i<bts.length;++i){
this.add(bts[i]);
}
//1.设置标题 图标 大小 可见性 窗口事件(设置窗体:是一个基本容器)
this.setTitle(“这是一个窗口”);
this.setSize(350,350);
this.setLocation(400,300);
this.setVisible(true);
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
}
//2.用对象形式创建窗体
public void tset(){
JFrame frame =new JFrame();
frame.setSize(350,350);
frame.setLocation(400,300);
frame.setVisible(true);
frame.setDefaultCloseOperation(EXIT_ON_CLOSE);
} }
4、空布局null:添加组件的时候,没有有管理则组件没有大小,没有位置,创建时首先要给出大小和位置,绝对定位
在这里插入图片描述
public class FrameDemo extends JFrame{

private JLabel label;

private JButton button;
private JButton bts[]=new JButton[16];
 
private void init(){
	label=new JLabel("姓名");

label.setBounds(10,10,40,40); (x,y,width,heigh)大小位置

	label.setSize(40,40);
	label.setLocation(10,10);
	button =new JButton("按钮");
	button.setSize(40,40);
	button.setLocation(60,10);
	for(int i=0;i<bts.length;++i){
		bts[i]=new JButton ("按钮"+i);
		
	}
}

public FrameDemo(){
	//调用init
	init();
	//2.设置布局 
	//空布局:添加组件的时候,组件没有大小,没有位置;创建时首先要给出大小和位置
	setLayout(null);
	this.add(label);
	this.add(button);
	for(int i=0;i<bts.length;++i){
		this.add(bts[i]);
		
	}	
	//1.设置标题 图标 大小 可见性  窗口事件(设置窗体:是一个基本容器)
	this.setTitle("这是一个窗口");
	this.setSize(350,350);
	this.setLocation(400,300);
	this.setVisible(true);
	this.setDefaultCloseOperation(EXIT_ON_CLOSE);
	}

//2.用对象形式创建窗体
public void tset(){
	JFrame frame =new JFrame();
	frame.setSize(350,350);
	frame.setLocation(400,300);
	frame.setVisible(true);
	frame.setDefaultCloseOperation(EXIT_ON_CLOSE);
}

}

创建一个QQ登录界面
package com.imau.gui;
import java.awt.;
import javax.swing.
;

public class Login extends JFrame {

private JLabel lname;
private JLabel lpass;
private JTextField tname;
private JTextField tpass;
private JButton login;


private void init (){
	lname=new JLabel("用户名");
	lpass=new JLabel("密   码");
	tname =new JTextField(14);
	tpass= new JTextField(14);
	login =new JButton("登录");
	
	this.setLayout(new FlowLayout( FlowLayout.CENTER));
	this.add(lname);
	this.add(tname);
	this.add(lpass);
	this.add(tpass);
	this.add(login);
	//1.获取屏幕大小
	//2.设置为大小
	Dimension dim =getToolkit().getScreenSize();
	//dim.height/2;
	//dim.width/2;
	
	
	this.setResizable(false);
	this.setTitle("QQ登录");
	this.setSize(250,125);
	this.setLocation(400,300);
	
	
	this.setVisible(true);
	this.setDefaultCloseOperation(EXIT_ON_CLOSE);

}
public Login(){
	init ();
	

}

}
在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值