2011-08-10 11 views
2

左右のマウスクリックに固有の特定のイベントアクションを作成する方法はありますか?掃海隊のアクションイベント

私は掃除機のGUIを作成していますので、正方形を左クリックすると、それが暴露されます。&が右クリックされると、フラグが立てられます。

私は文法的にこれをチェックする方法がわかりませんでしたが、&はツタンカーメンでそれを見つけることができませんでした。

ありがとうございました!

+0

私はCardLayoutを使用して、地雷セルの表示を最適にするために成功しました。例えば、すべてのセルは最初に、MouseListenerを介してマウスを右クリックすることによってIconをスワップすることができるJButtonを表示できます。次に、マウスの左キー(ActionListenerを使用)でボタンをクリックすると、CardLayoutを使用して、画像アイコンがあるJLabelが表示されるか、番号が表示されます。 –

+0

本当にクールなああ男!投稿してくれてありがとう、私はレイアウトを使用することは考えていないだろう。私は間違いなくそれを調べます。 – mdegges

答えて

5

私はタイマーやリセットなしで(まだ)単純なMine Sweeperアプリケーションを作成しようとしましたが、それは機能的でGUIセルクラスと非GUIモデルの両方を使用しますクラス(Javaの宿題の紹介にコピーして使用することはできません)。

編集1:今、リセット機能を持っています

MineSweeper.java:mainメソッドを保持し、開始したJFrame

import java.awt.event.*; 
import javax.swing.*; 

@SuppressWarnings("serial") 
public class MineSweeper { 
    private JPanel mainPanel = new JPanel(); 
    private MineCellGrid mineCellGrid; 
    private JButton resetButton = new JButton("Reset"); 

    public MineSweeper(int rows, int cols, int mineTotal) { 
     mainPanel.setLayout(new BoxLayout(mainPanel, BoxLayout.PAGE_AXIS)); 
     mineCellGrid = new MineCellGrid(rows, cols, mineTotal); 

     resetButton.setMnemonic(KeyEvent.VK_R); 
     resetButton.addActionListener(new ActionListener() { 
     public void actionPerformed(ActionEvent e) { 
      mineCellGrid.reset(); 
     } 
     }); 

     mainPanel.add(mineCellGrid); 
     mainPanel.add(new JSeparator()); 
     mainPanel.add(new JPanel(){{add(resetButton);}}); 
    } 

    private JPanel getMainPanel() { 
     return mainPanel; 
    } 

    private static void createAndShowUI() { 
     JFrame frame = new JFrame("MineSweeper"); 
     //frame.getContentPane().add(new MineSweeper(20, 20, 44).getMainPanel()); 
     frame.getContentPane().add(new MineSweeper(12, 12, 13).getMainPanel()); 
     frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     frame.pack(); 
     frame.setLocationRelativeTo(null); 
     frame.setVisible(true); 
    } 

    public static void main(String[] args) { 
     java.awt.EventQueue.invokeLater(new Runnable() { 
     public void run() { 
      createAndShowUI(); 
     } 
     }); 
    } 
} 

MineCellGrid.java:鉱山細胞と時間のグリッドを表示するクラスそれらのすべて一緒に。

import java.awt.GridLayout; 
import java.util.ArrayList; 
import java.util.List; 
import javax.swing.JPanel; 

@SuppressWarnings("serial") 
public class MineCellGrid extends JPanel { 
    private MineCellGridModel model; 
    private List<MineCell> mineCells = new ArrayList<MineCell>(); 

    public MineCellGrid(final int maxRows, final int maxCols, int mineNumber) { 
     model = new MineCellGridModel(maxRows, maxCols, mineNumber); 
     setLayout(new GridLayout(maxRows, maxCols)); 

     for (int row = 0; row < maxRows; row++) { 
     for (int col = 0; col < maxCols; col++) { 
      MineCell mineCell = new MineCell(row, col); 
      add(mineCell); 
      mineCells.add(mineCell); 
      model.add(mineCell.getModel(), row, col); 
     } 
     } 

     reset(); 
    } 

    public void reset() { 
     model.reset(); 
     for (MineCell mineCell : mineCells) { 
     mineCell.reset(); 
     } 
    } 
} 

MineCellGridModel.java:私が始めたクラス:MineCellGrid

import java.beans.PropertyChangeEvent; 
import java.beans.PropertyChangeListener; 
import java.util.ArrayList; 
import java.util.Collections; 
import java.util.List; 
import javax.swing.JOptionPane; 

public class MineCellGridModel { 
    private MineCellModel[][] cellModelGrid; 
    private List<Boolean> mineList = new ArrayList<Boolean>(); 
    private CellModelPropertyChangeListener cellModelPropChangeListener = new CellModelPropertyChangeListener(); 
    private int maxRows; 
    private int maxCols; 
    private int mineNumber; 
    private int buttonsRemaining; 

