![]() |
#2
lgw6466342312015-12-12 00:25
|

// 添加25个按钮到p1
JButton[] pb = new JButton[25];
int bn;
for (bn=0;bn < 25; bn++){
p1.add(pb[bn] = new JButton(new ImageIcon("./src/images/cm.jpg")));// 按钮图片
//监听者
pb[bn].addActionListener(new ActionListener() {
//监听事件
public void actionPerformed(ActionEvent e) {
pb[bn].setIcon(gw);
pb[bn+1].setIcon(gw);
pb[bn+5].setIcon(gw);
}
});}
JButton[] pb = new JButton[25];
int bn;
for (bn=0;bn < 25; bn++){
p1.add(pb[bn] = new JButton(new ImageIcon("./src/images/cm.jpg")));// 按钮图片
//监听者
pb[bn].addActionListener(new ActionListener() {
//监听事件
public void actionPerformed(ActionEvent e) {
pb[bn].setIcon(gw);
pb[bn+1].setIcon(gw);
pb[bn+5].setIcon(gw);
}
});}


我这个样子写,提示void 对于actionPerfomed 无效,

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class luoguowen_game extends Frame implements ActionListener {
/**
*
*/
private static final long serialVersionUID = -1383975989841903439L;
public luoguowen_game() {
// 创建
JFrame f = new JFrame("国哥的第一个游戏");// 标题
JPanel p1 = new JPanel();// 创建1容器
JPanel p2 = new JPanel();// 创建2容器
JSplitPane sp = new JSplitPane();// 创建容器分割
ImageIcon ii = new ImageIcon("./src/images/tubiao.png");// 添加JFrame图标
ImageIcon gw = new ImageIcon("./src/images/gw.jpg");// 添加替换图片
Image tubiao = ii.getImage();// 创建JFrame图标对象
Image image2 = gw.getImage();// 创建替换图片对象
// 主框体属性
f.add(p1);// 主框添加容器
f.setSize(610, 870);// 主框体尺寸
f.setResizable(false);// 不可改变框体大小
f.setLocationRelativeTo(null);// 主框出现在屏幕中央
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);// 点叉完全退出
f.setIconImage(tubiao);// 设置JFrame图标
JButton start = new JButton("开始");// 创建开始按钮
p2.add(start);// 向p2添加开始按钮
start.setPreferredSize(new java.awt.Dimension(100, 110));// 开始按钮设置为宽100,高110
start.setFont(new Font("宋体", Font.LAYOUT_NO_LIMIT_CONTEXT, 33));// 开始字体设置为宋体、无布局限制、33号
JButton restart = new JButton("重置");// 创建重置按钮
p2.add(restart);// 向p2添加重置按钮
restart.setPreferredSize(new java.awt.Dimension(100, 110));// 重置按钮设置为宽100,高110
restart.setFont(new Font("宋体", Font.LAYOUT_NO_LIMIT_CONTEXT, 33));// 重置字体设置为宋体、无布局限制、33号
// 布局
p1.setLayout(new GridLayout(5, 5));// 为p1容器设置5*5网格布局
p2.setLayout(new FlowLayout());// 为p2容器设置流布局
sp.setOrientation(JSplitPane.VERTICAL_SPLIT);// 分割容器为上下两部分
sp.setBottomComponent(p2);// 设置p2在下
sp.setTopComponent(p1);// 设置p1在上
sp.setDividerSize(3); // 分割线大小设置3
sp.setDividerLocation(710);// 分割线位置710
f.setContentPane(sp);// 替换主框容器为分割容器
// 按钮
// String
// b[]={"1","2","3","4","5","6","7","8","9","10","11","12","13","14","15","16","17","18",
// "19","20","21","22","23","24","25"};
// 添加25个按钮到p1
int bn;
JButton[] pb = new JButton[25];
for (bn = 0; bn < 25; bn++)
p1.add(pb[bn] = new JButton(new ImageIcon("./src/images/cm.jpg")));// 按钮图片
f.setVisible(true);// 主框体可见
// 监听者
pb[bn].addActionListener(this);
// 监听事件
public void actionPerformed(ActionEvent e) {
pb[bn].setIcon(gw);
pb[bn+1].setIcon(gw);
pb[bn+5].setIcon(gw);
}
}
public static void main(String[] args) {
new luoguowen_game();
}
}
import java.awt.*;
import java.awt.event.*;
public class luoguowen_game extends Frame implements ActionListener {
/**
*
*/
private static final long serialVersionUID = -1383975989841903439L;
public luoguowen_game() {
// 创建
JFrame f = new JFrame("国哥的第一个游戏");// 标题
JPanel p1 = new JPanel();// 创建1容器
JPanel p2 = new JPanel();// 创建2容器
JSplitPane sp = new JSplitPane();// 创建容器分割
ImageIcon ii = new ImageIcon("./src/images/tubiao.png");// 添加JFrame图标
ImageIcon gw = new ImageIcon("./src/images/gw.jpg");// 添加替换图片
Image tubiao = ii.getImage();// 创建JFrame图标对象
Image image2 = gw.getImage();// 创建替换图片对象
// 主框体属性
f.add(p1);// 主框添加容器
f.setSize(610, 870);// 主框体尺寸
f.setResizable(false);// 不可改变框体大小
f.setLocationRelativeTo(null);// 主框出现在屏幕中央
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);// 点叉完全退出
f.setIconImage(tubiao);// 设置JFrame图标
JButton start = new JButton("开始");// 创建开始按钮
p2.add(start);// 向p2添加开始按钮
start.setPreferredSize(new java.awt.Dimension(100, 110));// 开始按钮设置为宽100,高110
start.setFont(new Font("宋体", Font.LAYOUT_NO_LIMIT_CONTEXT, 33));// 开始字体设置为宋体、无布局限制、33号
JButton restart = new JButton("重置");// 创建重置按钮
p2.add(restart);// 向p2添加重置按钮
restart.setPreferredSize(new java.awt.Dimension(100, 110));// 重置按钮设置为宽100,高110
restart.setFont(new Font("宋体", Font.LAYOUT_NO_LIMIT_CONTEXT, 33));// 重置字体设置为宋体、无布局限制、33号
// 布局
p1.setLayout(new GridLayout(5, 5));// 为p1容器设置5*5网格布局
p2.setLayout(new FlowLayout());// 为p2容器设置流布局
sp.setOrientation(JSplitPane.VERTICAL_SPLIT);// 分割容器为上下两部分
sp.setBottomComponent(p2);// 设置p2在下
sp.setTopComponent(p1);// 设置p1在上
sp.setDividerSize(3); // 分割线大小设置3
sp.setDividerLocation(710);// 分割线位置710
f.setContentPane(sp);// 替换主框容器为分割容器
// 按钮
// String
// b[]={"1","2","3","4","5","6","7","8","9","10","11","12","13","14","15","16","17","18",
// "19","20","21","22","23","24","25"};
// 添加25个按钮到p1
int bn;
JButton[] pb = new JButton[25];
for (bn = 0; bn < 25; bn++)
p1.add(pb[bn] = new JButton(new ImageIcon("./src/images/cm.jpg")));// 按钮图片
f.setVisible(true);// 主框体可见
// 监听者
pb[bn].addActionListener(this);
// 监听事件
public void actionPerformed(ActionEvent e) {
pb[bn].setIcon(gw);
pb[bn+1].setIcon(gw);
pb[bn+5].setIcon(gw);
}
}
public static void main(String[] args) {
new luoguowen_game();
}
}
[此贴子已经被作者于2015-12-12 09:44编辑过]