一、GUI
1.概念
- 先前所学的只是服务器端的应用程序,客户端的用户接口是由web页面组成的
- 通过GUI来实现界面及其内部内容的创建
2.GUI的语句结构
1)创建Frame
- 此时的按钮是没有任何功能的
import javax.swing.*;//引入特定的包
public class SimpleGui1 {
public static void main(String[]args){
JFrame frame=new JFrame();//创建JFrame。JFrame代表屏幕上的window对象,可以把button、checkbox、text等接口放在Window上
JButton button=new JButton("按钮");//创建button,在button上会显示“按钮”字样
frame.getContentPane().add(button);//把button加到frame的pane上
frame.setSize(100,100);//设置frame的大小
frame.setVisible(true);//最后把button显示出来
}
}
2)将按钮上的字样进行重新填写
public void changeIt(){
button.setText("已经改变");
}
3)为GUI中按钮添加功能
- 需要按钮的任务和按钮被按下的方法(按钮的感应装置)
- 获取用户事件:首先按钮要知道它的作用,其次按钮要在按键事件发生时调用执行功能的方法
- 想要知道按钮的事件,就会监听事件的接口
- 接口监听是介于监听(用户)和事件源(按钮)之间的桥梁
- 实现监听接口让按钮有一个回头调用程序的方式。interface正是声明调用方法的地方
import javax.swing.*;//引入特定的包Frame所在的包
import java.awt.event.*;//引入ActionListener和ActionEvent所在的包
public class SimpleGui1 implements ActionListener {
//实现ActionListener接口。表示Simplegui1是个ActionListener
JButton button;
public static void main(String[]args){
SimpleGui1 gui=new SimpleGui1();
gui.go();
}
public void go(){
JFrame frame=new JFrame();//创建JFrame。JFrame代表屏幕上的window对象,可以把button、checkbox、text等接口放在Window上
button=new JButton("按钮");//创建button,在button上会显示“按钮”字样
button.addActionListener(this);//向按钮注册
frame.getContentPane().add(button);//把button加到frame的pane上
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(100,100);//设置frame的大小
frame.setVisible(true);//最后把button显示出来
}
public void actionPerformed(ActionEvent event) {
// TODO Auto-generated method stub
button.setText("已经改变");
}
}
4)GUI的创建到实现
- (1)创建Window,通常使用Frame,对应1)
JFrame frame=new JFrame();
- (2)加入按钮或者文字字段,对应4)
frame.getContentPane().add(button);
- (3)显示Window,要指定尺寸和执行显示动作
frame.setSize(100,100);
frame.setVisible(true);
- (4)监听GUI事件以便知道用户对接口做了什么事情
- (5)要对事件源注册就调用事件源的注册方法
button.addActionListener(this);
- (6)对实现所有的事件处理方法来实现监听接口
public void actionPerformed(ActionEvent event) {
// TODO Auto-generated method stub
button.setText("已经改变");
}
5)GUI上可以添加的东西(一个button)
<1>放置小组件
- GUI上可以放置很多东西,按钮、窗体之类的都可以,这些都存在于javax.swing这个包里
frame.getContentPane().add(button);
放置按钮
放置窗体
6)在widget上绘制图形
基本语句
-使用graphics对象来绘制图形
graphics.fillOval(70,70,100,100);
创建自己的绘图组件
- 创建JPanel的子类并覆盖掉paintComponent()这个方法
- 所有的绘图程序代码都在paintComponent()中
public void paintComponent(Graphics g){
//覆盖父类原来的方法,由系统来调用
g.setColor(Color.orange);
g.fillRect(20,50,100,100);
<1>Graphics引用知识点
- 在以上public void paintComponent(Graphics g),由于参数被声明为Graphics类型,所以g是个Graphics对象
- 由g参数所引用的对象实际上是个Graphics2D的实例
- 已知g是Graphics类型,为了调用Graphics2D类的方法,必须要经过一定的转换
- 二维图形可以直接画在图形组件上
- .gif,.jpeg文件可以直接放在组件上
Graphics2D g2d=(Graphics2D)g;
-
Graphics引用调用的方法可以有:
drawImage();
drawLine();
drawPolygon();
drawRect();
drawOval();
fillRect();
fillRoundRect();
setColor(); -
Graphics2D引用调用的方法
fill3DRect();
draw3DRect();
rotate();
scale();
shear();
transform();
setRenderingHints();
<2>在黑色背景画上随机色彩的圆圈
public void paintComponent(Graphics g){
g.fillRect(0, 0, this.getWidth(), this.getHeight());//以默认颜色填充
int red=(int)(Math.random()*255);
int green=(int)(Math.random()*255);
int blue=(int)(Math.random()*255);
Color randomColor=new Color(red,green,blue);//传入3个参数来代表RGB值
g.setColor(randomColor);
g.fillOval(70, 70, 100, 100);//填满参数指定的椭圆形区域
}
<3>在widget上绘制JPEG图
graphics.drawImage(myPic,10,10,this)
public void paintComponent(Graphics g){
//显示JPEG
Image image=new ImageIcon("catzilla.jpg").getImage();
g.drawImage(image, 3, 4, this);
<4>渐层颜色(颜色特指)
public void paintComponent(Graphics g){
//实际上是一个Graphics2D对象
Graphics2D g2d=(Graphics2D)g;
GradientPaint gradient=new GradientPaint(70,70,Color.blue,150,150,Color.orange);//70,70是起点;Color.blue是开始的颜色;150,150是终点;Color.orange是渐层最后的颜色
g2d.setPaint(gradient);//将虚拟的笔刷换成渐层
g2d.fillOval(70, 70, 100, 100);//用目前的笔刷设定来填满椭圆形区域
}
<5>渐层颜色(颜色随机)
public void paintComponent(Graphics g){
//实际上是一个Graphics2D对象
Graphics2D g2d=(Graphics2D)g;
int red=(int)(Math.random()*255);
int green=(int)(Math.random()*255);
int blue=(int)(Math.random()*255);
Color startColor=new Color(red,green,blue);
red=(int)(Math.random()*255);
green=(int)(Math.random()*255);
blue=(int)(Math.random()*255);
Color endColor=new Color(red,green,blue);
GradientPaint gradient=new GradientPaint(70,70,startColor,150,150,endColor);//70,70是起点;150,150是终点;
g2d.setPaint(gradient);//将虚拟的笔刷换成渐层
g2d.fillOval(70, 70, 100, 100);//用目前的笔刷设定来填满椭圆形区域
}
<6>实现以上绘制图形的方法
import javax.swing.*;//引入特定的包Frame所在的包
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class SimpleGui1 implements ActionListener {
//实现ActionListener接口。表示Simplegui1是个ActionListener
JFrame frame;
public static void main(String[]args){
SimpleGui1 gui=new SimpleGui1()