package day0619;
import javax.swing.*;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.Collections;
public class java6 {
public static void main(String[] args) {
//1.定义一个变量w指代一个新建的窗口(用JFrame文件)
JFrame w=new JFrame();
//3用JPanel文件夹架一个面板对象,存入到变量JPanel中(用来承载界面上的所有元素)
JPanel p=new JPanel();
//9.Layout布局//将默认的布局方式设置为空(没有布局方式(通过xy坐标给每个元素设置位置))
p.setLayout(null);
//4.用ImageIcon文件创建一个ImageIcon对象存入bg变量中,用来操作“images/背景.jpg”
ImageIcon bg=new ImageIcon("images/背景.jpg");
//5.用JLabel文件创建一个对象存入j变量中(表示标签,用来包含背景图)
JLabel j=new JLabel();
//10.给包含背景图的标签设置宽高和坐标
j.setSize(480,800);
j.setLocation(0,0);
//6.将变量指代的背景图设置到标签j中(给标签j设置图片bg//让标签包含图片bg)
j.setIcon(bg);
//7.将j变量指代的包含背景图的标签设置到面板p中(给面板p中添加标签j)
p.add(j);
//11.创建所有的按钮的代码
//11(1)创建一个包含所有图片名字的String【】 names数组
ArrayList<String> names2=new ArrayList<>();
String[] names={"刷子","剪刀","叉子","奶瓶","干草","手套","树桩",
"棉花","毛线","水桶","火","玉米","白菜","草","萝卜","铃铛"};
for (int i=0;i<names.length;i++){//将数组元素存入到集合中
names2.add(names[i]);
}
//11.(2)创建一个JButton类型的数组is,容量为96,用来存储下边双层for信息创建的96个按钮
ArrayList<JButton> bottom=new ArrayList<>();
ArrayList<JButton> is=new ArrayList<>();
//外循环执行0到5次(6次),表示names.length种按钮创建6遍(总共6*names.length)
for (int i=0;i<6;i++){//6*16=96
//内循环执行names次,表示names.length(16)种按钮
for (int b=0;b<names.length;b++){
//创建一个ImageIcon b1Icon变量中
//用来操作("images/"+names[b]+".png"图片)
ImageIcon b1Name=new ImageIcon("images/"+names[b]+".png");
//创建一个JButton对象存入到JButtonb1变量中(表示本次循环创建一个一个按钮)
JButton b1=new JButton();
//给按钮设置宽59,高66
b1.setSize(59,66);
//给按钮b1设置图片b1Icon
b1.setIcon(b1Name);
b1.setName(names[b]);
//将按钮b1存入到is数组的i*16+b位置
//is【i*16+b】=b1;
is.add(b1);
}
}
Collections.shuffle(is);//13打乱96个按钮
//12第一层按钮
for (int i=0;i<42;i++){//此for循环从0开始到41结束(执行42次),从is数组中取出42个按钮
JButton b=(JButton) is.get(i);//is【i】//从is数组中取出一个按钮is【i】存入变量JButton b(放在界面上)
int x=30+i%7*59;//计算此按钮的x坐标//59是按钮高度
int y=100+i/7*66;//计算此按钮的y坐标//66是按钮高度
b.setLocation(x,y);//将xy坐标设置给按钮b
p.add(b,0);//将按钮b添加到p的第0层
}
for (int i=42;i<72;i++){//此for循环从42开始到71结束(执行30次)从is数组中取出30个按钮
JButton b=(JButton) is.get(i);//is【i】//从is数组中取出一个按钮is【i】存入变量JButton b(放在界面上)
int x=60+(i-42)%6*59;//计算此按钮的x坐标//59是按钮高度
int y=133+(i-42)/6*66;//计算此按钮的y坐标//66是按钮高度
b.setLocation(x,y);//将xy坐标设置给按钮b
p.add(b,0);//将按钮b添加到p的第0层
}
for (int i=72;i<96;i++){//12此for循环从42开始到71结束(执行30次)从is数组中取出30个按钮
JButton b=(JButton) is.get(i);//is【i】//从is数组中取出一个按钮is【i】存入变量JButton b(放在界面上)
int x=55+(i-72)%6*59;//计算此按钮的x坐标//59是按钮高度
int y=166+(i-72)/6*66;//算此按钮的y坐标//66是按钮高度
b.setLocation(x,y);//将xy坐标设置给按钮b
p.add(b,0);//将按钮b添加到p的第0层
}
for (int i=0;i<is.size()-2;i++){//
for (int n=i+1;n<is.size();n++){//
int x1=((JButton)is.get(i)).getX()-((JButton)is.get(n)).getWidth();//
int x2=((JButton) is.get(i)).getX()+((JButton) is.get(n)).getWidth();//
int y1=((JButton) is.get(i)).getY()-((JButton) is.get(n)).getWidth();//
int y2=((JButton) is.get(i)).getY()+((JButton) is.get(n)).getWidth();//
if (x1<((JButton)is.get(n)).getX()&& ((JButton)is.get(n)).getX()<x2&&y1<((JButton)is.get(n)).getY()&&((JButton)is.get(n)).getY()<y2){
((JButton) is.get(i)).setEnabled(false);//
}//
}//
}//
for (int i=0;i<is.size();i++){//
JButton card= is.get(i);
int i2=i;//
card.addActionListener(
//第74到77行(e可以用来获取点击的按钮一些信息)
e->{//
JButton selected=(JButton) e.getSource();//用e来获取点击按钮 Source源头
// System.out.println("点击:"+selected.getName());//打印按钮的名字
// bottom 集合用来存储7个按钮(最多七个)
//bottom如果存满七个按钮就结束游戏(用return结束当前点击按钮的处理效果(还可以关闭窗口))
if (bottom.size()==7){
//消息Message Dialog对话框(在一个消息框中显示文字)
JOptionPane.showMessageDialog(selected,"槽子满了");
return;
}
//从is集合中的96个按钮中删除当前单击的按钮
is.remove(selected);
//给当前点击的按钮selected修改xy坐标(坐标设置到七个按钮的槽子位置)
selected.setLocation(25+bottom.size()*selected.getWidth(),640);
//更新界面
//用select按钮获取当前点击按钮的所有接听器,再用【0】取出第0个接听器(因为只给一个设置接听器)
//接听器用ActionLUisteener实现的
ActionListener l= selected.getActionListeners()[0];//获取当前按钮所有的点击效果的第0个
selected.removeActionListener(l);//从当前点击的按钮select中删除单击效果l
bottom.add(selected);//将当前点击按钮添加到只能添加七个按钮的集合中bottom
JButton b1=null;
JButton b2=null;
for (int r=0;r<bottom.size()-1;r++){//可以存储七个按钮的bottom集合取出每个按键和当前按钮selected
JButton other=bottom.get(r);//比较按钮名字
if (other.getName().equals(selected.getName())){//如果按钮名字相同则将bottom中相同的序号存入
if (b1==null){
b1=other;
}else {
b2=other;
}
}
}
//如果b1变量中存储了按钮,b2变量中也存储了按钮(select变量肯定存储了当前点击的按钮)
//则此时可以删除折三个按钮
if (b1!=null&&b2!=null){
bottom.remove(b1);//从可以存储七个按钮的bottom中删除三个按钮
bottom.remove(b2);
bottom.remove(selected);
p.remove(b1);//从界面中的面板p中删除三个按钮
p.remove(b2);
p.remove(selected);
p.repaint();//更新面板p的内荣
}
//从集合的长度size() 数组的长度length 字符串的长度为length()
for (int m=0;m<bottom.size();m++){
//从bottom中取出一个按钮b
JButton b=bottom.get(m);
//给此按钮b设置一个新的xy坐标
//(y坐标相同表示他们处于一个水平线,x坐标是:按钮序号*按钮宽度6+25)
b.setLocation(25+m*selected.getWidth(),640);
}
}
);
}
//8.将面板p、添加到窗口w中(窗口包含面板)
w.add(p);
//2设置窗口属性
//2(1)默认值Default 关闭Close 操作 Operation
w.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//2(2)设置窗口w的尺寸(宽高)
w.setSize(480,800);
//2(3)设置w代表的窗口显示(出来),true表示显示//Visible显示
w.setVisible(true);
}
}
羊了羊代码
最新推荐文章于 2024-05-18 09:59:11 发布