2013-04-22 2 views
0

프로젝트 용 Java 기반의 tic-tac-toe 게임을 만들었습니다. 게임에는 이벤트 리스너를 통해 마우스 클릭에 응답하는 Jbutton의 Jframe 디스플레이가 있습니다. GUI 디스플레이는 가장자리를 드래그하여 확장 할 수 있습니다. 그러나 X의 & O의 텍스트 크기는 고정되어 있습니다. 텍스트를 묶을 수있는 이벤트 또는 변경 리스너 또는 유사한 코드 유형을 찾으려고합니다. 사용자가 확장하여 디스플레이 상자를 접을 때 디스플레이 크기에 따라 텍스트 크기를 확장하거나 축소합니다.TicTacToe : 디스플레이 확장에 상대적인 텍스트 확장? 청취자를 변경 하시겠습니까?

package mytictactoe; 

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

import javax.swing.AbstractButton; 
import javax.swing.JButton; 
import javax.swing.JFrame; 
import javax.swing.JOptionPane; 


public class TicTacToeV1 implements ActionListener 
{ 
    /*Create winning combinations instance variable*/ 
    private int[][] winCombinations = new int[][] 
    { 
      {0, 1, 2}, {3, 4, 5}, {6, 7, 8}, //horizontal wins 
      {0, 3, 6}, {1, 4, 7}, {2, 5, 8}, //vertical wins 
      {0, 4, 8}, {2, 4, 6}    //diagonal wins 
    };//end winCombinations 

    //Create the rest of the instance variables 
    private String letter = ""; 
    private JFrame window = new JFrame("Tic-Tac-Toe "); 
    private JButton buttons[] = new JButton[9]; 
    private int count = 0; 
    private boolean win = false; 


    //Create Window Display 
    public TicTacToeV1() 
    { 
    window.setSize(300,300); 
    window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
    window.setLayout(new GridLayout(3,3)); 

    //MAC OS Specific Translucency Setting 
    window.getRootPane().putClientProperty("Window.alpha", new Float(0.95f)); 


    //Set Window display color 
    window.setBackground(Color.YELLOW); 

    //Center display on screen 
    window.setLocationRelativeTo(null); 
    window.setVisible(true); 






    //install buttons in window with a mouse click-event listener 
    for(int i=0; i<=8; i++){ 
     buttons[i] = new JButton(); 
     window.add(buttons[i]); 
     buttons[i].addActionListener(this); 

    }// End Window Display creation 

    //Make Game Window visible 
    window.setVisible(true); 
}//End TicTacToeV1 Board and Listener 




    //Solicit user input 


    /** 
    When an object is clicked, perform an action. 
    @param a action event object 
    */ 
    public void actionPerformed(ActionEvent a) 
    { 
     count++; 

     //Determine X or O turn 
     if(count % 2 == 0) 
     { 
      letter = "<html><font color = green>O</font></html>"; 
     }//End O turn 
     else 
     { 
      letter = "<html><font color = blue>X</font></html>"; 
     }//End X turn 


     //set font of X & O, when a button has been played, disable button from further use 

     Font font = new Font("Times New Roman",Font.BOLD, 50); 
     final JButton pressedButton = (JButton)a.getSource();//determine button selected 
     pressedButton.setFont(font); 
     pressedButton.setText(letter); 

     pressedButton.setEnabled(false);//disable selected buttons from further use 




     pressedButton.addActionListener(new ActionListener() //want expansion/drag listener - not action listener 
     {//if display box increasing: && if display box decreasing... limit box size expand & shrink 
      //button & font relative resizing 
      int size = 50; 

      public void actionPerformed(ActionEvent ev) 
      { 

      pressedButton.setFont(new Font("Times New Roman",Font.BOLD, ++size)); 

      }  
     } 
     ); 



     //Determine who won 
     for(int i=0; i<=7; i++) 
     { 
      if(buttons[winCombinations[i][0]].getText().equals(buttons[winCombinations[i][1]].getText()) && 
       buttons[winCombinations[i][1]].getText().equals(buttons[winCombinations[i][2]].getText()) && 
       buttons[winCombinations[i][0]].getText() != "") 
      { win = true;} 
     }//End For loop for win determination 



     //Show a dialog when game is over 

     if(win == true) 
     { 
      if(count % 2 == 0) 
      { 
      char lineWinner = letter.charAt(letter.indexOf("O"));//necessary for lineWinner, which replaces HTML 'letter' variable 
      JOptionPane.showMessageDialog(null, lineWinner + " wins the game!");//lineWinner was 'letter' 
      System.exit(0); 
      }//End O winner 
      else 
      { 
      char lineWinner = letter.charAt(letter.indexOf("X"));//necessary for lineWinner, which replaces HTML 'letter' variable 
      JOptionPane.showMessageDialog(null, lineWinner + " wins the game!");//lineWinner was letter 
      System.exit(0); 
      }//End X winner 
     }//End Win dialog 

     //If game is a draw 
      else if(count == 9 && win == false) 
      { 
      JOptionPane.showMessageDialog(null, "The game was tie!"); 
      System.exit(0); 
      }//End Draw dialog   
    }//End ActionPerformed 


    public static void main(String[] args) 
    { 
     DynamicFont re = new DynamicFont(); 
     re.setVisible(true); 

     TicTacToeV1 starter = new TicTacToeV1(); 
    }//End main, which calls TicTacToeV1 method 
} 
+0

코드를 추가했습니다. 그 실수에 대해 죄송합니다. 나는 결코 전에 질문을 게시했습니다 ... – user2309191

답변

2
  • 추측 : 여기

    내가 지금까지 완료 코드입니다 당신이 당신의 구성 요소의 위치를 ​​널 (null) 레이아웃과 setBounds(...)을 사용하고 있습니다.
  • 해결책 :하지 마십시오. 레이아웃 관리자를 사용하면 모든 것이 해결됩니다. 컨테이너를 사용하는 GridLayout (3, 3)은 JButtons의 3x3 그리드를 유지하는 데 효과적 일 수 있으며 컨테이너와 관련하여 버튼의 크기를 조정할 수 있습니다.
  • 버튼의 크기를 조정할 때 텍스트의 글꼴 크기를 변경하려면 componentResized(...) 메서드에서 텍스트를 보유하고 버튼 글꼴의 크기를 변경하는 JButton에 ComponentListener를 추가하는 것이 좋습니다. 이 작업을하려면 FontMetrics 클래스를 사용해야합니다.
+0

검토를 위해 코드를 추가했습니다. 죄송합니다. 나는 약간의 자바 놈입니다. – user2309191

+0

@ user2309191 : 위에 추가 된 마지막 글 머리 기호를보십시오. 이것이 당신이 사용하려고 시도해야하는 것입니다. –