今天我们来学习java中的桌面可视化界面的制作
1、GUI组件继承关系
2、GUI组件中常用方法
3、使用GUI编写个爱情测试程序
3.1创建窗口类
package com.yueqian.myswing1;
import java.awt.Color;
import java.awt.Font;
import java.awt.Frame;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
public class MyFrame2 extends JFrame{
//定义标签
private JLabel jLabel;
//定义按钮
private JButton yesBtn,noBtn;
public MyFrame2() {
super("爱情测试");
//设置窗口大小
this.setSize(400,300);
//设置窗口根据父窗口居中
this.setLocationRelativeTo(null);
//设定布局为空,只有这种情况下,组件的大小和位置才能自己设定
//添加组件
addComponent();
//默认布局根据组件的不同,默认布局不同:Jframe默认布局 :BorderLayout边界布局
this.setLayout(null);
//设置窗口可见
this.setVisible(true);
//设置窗口默认关闭操作
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
private void addComponent() {
//构造标签
jLabel = new JLabel("你爱我吗?");
//设置标签位置和大小
jLabel.setBounds(0, 30, 400, 30);
//设置内容横向居中 默认横向居左,纵向居中) 居左 JLabel.LEFT 居右 JLabel.RIGHT 居中 JLabel.CENTER
jLabel.setHorizontalAlignment(JLabel.CENTER);
//设置内容纵向居中
// jLabel.setVerticalAlignment(JLabel.CENTER);
//添加标签到窗口容器
this.add(jLabel);
//设置字体颜色
Font font = new Font("宋体",Font.PLAIN,20);
//设置字体
jLabel.setFont(font);
//设置背景不透明
jLabel.setOpaque(true);
//设置背景颜色
// jLabel.setBackground(Color.yellow);
//设置背景的前景色
// jLabel.set
//添加按钮
yesBtn = new JButton("爱你");
//设置按钮的大小和位置
yesBtn.setBounds(50, 70, 70, 25);
//添加按钮
this.add(yesBtn);
//添加按钮
noBtn = new JButton("不爱");
//设置按钮的大小和位置
noBtn.setBounds(272, 70, 70, 25);
//添加按钮
this.add(noBtn);
//定义监听器对象
YesBtnListener yesLis = new YesBtnListener(this);
//添加按钮监听器
yesBtn.addActionListener(yesLis);
//定义监听器对象
NoBtnListener noLis = new NoBtnListener(noBtn);
//添加按钮监听器
noBtn.addMouseListener(noLis);
}
public static void main(String[] args) {
new MyFrame2();
}
}
3.2、创建爱你点击事件类
package com.yueqian.myswing1;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JOptionPane;
public class YesBtnListener implements ActionListener {
//将MyFrame对象传递进来
private MyFrame2 window;
public YesBtnListener(MyFrame2 window) {
super();
this.window = window;
}
@Override
public void actionPerformed(ActionEvent e) {
//关于window子窗口位置模态
JOptionPane.showMessageDialog(window, "我也爱你!");
}
}
3.3创建不爱点击事件类
package com.yueqian.myswing1;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import javax.swing.JButton;
/**
* 鼠标移动事件
* @author LinChi
*
*/
public class NoBtnListener implements MouseListener {
private JButton noBtn;
public NoBtnListener(JButton noBtn) {
super();
this.noBtn = noBtn;
}
/**
* 监听按钮被单击
*/
@Override
public void mouseClicked(MouseEvent e) {
// TODO Auto-generated method stub
}
/**
* 监听鼠标按钮被按下
*/
@Override
public void mousePressed(MouseEvent e) {
// TODO Auto-generated method stub
}
/**
* 监听鼠标按钮被释放
*/
@Override
public void mouseReleased(MouseEvent e) {
// TODO Auto-generated method stub
}
/**
* 监听鼠标被放下
*/
@Override
public void mouseEntered(MouseEvent e) {
while(true) {
//设置随机横向坐标和纵向坐标
//横向移动坐标 x: 0-310
//纵向移动坐标 y: 95-235
int x = (int)(Math.random()*311);
int y = (int)(Math.random()*(235-95+1))+95;
//判断是否横向抛出按钮的宽度和纵向跑出按钮的宽度
if(Math.abs(x-noBtn.getX())>noBtn.getWidth() || Math.abs(y-noBtn.getY())>noBtn.getHeight()) {
//设定随机位置
noBtn.setBounds(x, y, 70, 25);
break;
}
}
}
@Override
public void mouseExited(MouseEvent e) {
// TODO Auto-generated method stub
}
}
3.4测试结果如图
去测试下你的女朋友呗,放心啦!她只能选择爱你哦,因为不爱按钮会动,无法点击哦!!!