关于java中菜单事件的监听问题,求各位大神! java菜单监听事件 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 import java.awt.*;import java.awt.event.*;import javax.swing.*;public class MenuEvent extends JFrame { private JMenuBar aaa; private JMenu bbb; private JPanel panel; private JButton buttons[][]; int a = 5, b = 5; Container c = getContentPane(); public MenuEvent() { aaa = new JMenuBar(); this.setJMenuBar(aaa); bbb = new JMenu("选项"); aaa.add(bbb); JMenuItem ccc = new JMenuItem("初级"); JMenuItem ddd = new JMenuItem("中级"); JMenuItem eee = new JMenuItem("高级"); ccc.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { a = 10; b = 10; refreshUI(); } }); bbb.add(ccc); bbb.addSeparator(); ddd.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { a = 15; b = 15; refreshUI(); } }); bbb.add(ddd); bbb.addSeparator(); eee.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { a = 20; b = 20; refreshUI(); } }); bbb.add(eee); refreshUI(); setSize(500, 500); setVisible(true); } private void refreshUI() { buttons = new JButton[a][b]; panel = new JPanel(); panel.setLayout(new GridLayout(a, b, 2, 2)); for (int i = 0; i < a; i++) { for (int j = 0; j < b; j++) { buttons[i][j] = new JButton(); panel.add(buttons[i][j]); } } c.removeAll(); c.add(panel, BorderLayout.CENTER); this.validate(); } public static void main(String[] args) { MenuEvent m = new MenuEvent(); }} 能在麻烦你个问题吗、、、我按照各变了扫雷的小程序,可是选到初级,中级,高级,逻辑好像就乱了,只有初始化的好使,但是别的等级就不好使了、import java.awt.*;import java.awt.event.*;import java.util.Scanner;import javax.swing.*;public class saolei extends JFrame { private int row=9;//不设初始值。按下开始按钮,就卡死了 private int col=9;//不设初始值。按下开始按钮,就卡死了 Bomb[][] buttons; //没个按钮初始看做为雷按钮 private JButton start; private JLabel label1; //雷数标签 private int BombNum=8; //设置雷的数目 private JPanel panel1; //标签面板 private JPanel panel2; // 雷面板 int foundBomb, restBomb, restbuttons; // 找到的地雷数,剩余雷数,剩余按钮 private int lei[][]; private Container container = getContentPane(); //构造方法 public saolei() { this.setTitle("扫雷小游戏"); JMenuBar TestJMenuBar=new JMenuBar(); JMenuItem primary,medium,senior; this.setJMenuBar(TestJMenuBar); JMenu begin=new JMenu("开始"); TestJMenuBar.add(begin); primary=new JMenuItem("初级"); primary.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { row=10; col=10; BombNum=10; initButtons(); } }); begin.add(primary); begin.addSeparator(); medium=new JMenuItem("中级"); medium.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { row=15; col=15; BombNum=15; initButtons(); } }); begin.add(medium); begin.addSeparator(); senior=new JMenuItem("高级"); senior.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { row=20; col=20; BombNum=20; initButtons(); } }); begin.add(senior); JMenu choose=new JMenu("选项"); TestJMenuBar.add(choose); choose.addSeparator(); JMenu change = new JMenu("改变颜色"); choose.add(change); change.add(new JMenuItem("红色")); change.add(new JMenuItem("黄色")); change.add(new JMenuItem("紫色")); change.add(new JMenuItem("蓝色")); choose.addSeparator(); JMenu help = new JMenu("帮助"); TestJMenuBar.add(help); help.addSeparator(); JMenu exit = new JMenu("退出"); TestJMenuBar.add(exit); this.setDefaultCloseOperation(EXIT_ON_CLOSE); initButtons();//primary.doClick(); this.setSize(1000, 1000); setBounds(400,100,450,500); setResizable(false); setVisible(true); } public void initButtons() { /* if (panel2 != null) { this.remove(panel2); }*/ label1=new JLabel("设置当前雷数"); start = new JButton("开始"); panel1=new JPanel(); panel1.add(label1); panel1.add(start); container.add(panel1,BorderLayout.NORTH); panel2=new JPanel(); panel2.setLayout(new GridLayout(row,col,2,2)); this.remove(panel2); container.add(panel2,BorderLayout.CENTER); start.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { startBomb(); } }); buttons= new Bomb[row][col]; for(int i = 0 ; i < row ; i++) { for(int j = 0 ; j < col ; j++) { buttons[i][j]= new Bomb(i,j); buttons[i][j].addMouseListener(new Bomb_mouseAdapter(this)); buttons[i][j].addActionListener(new Bomb_actionAdapter(this)); panel2.add(buttons[i][j]); } } //container.add(panel2,BorderLayout.CENTER); this.validate(); startBomb(); //窗口完成后开始布雷 } /* 开始,布雷 */ public void startBomb() { label1.setText("设置当前雷数" + ":" + BombNum); for (int i = 0; i < row ; i++) { for (int j = 0; j < col ; j++) { buttons[i][j].isBomb = false; buttons[i][j].isClicked = false; buttons[i][j].isRight = false; buttons[i][j].BombFlag = 0; buttons[i][j].BombRoundCount = 8; buttons[i][j].setEnabled(true); //设置的控件(这里是按钮buttons[i][j])是否被禁用,true是可用。 buttons[i][j].setText(""); foundBomb = 0; restBomb = BombNum; restbuttons = row*col - BombNum; } } for (int i = 0; i < BombNum; ) { int x = (int)(Math.random()*row); int y = (int)(Math.random()*col); if (buttons[x][y].isBomb != true) { buttons[x][y].isBomb = true; i++; } } CountRoundBomb(); //周围雷的个数 } public void CountRoundBomb() { //周围雷的个数 for (int i = 0; i < row ; i++) { for (int j = 0; j < col ; j++) { int count = 0; // 当需要检测的单元格本身无地雷的情况下,统计周围的地雷个数 if (buttons[i][j].isBomb != true) { for (int x = i - 1; x < i + 2; x++) { for (int y = j - 1; y < j + 2; y++) { if ( (x >= 0) && (y >= 0) && (x < row ) && (y < col )) { if (buttons[x][y].isBomb == true) { count++; } } } } buttons[i][j].BombRoundCount = count; } } } } /* 是否挖完了所有的雷就胜利啦*/ public void isWin() { restbuttons = row*col - BombNum; for (int i = 0; i < row ; i++) { for (int j = 0; j < col ; j++) { if (buttons[i][j].isClicked == true) { restbuttons--; } } } if (foundBomb == BombNum || restbuttons == 0) { JOptionPane.showMessageDialog(this, "您挖完了所有的雷,您胜利了!", "胜利",JOptionPane.INFORMATION_MESSAGE); startBomb(); } } /*当选中的位置为空,则翻开周围的地图*/ public void isNull_turnRound(Bomb ClickedButton) { int i, j; i = ClickedButton.nx; //按钮的横坐标 j = ClickedButton.ny; //按钮的纵坐标 for (int x = i - 1; x < i + 2; x++ ) { for (int y = j - 1; y < j + 2; y++ ) { if ( ( (x != i) || (y != j)) && (x >= 0) && (y >= 0) && (x < row ) && (y < col )) { if (buttons[x][y].isBomb == false && buttons[x][y].isClicked == false && buttons[x][y].isRight == false) { turn(buttons[x][y]); } } } } } /* 翻开 */ public void turn(Bomb ClickedButton) { ClickedButton.setEnabled(false); ClickedButton.isClicked = true; if (ClickedButton.BombRoundCount > 0) { ClickedButton.setText(ClickedButton.BombRoundCount + ""); } else { isNull_turnRound(ClickedButton); } } /* 左键点击 */ public void actionPerformed(ActionEvent e) { if ( ( (Bomb) e.getSource()).isClicked == false && ( (Bomb) e.getSource()).isRight == false) { if ( ( (Bomb) e.getSource()).isBomb == false) { turn( ( (Bomb) e.getSource())); isWin(); } else { for (int i = 0; i < row ; i++) { for (int j = 0; j < col ; j++) { if (buttons[i][j].isBomb == true) { buttons[i][j].setText("B"); } } } ( (Bomb) e.getSource()).setForeground(Color.RED); //事件源的背景色 ( (Bomb) e.getSource()).setText("X"); JOptionPane.showMessageDialog(this, "你踩到地雷了,按确定重来", "踩到地雷", 2); startBomb(); } } } /* 右键点击 */ public void mouseClicked(MouseEvent e) { Bomb bombSource = (Bomb) e.getSource(); boolean right = SwingUtilities.isRightMouseButton(e); if ( (right == true) && (bombSource.isClicked == false)) { bombSource.BombFlag = (bombSource.BombFlag + 1) % 3; if (bombSource.BombFlag == 1) { if (restBomb > 0) { bombSource.setForeground(Color.RED); bombSource.setText("F"); bombSource.isRight = true; restBomb--; } else { bombSource.BombFlag = 0; } } else if (bombSource.BombFlag == 2) { restBomb++; bombSource.setText(""); bombSource.isRight = false; } if (bombSource.isBomb == true) { if (bombSource.BombFlag == 1) { foundBomb++; } else if (bombSource.BombFlag == 2) { foundBomb--; } } label1.setText("当前雷数" + ":" + restBomb); isWin(); } } public static void main(String[] args) { saolei m = new saolei(); m.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); } }////////////////////////////////////////////////////////////////////////////////class Bomb extends JButton { int nx, ny; // 雷所在的坐标 int BombRoundCount; // 周围雷数 boolean isBomb; // 是否为雷 boolean isClicked; // 是否点击左键 int BombFlag; // 探雷标记 boolean isRight; // 是否点击右键 public Bomb(int x, int y) { nx = x; ny = y; BombFlag = 0; BombRoundCount = 8; isBomb = false; isClicked = false; isRight = false; }}class Bomb_actionAdapter implements ActionListener { private saolei adaptee; Bomb_actionAdapter(saolei adaptee) { this.adaptee = adaptee; } public void actionPerformed(ActionEvent e) { adaptee.actionPerformed(e); }}class Bomb_mouseAdapter extends MouseAdapter { private saolei adaptee; Bomb_mouseAdapter(saolei adaptee) { this.adaptee = adaptee; } public void mouseClicked(MouseEvent e) { adaptee.mouseClicked(e); }} 你需要认真学习一下变量的有效范围、生命周期,以及搞清楚你每个语句、事件过程中的变量的值。另外,按java的习惯,任何变量在声明的时候都应该赋初始值。 import javax.swing.*;import java.awt.*;import java.awt.event.ActionEvent;import java.awt.event.ActionListener; public class MenuEvent extends JFrame { private JPanel panel; public MenuEvent() { JMenuBar jMenuBar = new JMenuBar(); JMenu jMenu = new JMenu("选项"); JMenuItem item1 = new JMenuItem("初级"); JMenuItem item2 = new JMenuItem("中级"); JMenuItem item3 = new JMenuItem("高级"); jMenu.add(item1); jMenu.addSeparator(); jMenu.add(item2); jMenu.addSeparator(); jMenu.add(item3); jMenuBar.add(jMenu); this.setJMenuBar(jMenuBar); this.setDefaultCloseOperation(EXIT_ON_CLOSE); this.setSize(500, 500); item1.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { initButtons(10, 10); } }); item2.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { initButtons(15, 15); } }); item3.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { initButtons(20, 20); } }); item1.doClick(); } private void initButtons(int rows, int cols) { if (panel != null) { this.remove(panel); } panel = new JPanel(); panel.setLayout(new GridLayout(rows, cols, 2, 2)); for (int i = 0; i < rows * cols; i++) { panel.add(new JButton()); } this.add(panel, BorderLayout.CENTER); this.setVisible(true); } public static void main(String[] args) { new MenuEvent(); }} 求助:java如何合并在指定目录下的两个文件,放在一个新的文件中。 高手帮忙看看这道算法题如何做?“六度分隔”理论。谢谢 找不到 setAutoCreateRowSorter 新建一java qq群(24286605) 欢迎大家加入 关于隐藏列的问题 !!!求助连接sqlserver2000的问题,大家帮忙啊 找到从APPLET转换到HTML和JAVASCRIPT的东西了! 如何处理mouse 双击事件 如何将bmp转化为wbmp呀 招聘(声明:选择这里贴如此的帖子也许不合适,但觉得这里人才颇多,所以请斑竹保留几天。 :) ) SVN到底该怎么用? 函数传递Integer参数不是传递引用吗?
import java.awt.event.*;import javax.swing.*;public class MenuEvent extends JFrame {
private JMenuBar aaa;
private JMenu bbb;
private JPanel panel;
private JButton buttons[][];
int a = 5, b = 5;
Container c = getContentPane(); public MenuEvent() {
aaa = new JMenuBar();
this.setJMenuBar(aaa);
bbb = new JMenu("选项");
aaa.add(bbb);
JMenuItem ccc = new JMenuItem("初级");
JMenuItem ddd = new JMenuItem("中级");
JMenuItem eee = new JMenuItem("高级");
ccc.addActionListener(new ActionListener() { @Override
public void actionPerformed(ActionEvent e) {
a = 10;
b = 10;
refreshUI();
}
});
bbb.add(ccc);
bbb.addSeparator();
ddd.addActionListener(new ActionListener() { @Override
public void actionPerformed(ActionEvent e) {
a = 15;
b = 15;
refreshUI();
}
});
bbb.add(ddd);
bbb.addSeparator();
eee.addActionListener(new ActionListener() { @Override
public void actionPerformed(ActionEvent e) {
a = 20;
b = 20;
refreshUI();
}
});
bbb.add(eee); refreshUI();
setSize(500, 500);
setVisible(true);
} private void refreshUI() {
buttons = new JButton[a][b];
panel = new JPanel();
panel.setLayout(new GridLayout(a, b, 2, 2));
for (int i = 0; i < a; i++) {
for (int j = 0; j < b; j++) {
buttons[i][j] = new JButton();
panel.add(buttons[i][j]);
}
}
c.removeAll();
c.add(panel, BorderLayout.CENTER);
this.validate();
} public static void main(String[] args) {
MenuEvent m = new MenuEvent();
}
}
import java.awt.*;
import java.awt.event.*;
import java.util.Scanner;import javax.swing.*;public class saolei extends JFrame {
private int row=9;//不设初始值。按下开始按钮,就卡死了
private int col=9;//不设初始值。按下开始按钮,就卡死了
Bomb[][] buttons; //没个按钮初始看做为雷按钮
private JButton start;
private JLabel label1; //雷数标签
private int BombNum=8; //设置雷的数目
private JPanel panel1; //标签面板
private JPanel panel2; // 雷面板
int foundBomb, restBomb, restbuttons; // 找到的地雷数,剩余雷数,剩余按钮
private int lei[][];
private Container container = getContentPane(); //构造方法
public saolei() {
this.setTitle("扫雷小游戏");
JMenuBar TestJMenuBar=new JMenuBar();
JMenuItem primary,medium,senior;
this.setJMenuBar(TestJMenuBar);
JMenu begin=new JMenu("开始");
TestJMenuBar.add(begin);
primary=new JMenuItem("初级");
primary.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
row=10;
col=10;
BombNum=10;
initButtons();
}
});
begin.add(primary);
begin.addSeparator();
medium=new JMenuItem("中级");
medium.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
row=15;
col=15;
BombNum=15;
initButtons();
}
});
begin.add(medium);
begin.addSeparator();
senior=new JMenuItem("高级");
senior.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
row=20;
col=20;
BombNum=20;
initButtons();
}
});
begin.add(senior);
JMenu choose=new JMenu("选项");
TestJMenuBar.add(choose);
choose.addSeparator();
JMenu change = new JMenu("改变颜色");
choose.add(change);
change.add(new JMenuItem("红色"));
change.add(new JMenuItem("黄色"));
change.add(new JMenuItem("紫色"));
change.add(new JMenuItem("蓝色"));
choose.addSeparator();
JMenu help = new JMenu("帮助");
TestJMenuBar.add(help);
help.addSeparator();
JMenu exit = new JMenu("退出");
TestJMenuBar.add(exit);
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
initButtons();//primary.doClick();
this.setSize(1000, 1000);
setBounds(400,100,450,500);
setResizable(false);
setVisible(true);
}
public void initButtons() {
/* if (panel2 != null) {
this.remove(panel2);
}*/
label1=new JLabel("设置当前雷数");
start = new JButton("开始");
panel1=new JPanel();
panel1.add(label1);
panel1.add(start);
container.add(panel1,BorderLayout.NORTH);
panel2=new JPanel();
panel2.setLayout(new GridLayout(row,col,2,2));
this.remove(panel2);
container.add(panel2,BorderLayout.CENTER);
start.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
startBomb();
}
});
buttons= new Bomb[row][col];
for(int i = 0 ; i < row ; i++) {
for(int j = 0 ; j < col ; j++) {
buttons[i][j]= new Bomb(i,j);
buttons[i][j].addMouseListener(new Bomb_mouseAdapter(this));
buttons[i][j].addActionListener(new Bomb_actionAdapter(this));
panel2.add(buttons[i][j]);
}
}
//container.add(panel2,BorderLayout.CENTER);
this.validate();
startBomb(); //窗口完成后开始布雷
} /* 开始,布雷 */ public void startBomb() {
label1.setText("设置当前雷数" + ":" + BombNum);
for (int i = 0; i < row ; i++) {
for (int j = 0; j < col ; j++) {
buttons[i][j].isBomb = false;
buttons[i][j].isClicked = false;
buttons[i][j].isRight = false;
buttons[i][j].BombFlag = 0;
buttons[i][j].BombRoundCount = 8;
buttons[i][j].setEnabled(true); //设置的控件(这里是按钮buttons[i][j])是否被禁用,true是可用。
buttons[i][j].setText("");
foundBomb = 0;
restBomb = BombNum;
restbuttons = row*col - BombNum;
}
} for (int i = 0; i < BombNum; ) {
int x = (int)(Math.random()*row);
int y = (int)(Math.random()*col); if (buttons[x][y].isBomb != true) {
buttons[x][y].isBomb = true;
i++;
}
}
CountRoundBomb(); //周围雷的个数
}
public void CountRoundBomb() { //周围雷的个数
for (int i = 0; i < row ; i++) {
for (int j = 0; j < col ; j++) {
int count = 0;
// 当需要检测的单元格本身无地雷的情况下,统计周围的地雷个数
if (buttons[i][j].isBomb != true) {
for (int x = i - 1; x < i + 2; x++) {
for (int y = j - 1; y < j + 2; y++) {
if ( (x >= 0) && (y >= 0) && (x < row ) && (y < col )) {
if (buttons[x][y].isBomb == true) {
count++;
}
}
}
}
buttons[i][j].BombRoundCount = count;
}
}
}
}
/* 是否挖完了所有的雷就胜利啦*/
public void isWin() {
restbuttons = row*col - BombNum;
for (int i = 0; i < row ; i++) {
for (int j = 0; j < col ; j++) {
if (buttons[i][j].isClicked == true) {
restbuttons--;
}
}
} if (foundBomb == BombNum || restbuttons == 0) {
JOptionPane.showMessageDialog(this, "您挖完了所有的雷,您胜利了!", "胜利",JOptionPane.INFORMATION_MESSAGE);
startBomb();
}
}
/*当选中的位置为空,则翻开周围的地图*/ public void isNull_turnRound(Bomb ClickedButton) {
int i, j;
i = ClickedButton.nx; //按钮的横坐标
j = ClickedButton.ny; //按钮的纵坐标
for (int x = i - 1; x < i + 2; x++ ) {
for (int y = j - 1; y < j + 2; y++ ) {
if ( ( (x != i) || (y != j)) && (x >= 0) && (y >= 0) && (x < row ) && (y < col )) {
if (buttons[x][y].isBomb == false && buttons[x][y].isClicked == false && buttons[x][y].isRight == false) {
turn(buttons[x][y]);
}
}
}
}
} /* 翻开 */
public void turn(Bomb ClickedButton) {
ClickedButton.setEnabled(false);
ClickedButton.isClicked = true;
if (ClickedButton.BombRoundCount > 0) {
ClickedButton.setText(ClickedButton.BombRoundCount + "");
}
else {
isNull_turnRound(ClickedButton);
}
}
/* 左键点击 */ public void actionPerformed(ActionEvent e) {
if ( ( (Bomb) e.getSource()).isClicked == false && ( (Bomb) e.getSource()).isRight == false) {
if ( ( (Bomb) e.getSource()).isBomb == false) {
turn( ( (Bomb) e.getSource()));
isWin();
} else {
for (int i = 0; i < row ; i++) {
for (int j = 0; j < col ; j++) {
if (buttons[i][j].isBomb == true) {
buttons[i][j].setText("B");
}
}
}
( (Bomb) e.getSource()).setForeground(Color.RED); //事件源的背景色
( (Bomb) e.getSource()).setText("X");
JOptionPane.showMessageDialog(this, "你踩到地雷了,按确定重来", "踩到地雷", 2);
startBomb();
}
}
} /* 右键点击 */ public void mouseClicked(MouseEvent e) {
Bomb bombSource = (Bomb) e.getSource();
boolean right = SwingUtilities.isRightMouseButton(e); if ( (right == true) && (bombSource.isClicked == false)) {
bombSource.BombFlag = (bombSource.BombFlag + 1) % 3;
if (bombSource.BombFlag == 1) {
if (restBomb > 0) {
bombSource.setForeground(Color.RED);
bombSource.setText("F");
bombSource.isRight = true;
restBomb--;
}
else {
bombSource.BombFlag = 0;
}
}
else if (bombSource.BombFlag == 2) {
restBomb++;
bombSource.setText("");
bombSource.isRight = false;
} if (bombSource.isBomb == true) {
if (bombSource.BombFlag == 1) {
foundBomb++;
}
else if (bombSource.BombFlag == 2) {
foundBomb--;
}
}
label1.setText("当前雷数" + ":" + restBomb);
isWin();
}
}
public static void main(String[] args) {
saolei m = new saolei();
m.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}////////////////////////////////////////////////////////////////////////////////class Bomb extends JButton {
int nx, ny; // 雷所在的坐标
int BombRoundCount; // 周围雷数
boolean isBomb; // 是否为雷
boolean isClicked; // 是否点击左键
int BombFlag; // 探雷标记
boolean isRight; // 是否点击右键 public Bomb(int x, int y) {
nx = x;
ny = y;
BombFlag = 0;
BombRoundCount = 8;
isBomb = false;
isClicked = false;
isRight = false;
}
}class Bomb_actionAdapter implements ActionListener {
private saolei adaptee;
Bomb_actionAdapter(saolei adaptee) {
this.adaptee = adaptee;
}
public void actionPerformed(ActionEvent e) {
adaptee.actionPerformed(e);
}
}class Bomb_mouseAdapter extends MouseAdapter {
private saolei adaptee;
Bomb_mouseAdapter(saolei adaptee) {
this.adaptee = adaptee;
}
public void mouseClicked(MouseEvent e) {
adaptee.mouseClicked(e);
}
}
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class MenuEvent extends JFrame {
private JPanel panel;
public MenuEvent() {
JMenuBar jMenuBar = new JMenuBar();
JMenu jMenu = new JMenu("选项");
JMenuItem item1 = new JMenuItem("初级");
JMenuItem item2 = new JMenuItem("中级");
JMenuItem item3 = new JMenuItem("高级");
jMenu.add(item1);
jMenu.addSeparator();
jMenu.add(item2);
jMenu.addSeparator();
jMenu.add(item3);
jMenuBar.add(jMenu);
this.setJMenuBar(jMenuBar);
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
this.setSize(500, 500);
item1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
initButtons(10, 10);
}
});
item2.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
initButtons(15, 15);
}
});
item3.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
initButtons(20, 20);
}
});
item1.doClick();
}
private void initButtons(int rows, int cols) {
if (panel != null) {
this.remove(panel);
}
panel = new JPanel();
panel.setLayout(new GridLayout(rows, cols, 2, 2));
for (int i = 0; i < rows * cols; i++) {
panel.add(new JButton());
}
this.add(panel, BorderLayout.CENTER);
this.setVisible(true);
}
public static void main(String[] args) {
new MenuEvent();
}
}