import javax.swing.*;
import java.awt.*;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
public class MainFrame extends JFrame {
private BackgroundPanel jContentPanel = null;
private Ball ball = null;
private static JPanel infoPanel;//提示面板
public static void main(String[] args) {
new MainFrame().setVisible(true);
}
public MainFrame() throws HeadlessException{//在不支持键鼠设备抛出的异常
super();
this.setTitle("消除小球游戏");
this.setSize(512,384);
this.setLocationRelativeTo(null);
this.setResizable(false);//窗口不可最大化
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setContentPane(getJContentPanel());
/**提示信息*/
infoPanel = (JPanel) this.getGlassPane();
JLabel label = new JLabel("准备开始");
label.setHorizontalAlignment(SwingConstants.CENTER);//标签出现位置
label.setFont(new Font("华文行楷",Font.BOLD,35));
label.setForeground(Color.blue);
infoPanel.setLayout(new BorderLayout());
infoPanel.add(label);
}
private BackgroundPanel getJContentPanel(){
if(jContentPanel == null){
jContentPanel = new BackgroundPanel();
jContentPanel.setLayout(null);
addBall();
}
return jContentPanel;
}
public void addBall(){
for(int i = 0;i<5;i++){
ball = new Ball();
ball.setBounds(new Rectangle(121,67,40,40));
jContentPanel.add(ball,null);
ball.addMouseListener(new MouseAdapter() {
public void mousePressed(MouseEvent e){
super.mousePressed(e);
if(e.getSource() instanceof JLabel){//鼠标击中小球
JLabel ball = (JLabel) e.getSource();//把事件源赋给小球
jContentPanel.remove(ball);//从父容器中移出小球
jContentPanel.repaint();//重绘面板
}
int count = jContentPanel.getComponentCount();//获取小球数量
if(count<=0){
int option = JOptionPane.showConfirmDialog(null,"再来一局!");//提示框
if(option == JOptionPane.YES_OPTION){//如果选择是
new Thread(new Runnable() {
@Override
public void run() {
infoPanel.setVisible(true);//出现提示信息
try {
Thread.sleep(3000);//等待3s
}catch (Exception e){
e.printStackTrace();
}
infoPanel.setVisible(false);//关闭提示信息
addBall();//重新生成小球
}
}).start();
}
}
}
});
}
}
}

四月天行健
- 粉丝: 4w+
最新资源
- 高级人工智能-5.pptx
- 【河北省】专业技术人员标准化的理论方法与实践试卷4答案.doc
- 工程报验申请表.doc
- 基于STC89C52单片机-红外智能循迹小车(同名9240).doc
- 深度强化学习各算法详解及 Pytorch 实现方法 深度强化学习各类算法介绍与 Pytorch 实现方式 深度强化学习各算法的介绍及 Pytorch 实现详解 深度强化学习不同算法介绍与 Pytorc
- 实验幼儿园儿童园内饮食记录表.doc
- 公司物流服务方案.doc
- 小学英语人称代词表格总结.doc
- 移位指令SFTPPT课件.ppt
- 计量经济学-四元线性回归模型案例分析.doc
- 计算机专业假期社会实践报告范文.docx
- 天天讲安全表格.doc
- 公司信用管理总结.doc
- 民营企业高管绩效考核研究.doc
- 教师网络远程培训心得体会四篇.doc
- 水果拼盘店创业计划书.docx
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈



评论0