2016-10-25 8 views
0

私は以下のコードを使用して(xSize = 10)(ySize = 10)JButtonのグリッドを作成しています。このコードが置かれているクラス内でボタンを操作する限り、ボタンを変更することができます。グリッドボタンとアクションリスナー

しかし、ボタンのテキストを別のクラスから変更しようとしています。その本質に煮詰めたときに、私は、これは良い方法

で質問を願っています

public class Ocean { 

public Ocean(){ 
} 

private final int xSize = 10; 
private final int ySize = 10; 
private final JButton[][] buttons = new JButton[xSize][ySize]; 

public gameBoard() { 
    for (int row = 0; row < xSize; row++) { 
     for (int col = 0; col < ySize; col++) { 
      buttons[row][col] = new JButton(/*space1*/); 
      buttons[row][col].setText("E"); 
      buttons[row][col].addActionListener(new myActionListener()); 
      buttons[row][col].setPreferredSize(new Dimension(50, 50)); 
      playerPanel.add(buttons[row][col]); 
     } 
    } 

} 

次のクラス

public class Battleship { 

    int length = 4; 

    public Battleship() { 
    } 

    public changeText(int row, int col) { 
     ocean.buttons[row][col].setText("H");  need to make this work 
    } 

はあなたに

答えて

2

あなたの質問に感謝単にこれです:どのようにすることができます私は、あるオブジェクトが別のオブジェクトの状態を変更しているのですか?

この猫をスキンする方法はたくさんありますが、シンプルなのは、外部のActionListenerが呼び出すことができるOceanクラス(ここではgameBoard?あなたのコードは混乱します)のpublicメソッドを与えることです。

public void setText(int row, int col, String text) { 
    buttons[row][col].setText(text); 
} 

のActionListener可能、コンストラクタのパラメータによってこのグリッド保持クラスへの参照を渡すことができますので、は次のようにのsetText(...):たとえば、あなたは、ボタングリッド保持クラスにパブリックメソッドを与えることができます必要に応じてこのメソッドを呼び出すことができます。

ActionListenerのActionPerformedメソッドのActionEventパラメータを使用して、常に押されたJButtonへの参照を取得できることにも注意してください。これは、イベントを開始したオブジェクト(ここではJButton)への参照を返すメソッドgetSource()を持っています。

さらに詳しい解決方法が必要な場合は、有効なMinimal, Complete, and Verifiable exampleを作成して投稿することを検討してください。例えば

import java.awt.GridLayout; 
import java.awt.event.ActionEvent; 
import java.awt.event.ActionListener; 

import javax.swing.*; 

public class SimpleButtonGridMain extends JPanel { 
    private static void createAndShowGui() { 
     SimpleButtonGrid simpleButtonGrid = new SimpleButtonGrid(); 

     MyButtonListener myButtonListener = new MyButtonListener(); 
     simpleButtonGrid.addActionListener(myButtonListener); 

     JFrame frame = new JFrame("SimpleButtonGridMain"); 
     frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     frame.getContentPane().add(simpleButtonGrid); 
     frame.pack(); 
     frame.setLocationRelativeTo(null); 
     frame.setVisible(true); 
    } 

    public static void main(String[] args) { 
     SwingUtilities.invokeLater(() -> createAndShowGui()); 
    } 
} 

class SimpleButtonGrid extends JPanel { 
    private static final int ROWS = 10; 
    private static final int COLS = ROWS; 
    private JButton[][] buttons = new JButton[ROWS][COLS]; 

    public SimpleButtonGrid() { 
     setLayout(new GridLayout(ROWS, COLS, 3, 3)); 
     for (int row = 0; row < buttons.length; row++) { 
      for (int col = 0; col < buttons[row].length; col++) { 
       String text = String.format(" [ %d, %d ] ", col, row); 
       JButton button = new JButton(text); 
       add(button); 
       buttons[row][col] = button; 
      } 
     } 
    } 

    public void addActionListener(ActionListener listener) { 
     for (JButton[] jButtons : buttons) { 
      for (JButton jButton : jButtons) { 
       jButton.addActionListener(listener); 
      } 
     } 
    } 
} 

class MyButtonListener implements ActionListener { 
    @Override 
    public void actionPerformed(ActionEvent e) { 
     String actionCommand = e.getActionCommand(); 
     JButton sourceBtn = (JButton) e.getSource(); 

     String message = "Button pressed: " + actionCommand; 
     JOptionPane.showMessageDialog(sourceBtn, message, "Button Pressed", JOptionPane.PLAIN_MESSAGE); 
     sourceBtn.setText("Pressed"); 
     sourceBtn.setEnabled(false); 
    } 
} 
2

それはあなたのコードの中で何をしたいのか、右は明らかではありません。ボタンがクリックされた場合、サンプルコードはすべてのボタンのテキストを変更します。

たぶん、あなたは、このコードは助けることができる、クリックされたボタンのテキストを変更したい:ここ

@Override 
public void actionPerformed(ActionEvent e) { 
    JButton theClickedButton = (JButton) e.getSource(); 
    theClickedButton.setText("M"); 
} 
+0

私は自分の質問であまりにも明確ではなかったことを実感しました。私は本当にどのように、何を尋ねるのか分かりませんでした。それを処理していただきありがとうございます。私がしようとしていることは、船をボードに置くことです。私が選択した場所は、船の種類に基づいてテキストをセットレターに変更します。私は、プレイヤーの推測のためにボタンをクリックするためにactionPerformedを使用しています。 – Asuu

+0

船の位置とタイプの情報をJButton配列ではなく、この情報を表すクラスに保存すべきだと思います。ボタンをクリックするたびに、クリックされた位置の情報が変更されます(海洋だけではなく、船舶の種類、どのような種類の船ですか?)。このクラスは、どの位置が既にクリックされたかについての情報を保持することもでき、ボタンを無効にして再度クリックすることを避けることができます。 –

0

は私のために働いていたものです。クラスでは、そのクラスでクラスを呼び出すメソッドを使用しました。

public void placeShipAt(int row, int column, Ocean ocean) { 

     ocean.buttons[row][column].setText("B"); 
} 
関連する問題