2017-03-02 7 views
1

現在、TicTacToeゲームを作成しようとすると、フレームと9つのボタンが設定されています(プレイヤーが選択できる各四角形ごとに)。クリックしたときに各ボタンに機能を追加するのに少し問題があります。クリックしたときに画像をJButtonに置き換える方法

package OX; 

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

public class OX{ 

public static void main(String[] args){ 
    JFrame frame = new JFrame("OX"); 
    frame.setSize(800,800); 
    frame.setVisible(true); 
    frame.getContentPane().setBackground(Color.BLACK); 
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
    JPanel panel = new JPanel(new GridBagLayout()); 
    frame.add(panel); 
    GridBagConstraints c = new GridBagConstraints(); 
    JButton button1 = new JButton("1"); 
    button1.setPreferredSize(new Dimension(100,100)); 
    c.gridx = 1; 
    c.gridy = 1; 
    c.insets = new Insets(50,50,50,50);  
    panel.add(button1, c); 
    JButton button2 = new JButton("2"); 
    button2.setPreferredSize(new Dimension(100,100)); 
    c.gridx = 2; 
    c.gridy = 1; 
    panel.add(button2, c); 
    JButton button3 = new JButton("3"); 
    button3.setPreferredSize(new Dimension(100,100)); 
    c.gridx = 3; 
    c.gridy = 1; 
    panel.add(button3, c); 
    JButton button4 = new JButton("4"); 
    button4.setPreferredSize(new Dimension(100,100)); 
    c.gridx = 1; 
    c.gridy = 2; 
    panel.add(button4, c); 
    JButton button5 = new JButton("5"); 
    button5.setPreferredSize(new Dimension(100,100)); 
    c.gridx = 2; 
    c.gridy = 2; 
    panel.add(button5, c); 
    JButton button6 = new JButton("6"); 
    button6.setPreferredSize(new Dimension(100,100)); 
    c.gridx = 3; 
    c.gridy = 2; 
    panel.add(button6, c); 
    JButton button7 = new JButton("7"); 
    button7.setPreferredSize(new Dimension(100,100)); 
    c.gridx = 1; 
    c.gridy = 3; 
    panel.add(button7, c); 
    JButton button8 = new JButton("8"); 
    button8.setPreferredSize(new Dimension(100,100)); 
    c.gridx = 2; 
    c.gridy = 3; 
    panel.add(button8, c); 
    JButton button9 = new JButton("9"); 
    button9.setPreferredSize(new Dimension(100,100)); 
    c.gridx = 3; 
    c.gridy = 3; 
    panel.add(button9, c);  
    } 
} 

私はさまざまなアイデアを試しましたが、ちょうどそれが正しいと思われます。

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

答えて

1

最初にコードはモジュール化されていません。私はあなたが適切なクラスでそれをモジュール化することをお勧めします

スウィングの重要な受諾はリスナークラスです。ボタンの機能を記述する。 ActionListenerを実装してこのクラスを構築する必要があります すべてのゲームロジックがこのListenerクラスで実装されます。

このリスナーからのゲームステータス(勝者/敗者)を確認する別の方法があります。これにより、作業が簡単になります。

参照してください - https://docs.oracle.com/javase/8/docs/api/java/awt/event/ActionListener.html http://www.codejava.net/java-core/the-java-language/java-8-lambda-listener-example

は、この情報がお役に立てば幸いです。乾杯!

+0

iはActionListenerを実装してきましたし、すべてがうまくようで、しかし、私はバグを作成していると私は持っている、返信いただきありがとうござい各ボタンにActionListenerを追加どのように、なぜそれが私がプログラムを立ち上げるときに起こるのかわからない。プログラムを起動するたびに、パネル上に配置されているボタンがランダムに選択されます。これは、一度にすべてが実行されているためで、ボタンを配置する順序がランダムに選択されている可能性がありますか? – user7431342

+0