    public MineCellGridModel(final int maxRows, final int maxCols, int mineNumber) { 
     this.maxRows = maxRows; 
     this.maxCols = maxCols; 
     this.mineNumber = mineNumber; 
     for (int i = 0; i < maxRows * maxCols; i++) { 
     mineList.add((i < mineNumber) ? true : false); 
     } 
     cellModelGrid = new MineCellModel[maxRows][maxCols]; 
     buttonsRemaining = (maxRows * maxCols) - mineNumber; 
    } 

    public void add(MineCellModel model, int row, int col) { 
     cellModelGrid[row][col] = model; 
     model.addPropertyChangeListener(cellModelPropChangeListener); 
    } 

    public void reset() { 
     buttonsRemaining = (maxRows * maxCols) - mineNumber; 

     // randomize the mine location 
     Collections.shuffle(mineList); 
     // reset the model grid and set mines 
     for (int r = 0; r < cellModelGrid.length; r++) { 
     for (int c = 0; c < cellModelGrid[r].length; c++) { 
      cellModelGrid[r][c].reset(); 
      cellModelGrid[r][c].setMined(mineList.get(r 
        * cellModelGrid[r].length + c)); 
     } 
     } 
     // advance value property of all neighbors of a mined cell 
     for (int r = 0; r < cellModelGrid.length; r++) { 
     for (int c = 0; c < cellModelGrid[r].length; c++) { 
      if (cellModelGrid[r][c].isMined()) { 
       int rMin = Math.max(r - 1, 0); 
       int cMin = Math.max(c - 1, 0); 
       int rMax = Math.min(r + 1, cellModelGrid.length - 1); 
       int cMax = Math.min(c + 1, cellModelGrid[r].length - 1); 
       for (int row2 = rMin; row2 <= rMax; row2++) { 
        for (int col2 = cMin; col2 <= cMax; col2++) { 
        cellModelGrid[row2][col2].incrementValue(); 
        } 
       } 
      } 
     } 
     } 
    } 

    private class CellModelPropertyChangeListener implements 
      PropertyChangeListener { 

     public void propertyChange(PropertyChangeEvent evt) { 
     MineCellModel model = (MineCellModel) evt.getSource(); 
     int row = model.getRow(); 
     int col = model.getCol(); 

     if (evt.getPropertyName().equals(MineCellModel.BUTTON_PRESSED)) { 
      if (cellModelGrid[row][col].isMineBlown()) { 
       mineBlown(); 
      } else { 
       buttonsRemaining--; 
       if (buttonsRemaining <= 0) { 
        JOptionPane.showMessageDialog(null, "You've Won!!!", "Congratulations", JOptionPane.PLAIN_MESSAGE); 
       } 
       if (cellModelGrid[row][col].getValue() == 0) { 
        zeroValuePress(row, col); 
       } 
      } 
     } 
     } 

     private void mineBlown() { 
     for (int r = 0; r < cellModelGrid.length; r++) { 
      for (int c = 0; c < cellModelGrid[r].length; c++) { 
       MineCellModel model = cellModelGrid[r][c]; 
       if (model.isMined()) { 
        model.setMineBlown(true); 
       } 
      } 
     } 

     } 

     private void zeroValuePress(int row, int col) { 
     int rMin = Math.max(row - 1, 0); 
     int cMin = Math.max(col - 1, 0); 
     int rMax = Math.min(row + 1, cellModelGrid.length - 1); 
     int cMax = Math.min(col + 1, cellModelGrid[row].length - 1); 
     for (int row2 = rMin; row2 <= rMax; row2++) { 
      for (int col2 = cMin; col2 <= cMax; col2++) { 
       cellModelGrid[row2][col2].pressedAction(); 
      } 
     } 
     } 
    } 
} 

MineCell.javaための非GUIモデル。モデルクラスを非GUIニュークリアスとして使用します。

import java.awt.CardLayout; 
import java.awt.Color; 
import java.awt.Dimension; 
import java.awt.Font; 
import java.awt.Insets; 
import java.awt.event.*; 
import java.beans.PropertyChangeEvent; 
import java.beans.PropertyChangeListener; 
import javax.swing.*; 

/** 
* http://stackoverflow.com/questions/7006029/minesweeper-action-events 
* 
* @author Pete 
*/ 
@SuppressWarnings("serial") 
public class MineCell extends JPanel { 
    private static final String LABEL = "label"; 
    private static final String BUTTON = "button"; 
    private static final int PS_WIDTH = 24; 
    private static final int PS_HEIGHT = PS_WIDTH; 
    private static final float LABEL_FONT_SIZE = (float) (24 * PS_WIDTH)/30f; 
    private static final float BUTTON_FONT_SIZE = (float) (14 * PS_WIDTH)/30f; 
    private JButton button = new JButton(); 
    private JLabel label = new JLabel(" ", SwingConstants.CENTER); 
    private CardLayout cardLayout = new CardLayout(); 
    private MineCellModel model; 

