2013-07-17 3 views
1

나는 온라인에서 찾은 다소 오래된 구식 전술 발가락 코드를 업데이트했다. (실제로 작동하는 것처럼 : P) 거의 완성되었지만 동일한 문제가 계속 발생한다. 한 가지를 제외하고는 틱택 막 발가락 보드 안의 3x3 버튼이 완벽하게 작동합니다. 클릭 한 색상은 항상 배경입니다.이 문제를 해결하기위한 시도가 있었지만 버튼을 전혀 변경하지 않는 것이 전부였습니다. 두 가지 색상은 빨간색과 초록색이며 각 플레이어가 버튼을 클릭 할 때 색상이 바뀌기를 바랍니다.Tic Tac Toe 버튼 색상

주요 게임 코드 :

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

public class TicTacToeGame extends JFrame implements ActionListener 
{ 
JLabel l1; 
JLabel p1namel; 
JLabel p2namel; 
JLabel l3; 
JButton b1; 
JButton b2; 
JButton b3; 
JButton b4; 
JButton b5; 
JButton b6; 
JButton b7; 
JButton b8; 
JButton b9; 
int b1ctr = 1 ,b2ctr = 2 ,b3ctr = 3 ,b4ctr = 4, b5ctr = 5, b6ctr = 6, b7ctr = 7, b8ctr = 8, b9ctr = 9; 
Font textfont = new Font("Serif", Font.PLAIN,70); 
int ctr = 1; 
String p1name,p2name; 

public TicTacToeGame(String p1,String p2) 
{ 
    TicTacToeGameLayout customLayout = new TicTacToeGameLayout(); 

    getContentPane().setFont(new Font("Helvetica", Font.PLAIN, 12)); 
    getContentPane().setLayout(customLayout); 
    getContentPane().setBackground(Color.CYAN); 

    l1 = new JLabel(""); 
    getContentPane().add(l1); 

    p1name = p1; 
    p2name = p2; 

    p1namel = new JLabel(""); 
    p1namel.setText(p1); 
    p1namel.setFont(textfont); 
    p1namel.setForeground(Color.RED); 
    getContentPane().add(p1namel); 

    p2namel = new JLabel(""); 
    p2namel.setText(p2); 
    p2namel.setForeground(Color.GREEN); 
    p2namel.setFont(textfont); 
    getContentPane().add(p2namel); 

    l3 = new JLabel("<->"); 
    l3.setFont(textfont); 
    getContentPane().add(l3); 

    b1 = new JButton(""); 
    getContentPane().add(b1); 

    b2 = new JButton(""); 
    getContentPane().add(b2); 

    b3 = new JButton(""); 
    getContentPane().add(b3); 

    b4 = new JButton(""); 
    getContentPane().add(b4); 

    b5 = new JButton(""); 
    getContentPane().add(b5); 

    b6 = new JButton(""); 
    getContentPane().add(b6); 

    b7 = new JButton(""); 
    getContentPane().add(b7); 

    b8 = new JButton(""); 
    getContentPane().add(b8); 

    b9 = new JButton(""); 
    getContentPane().add(b9); 


    l3.setForeground(Color.WHITE); 

    b1.setFont(textfont); 
    b2.setFont(textfont); 
    b3.setFont(textfont); 
    b4.setFont(textfont); 
    b5.setFont(textfont); 
    b6.setFont(textfont); 
    b7.setFont(textfont); 
    b8.setFont(textfont); 
    b9.setFont(textfont); 

    b1.addActionListener(this); 
    b2.addActionListener(this); 
    b3.addActionListener(this); 
    b4.addActionListener(this); 
    b5.addActionListener(this); 
    b6.addActionListener(this); 
    b7.addActionListener(this); 
    b8.addActionListener(this); 
    b9.addActionListener(this); 

    setSize(getPreferredSize()); 

    addWindowListener(new WindowAdapter() 
    { 
     public void windowClosing(WindowEvent e) 
     { 
      System.exit(0); 
     } 
    }); 
} 


public void actionPerformed(ActionEvent ae) 