申し訳ありませんが、コードを見ずにコメントすることはできません。コード –

+0

を投稿してください。 – user7431342

1
package OX; 

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

public class OX{ 

public static void main(String[] args){ 
    JFrame frame = new JFrame("OX"); 
    frame.setSize(800,800); 
    frame.setVisible(true); 
    frame.getContentPane().setBackground(Color.BLACK); 
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
    JPanel panel = new JPanel(new GridBagLayout()); 
    frame.add(panel); 
    GridBagConstraints c = new GridBagConstraints();   
    JButton button1 = new JButton("1"); 
    button1.setPreferredSize(new Dimension(100,100)); 
    c.gridx = 1; 
    c.gridy = 1; 
    c.insets = new Insets(50,50,50,50); 
    button1.addActionListener(new ActionListener(){ 
     public void actionPerformed(ActionEvent e){ 
      System.out.println("test"); 
      } 
     }); 
    panel.add(button1, c);  
    JButton button2 = new JButton("2"); 
    button2.setPreferredSize(new Dimension(100,100)); 
    c.gridx = 2; 
    c.gridy = 1; 
    button2.addActionListener(new ActionListener(){ 
     public void actionPerformed(ActionEvent e){ 
      System.out.println("test2"); 
      } 
     }); 
    panel.add(button2, c);  
    JButton button3 = new JButton("3"); 
    button3.setPreferredSize(new Dimension(100,100)); 
    c.gridx = 3; 
    c.gridy = 1; 
    button3.addActionListener(new ActionListener(){ 
     public void actionPerformed(ActionEvent e){ 
      System.out.println("test3"); 
      } 
     }); 
    panel.add(button3, c);  
    JButton button4 = new JButton("4"); 
    button4.setPreferredSize(new Dimension(100,100)); 
    c.gridx = 1; 
    c.gridy = 2; 
    button4.addActionListener(new ActionListener(){ 
     public void actionPerformed(ActionEvent e){ 
      System.out.println("test4"); 
      } 
     }); 
    panel.add(button4, c);  
    JButton button5 = new JButton("5"); 
    button5.setPreferredSize(new Dimension(100,100)); 
    c.gridx = 2; 
    c.gridy = 2; 
    button5.addActionListener(new ActionListener(){ 
     public void actionPerformed(ActionEvent e){ 
      System.out.println("test5"); 
      } 
     }); 
    panel.add(button5, c);  
    JButton button6 = new JButton("6"); 
    button6.setPreferredSize(new Dimension(100,100)); 
    c.gridx = 3; 
    c.gridy = 2; 
    button6.addActionListener(new ActionListener(){ 
     public void actionPerformed(ActionEvent e){ 
      System.out.println("test6"); 
      } 
     }); 
    panel.add(button6, c);  
    JButton button7 = new JButton("7"); 
    button7.setPreferredSize(new Dimension(100,100)); 
    c.gridx = 1; 
    c.gridy = 3; 
    button7.addActionListener(new ActionListener(){ 
     public void actionPerformed(ActionEvent e){ 
      System.out.println("test7"); 
      } 
     }); 
    panel.add(button7, c); 
    JButton button8 = new JButton("8"); 
    button8.setPreferredSize(new Dimension(100,100)); 
    c.gridx = 2; 
    c.gridy = 3; 
    panel.add(button8, c); 
    button8.addActionListener(new ActionListener(){ 
     public void actionPerformed(ActionEvent e){ 
      System.out.println("test8"); 
      } 
     });  
    JButton button9 = new JButton("9"); 
    button9.setPreferredSize(new Dimension(100,100)); 
    c.gridx = 3; 
    c.gridy = 3; 
    panel.add(button9, c);  
    button9.addActionListener(new ActionListener(){ 
     public void actionPerformed(ActionEvent e){ 
      System.out.println("test9"); 
      } 
     }); 

    } 
} 

@Vijayan可児

私は