    public MineCell(final boolean mined, int row, int col) { 
     model = new MineCellModel(mined, row, col); 
     model.addPropertyChangeListener(new MyPCListener()); 
     label.setFont(label.getFont().deriveFont(Font.BOLD, LABEL_FONT_SIZE)); 
     button.setFont(button.getFont().deriveFont(Font.PLAIN, BUTTON_FONT_SIZE)); 
     button.setMargin(new Insets(1, 1, 1, 1)); 
     setLayout(cardLayout); 

     add(button, BUTTON); 
     add(label, LABEL); 

     button.addActionListener(new ActionListener() { 
     public void actionPerformed(ActionEvent e) { 
      pressedAction(); 
     } 
     }); 
     button.addMouseListener(new MouseAdapter() { 
     @Override 
     public void mousePressed(MouseEvent e) { 
      if (e.getButton() == MouseEvent.BUTTON3) { 
       model.upDateButtonFlag(); 
      } 
     } 
     }); 
    } 

    public MineCell(int row, int col) { 
     this(false, row, col); 
    } 

    @Override 
    public Dimension getPreferredSize() { 
     return new Dimension(PS_WIDTH, PS_HEIGHT); 
    } 

    public void pressedAction() { 
     if (model.isFlagged()) { 
     return; 
     } 
     model.pressedAction(); 
    } 

    public void showCard(String cardConstant) { 
     cardLayout.show(this, cardConstant); 
    } 

    // TODO: have this change the button's icon 
    public void setFlag(boolean flag) { 
     if (flag) { 
     button.setBackground(Color.yellow); 
     button.setForeground(Color.red); 
     button.setText("f"); 
     } else { 
     button.setBackground(null); 
     button.setForeground(null); 
     button.setText(""); 
     } 
    } 

    private void setMineBlown(boolean mineBlown) { 
     if (mineBlown) { 
     label.setBackground(Color.red); 
     label.setOpaque(true); 
     showCard(LABEL); 
     } else { 
     label.setBackground(null); 
     } 
    } 

    public MineCellModel getModel() { 
     return model; 
    } 

    public void addPropertyChangeListener(PropertyChangeListener listener) { 
     model.addPropertyChangeListener(listener); 
    } 

    public void removePropertyChangeListener(PropertyChangeListener listener) { 
     model.removePropertyChangeListener(listener); 
    } 

    private class MyPCListener implements PropertyChangeListener { 
     public void propertyChange(PropertyChangeEvent evt) { 
     String propName = evt.getPropertyName(); 
     if (propName.equals(MineCellModel.MINE_BLOWN)) { 
      setMineBlown(true); 
     } else if (propName.equals(MineCellModel.FLAG_CHANGE)) { 
      setFlag(model.isFlagged()); 
     } else if (propName.equals(MineCellModel.BUTTON_PRESSED)) { 
      if (model.isMineBlown()) { 
       setMineBlown(true); 
      } else { 
       String labelText = (model.getValue() == 0) ? "" : String 
         .valueOf(model.getValue()); 
       label.setText(labelText); 
      } 
      showCard(LABEL); 
     } 
     } 
    } 

    public void reset() { 
     setFlag(false); 
     setMineBlown(false); 
     showCard(BUTTON); 
     label.setText(""); 
    } 

} 

MineCellModel。java:鉱山セルの非GUIモデル

+0

Edit 1:リセット機能があり、グリッドに非GUIモデルが追加されました。 –

1

あなたはjava.awt.eventのMouseEventクラスに興味があるかもしれません。 here

+1

リンクは非常に古いです。 JavaSE 1.4.2用です。 –

+0

申し訳ありませんが、私は気付かなかった。私はリンクを更新しました。 – wespiserA

2

あなたが

スイングを使用している場合は、左に固有の特定のイベントのアクションと 、マウスの右クリックを作成する方法はありますか?

MouseListenerコンポーネントを実装しません。次に実装されたメソッドではMouseEventオブジェクトがあり、どのマウスが押されたかを示すgetButton()メソッドがあります。

編集

OPは、次の質問をしたが、今、それを削除しています。

game_lostが真になったときに、アクションイベントで他の中にネストされたこのGUIですか?

このためにJDialogを開くことができます。

+0

ありがとう!私が2番目の質問を投稿したとき、私はその部分を私が探していたツタに見ました。 – mdegges

+0

あなたは大歓迎です。 –

関連する問題