    { 

    if (ctr % 2 == 1) 

     { 

     if(ae.getSource() == b1) 
      { 


       b1ctr = 0; 
       b1.setEnabled(false); 
       b1.setBackground(Color.GREEN); 
       winnerfind(); 
       ctr++;  

      } 
      else if (ae.getSource() == b2) 

       { 

       b2ctr = 0; 
       b2.setEnabled(false); 
       b2.setBackground(Color.GREEN); 
       winnerfind(); 
       ctr++; 

       } 
       else if (ae.getSource() == b3) 

       { 

       b3ctr = 0; 
       b3.setEnabled(false); 
       b3.setBackground(Color.GREEN); 
       winnerfind(); 
       ctr++; 

       } 
       else if (ae.getSource() == b4) 

       { 

       b4ctr = 0; 
       b4.setEnabled(false); 
       b4.setBackground(Color.GREEN); 
       winnerfind(); 
       ctr++; 

       } 
       else if (ae.getSource() == b5) 

       { 

       b5ctr = 0; 
       b5.setEnabled(false); 
       b5.setBackground(Color.GREEN); 
       winnerfind(); 
       ctr++; 

       } 
       else if (ae.getSource() == b6) 

       { 

       b6ctr = 0; 
       b6.setEnabled(false); 
       b6.setBackground(Color.GREEN); 
       winnerfind(); 
       ctr++; 

       } 
       else if (ae.getSource() == b7) 

       { 

       b7ctr = 0; 
       b7.setEnabled(false); 
       b7.setBackground(Color.GREEN); 
       winnerfind(); 
       ctr++; 

       } 
       else if (ae.getSource() == b8) 

       { 

       b8ctr = 0; 
       b8.setEnabled(false); 
       b8.setBackground(Color.GREEN); 
       winnerfind(); 
       ctr++; 

       } 
       else if (ae.getSource() == b9) 

       { 

       b9ctr = 0; 
       b9.setEnabled(false); 
       b9.setBackground(Color.GREEN); 
       winnerfind(); 
       ctr++; 

       } 

     } 
     else if (ctr % 2 != 1) 

       { 

       if(ae.getSource() == b1)  
        { 


         b1ctr = 1; 
         b1.setEnabled(false); 
         b1.setBackground(Color.YELLOW); 
         winnerfind1(); 
         ctr++;  

        } 
        else if (ae.getSource() == b2) 

         { 

         b2ctr = 1; 
         b2.setEnabled(false); 
         b2.setBackground(Color.YELLOW); 
         winnerfind1(); 
         ctr++; 

         } 
         else if (ae.getSource() == b3) 

         { 

         b3ctr = 1; 
         b3.setEnabled(false); 
         b3.setBackground(Color.YELLOW); 
         winnerfind1(); 
         ctr++; 

         } 
         else if (ae.getSource() == b4) 

         { 

         b4ctr = 1; 
         b4.setEnabled(false); 
         b4.setBackground(Color.YELLOW); 
         winnerfind1(); 
         ctr++; 

         } 
         else if (ae.getSource() == b5) 

         { 

         b5ctr = 1; 
         b5.setEnabled(false); 
         b5.setBackground(Color.YELLOW); 
         winnerfind1(); 
         ctr++; 

         } 
         else if (ae.getSource() == b6) 

         { 

         b6ctr = 1; 
         b6.setEnabled(false); 
         b6.setBackground(Color.YELLOW); 
         winnerfind1(); 
         ctr++; 

         } 
         else if (ae.getSource() == b7) 

         { 

         b7ctr = 1; 
         b7.setEnabled(false); 
         b7.setBackground(Color.YELLOW); 
         winnerfind1(); 
         ctr++; 

         } 
         else if (ae.getSource() == b8) 

         { 

         b8ctr = 1; 
         b8.setEnabled(false); 
         b8.setBackground(Color.YELLOW); 
         winnerfind1(); 
         ctr++; 

         } 
         else if (ae.getSource() == b9) 

         { 

         b9ctr = 1; 
         b9.setEnabled(false); 
         b9.setBackground(Color.YELLOW); 
         winnerfind1(); 
         ctr++; 

         } 




      } 



    } 

    public void winnerfind() 

     { 

     if ((b1ctr == 0 && b2ctr == 0 && b3ctr == 0) || (b1ctr == 0 && b4ctr == 0 && b7ctr == 0) || (b2ctr == 0 && b5ctr == 0 && b8ctr == 0) || (b3ctr == 0 && b6ctr == 0 && b9ctr == 0) || (b4ctr == 0 && b5ctr == 0 && b6ctr == 0) || (b7ctr == 0 && b8ctr == 0 && b9ctr == 0) || (b1ctr == 0 && b5ctr == 0 && b9ctr == 0) || (b3ctr == 0 && b5ctr == 0 && b7ctr == 0)) 

      { 

      JOptionPane.showMessageDialog(null,""+p1name+" is the winner!"); 

      } 



     } 


    public void winnerfind1() 

     { 

     if ((b1ctr == 1 && b2ctr == 1 && b3ctr == 1) || (b1ctr == 1 && b4ctr == 1 && b7ctr == 1) || (b2ctr == 1 && b5ctr == 1 && b8ctr == 1) || (b3ctr == 1 && b6ctr == 1 && b9ctr == 1) || (b4ctr == 1 && b5ctr == 1 && b6ctr == 1) || (b7ctr == 1 && b8ctr == 1 && b9ctr == 1) || (b1ctr == 1 && b5ctr == 1 && b9ctr == 1) || (b3ctr == 1 && b5ctr == 1 && b7ctr == 1)) 

      { 

      JOptionPane.showMessageDialog(null,""+p2name+" is victorious!");  

      } 



     } 


} 

