Jbutton上单击鼠标右键,怎样使其发生对应的变化..
以前只有C的一点基础,上午看了下JAVA的电子教程,对着一个计算器源码想编写一个扫雷程序.但是不知道怎样让鼠标在一个JButton上单击右键时候,所对应的Jbutton变成红色(标记地雷).还有很多小问题,比如按钮太小数字会显示成 "... ",扫雷算法我也没考虑好...反正其他先不管了,先解决鼠标右键再说吧..
========以下半成品,说不定是废品==============
import java.awt.*;
import java.lang.*;
import javax.swing.*;
import javax.swing.event.*;
import java.awt.event.*;
import java.text.DecimalFormat;
import java.util.TimerTask;
import java.util.Timer;
import java.util.Random;
public class Saolei implements ActionListener,MouseListener{
JFrame frame;
JPanel panel, panel1, panel2, panel3;
JMenuBar mainMenu;
JButton button[];
JMenu gameMenu,helpMenu;
JMenuItem startItem, recordItem, exitItem, helpItem, aboutItem;
JTextArea help;
JTextField timeField, numberField;
JScrollPane scrollHelp;
int index;
//布局
public Saolei() {
frame = new JFrame( "扫雷 ");
panel = new JPanel();
frame.getContentPane().add(panel);
panel.setLayout(new BorderLayout());
panel1 = new JPanel();
panel1.setPreferredSize(new Dimension(300,40));
panel2 = new JPanel();
panel2.setPreferredSize(new Dimension(300,300));
//菜单
mainMenu = new JMenuBar();
gameMenu = new JMenu( "游戏 ");
helpMenu = new JMenu( "帮助 ");
startItem = new JMenuItem( "开局 ");
recordItem = new JMenuItem( "排名 ");
exitItem = new JMenuItem( "退出 ");
helpItem = new JMenuItem( "帮助 ");
aboutItem = new JMenuItem( "关于 ");
mainMenu.add(gameMenu);
mainMenu.add(helpMenu);
gameMenu.add(startItem);
gameMenu.add(recordItem);
gameMenu.add(exitItem);
helpMenu.add(helpItem);
helpMenu.add(aboutItem);
startItem.addActionListener(this);
recordItem.addActionListener(this);
exitItem.addActionListener(this);
helpItem.addActionListener(this);
aboutItem.addActionListener(this);
help = new JTextArea(8, 40);
scrollHelp = new JScrollPane(help);
help.setEditable(false);
help.append( "扫雷玩法\n ");
help.append( "1.在“游戏”菜单上,单击“开局”。\n ");
help.append( "2.要启动计时器,请单击游戏区中的任何方块。\n ");
help.append( "=======================\n ");
help.append( "注意:\n ");
help.append( "1.通过单击即可挖开方块。如果挖开的是地雷,则您输掉游戏。\n ");
help.append( "2.如果方块上出现数字,则表示在其周围的八个方块中共有多少颗地雷。\n ");
help.append( "3.要标记您认为可能有地雷的方块,请右键单击它。 \n ");
help.append( "4.游戏区包括雷区、地雷计数器和计时器。\n ");
panel.add(mainMenu, BorderLayout.NORTH);
panel.add(panel1, BorderLayout.CENTER);
panel.add(panel2, BorderLayout.SOUTH);
//时间 剩余地雷显示
panel1.setLayout(new BorderLayout());
numberField = new JTextField();
numberField.setPreferredSize(new Dimension(40,40));
numberField.setEditable(false);
numberField.setBackground(new Color(217, 217, 217));
panel1.add(numberField, BorderLayout.WEST
);
//游戏面板
panel2.setLayout(new GridLayout(9,9));
button = new JButton[81];
for (int i = 0; i < button.length; i++) {
button[i] = new JButton();
button[i].setForeground(new Color(0, 0, 255));
panel2.add(button[i]);
button[i].addMouseListener(this);
button[i].addActionListener(this);
}
//窗口
frame.setDefaultCloseOperation(frame.EXIT_ON_CLOSE);
frame.pack();
frame.setVisible(true);
frame.setResizable(false);
}
//产生地雷序号
public void Start() {
int[] dilei=new int[10];
int j=1;
dilei[0]=(int)(Math.random()*81);
while(j <dilei.length){
dilei[j]=(int)(Math.random()*81);
boolean b=true;
for(int k=0;k <j;k++){
if(dilei[j]==dilei[k]){
b=false;
}
}
if(b==true)
j++;
}
}
//鼠标,问题就在这里,如何让index与右键所点击的Jbutton对应啊
public void mouseClicked(MouseEvent e) {
int mod = e.getModifiers();
if(mod ==InputEvent.BUTTON3_MASK)
button[index].setBackground(Color.RED);
}
public void mouseEntered(MouseEvent e) {
}
public void mouseExited(MouseEvent e) {
}
public void mousePressed(MouseEvent e) {
}
public void mouseReleased(MouseEvent e) {
}
//设置各个按钮行为
public void actionPerformed(ActionEvent event) {
Object temp = event.getSource();
try{
for(int i = 0; i <81; i++){
if(temp == button[i])
button[i].setBackground(Color.BLUE);
}
//按下 "开局 "
if (temp == startItem) {
Start();
}
//按下 "排名 "
if (temp == recordItem) {
Start();
}
//按下 "退出 "
if (temp == exitItem) {
System.exit(0);
}
//按下 "帮助 "
if (temp == helpItem) {
JOptionPane.showMessageDialog(panel, scrollHelp);
}
//按下 "关于 "
if (temp == aboutItem) {
JOptionPane.showMessageDialog(panel, " 扫雷测试版\n开发者:MagicWolf ");
}
}
catch (Exception e) {
}
}
public static void main(String args[]) {
new Saolei();
}
}
[解决办法]
if(mod ==InputEvent.BUTTON3_MASK)
((JButton)e.getSource()).setBackground(Color.RED);
这样就可以 点右键 变红