class TicTacToeGameLayout implements LayoutManager 
{ 

public TicTacToeGameLayout() 
{ 
} 

public void addLayoutComponent(String name, Component comp) 
{ 
} 

public void removeLayoutComponent(Component comp) 
{ 
} 

public Dimension preferredLayoutSize(Container parent) 
{ 
    Dimension dim = new Dimension(0, 0); 

    Insets insets = parent.getInsets(); 
    dim.width = 719 + insets.left + insets.right; 
    dim.height = 455 + insets.top + insets.bottom; 

    return dim; 
} 

public Dimension minimumLayoutSize(Container parent) 
{ 
    Dimension dim = new Dimension(0, 0); 
    return dim; 
} 

public void layoutContainer(Container parent) 
{ 
    Insets insets = parent.getInsets(); 

    Component c; 
    c = parent.getComponent(0); 
    if (c.isVisible()) {c.setBounds(insets.left+0,insets.top+64,720,8);} 
    c = parent.getComponent(1); 
    if (c.isVisible()) {c.setBounds(insets.left+16,insets.top+8,428,80);} 
    c = parent.getComponent(2); 
    if (c.isVisible()) {c.setBounds(insets.left+472,insets.top+8,528,80);} 
    c = parent.getComponent(3); 
    if (c.isVisible()) {c.setBounds(insets.left+300,insets.top+8,150,80);} 
    c = parent.getComponent(4); 
    if (c.isVisible()) {c.setBounds(insets.left+176,insets.top+112,120,104);} 
    c = parent.getComponent(5); 
    if (c.isVisible()) {c.setBounds(insets.left+296,insets.top+112,112,104);} 
    c = parent.getComponent(6); 
    if (c.isVisible()) {c.setBounds(insets.left+408,insets.top+112,120,104);} 
    c = parent.getComponent(7); 
    if (c.isVisible()) {c.setBounds(insets.left+176,insets.top+216,120,96);} 
    c = parent.getComponent(8); 
    if (c.isVisible()) {c.setBounds(insets.left+296,insets.top+216,112,96);} 
    c = parent.getComponent(9); 
    if (c.isVisible()) {c.setBounds(insets.left+408,insets.top+216,120,96);} 
    c = parent.getComponent(10); 
    if (c.isVisible()) {c.setBounds(insets.left+176,insets.top+312,120,104);} 
    c = parent.getComponent(11); 
    if (c.isVisible()) {c.setBounds(insets.left+296,insets.top+312,112,104);} 
    c = parent.getComponent(12); 
    if (c.isVisible()) {c.setBounds(insets.left+408,insets.top+312,120,104);} 
    } 
} 
+0

이이 꺼져 : 위와 같이 변경하여

enter image description here

, 당신이와 끝까지 -topic,하지만 당신은 복사 - 붙여 넣기 코드를 줄이기 위해 새로운 메소드로 그 거대한'if' 체인을 리펙토링하려고 할 수 있습니다. –

+2

밥, 바보 같은 질문은 여기에 없습니다 (글쎄, 거기 있지만, 이것들 중 하나가 아닙니다). 프로그래밍을 배우려는 귀하의 시도에 박수를 보냅니다. 우리가 당신을 도울 수 있는지 보자. – paxdiablo

+0

알다시피,이 기능이 작동되면 가장 확실하게 알게 될 것입니다. 더 많은 에너지를 가지고있는 또 다른 날을위한 싸움입니다. 나는 초심자이기 때문에 좀 더 길게 xD 걸립니다. (첫 번째 코멘트에서 연출) – bob138

답변

4

당신은 빨간색 대신 노란색 사용하고 있다는 사실이 아닌 그 코드 정말 아무 문제가 없다 - 당신이 변경하려는 경우, 단지의 모든 발생을 변경 일관성에 대한 두 선수에 대한 Color.RED-Color.YELLOW 및 교환 색상 :

p1namel = new JLabel(""); 
p1namel.setText(p1); 
p1namel.setFont(textfont); 
p1namel.setForeground(Color.GREEN); // << HERE 
getContentPane().add(p1namel); 

p2namel = new JLabel(""); 
p2namel.setText(p2); 
p2namel.setForeground(Color.RED); // << AND HERE 
p2namel.setFont(textfont); 
getContentPane().add(p2namel); 

I 프레임 실행 얻을 그것의 바닥에 약간의 지원 코드를 추가 할 때 :

public static void main(String[] args) { 
    java.awt.EventQueue.invokeLater(new Runnable() { 
     public void run() { 
      TicTacToeGame frame = new TicTacToeGame("pax","diablo"); 
      frame.setVisible(true); 
     } 
    }); 
} 
는 이 (일치하지 않는 색 이외) 잘 작동하는 표시

:

enter image description here

+0

이것이 내가 본 것입니다 : 플레이어 빨간색 -> 버튼 녹색; 선수 녹색 -> 노란색 버튼. (단순히 빨간색 대신 황색을 사용하는 것이 아닙니다.) – johnchen902

+0

@ johnchen902, 업데이트를 참조하십시오 : 가장 쉬운 해결책은 모든 노란색을 빨간색으로 변경하고 플레이어 이름 색상을 그림과 같이 바꿀 수 있습니다. – paxdiablo

+0

버튼의 색상을 유지하고 플레이어의 색상을 노란색과 녹색으로 변경하는 것이 어떻습니까? :) – johnchen902