0

我为一个项目创建了一个基于 Java 的井字游戏。游戏有一个带有Jbutton's 的 Jframe 显示,通过事件监听器响应鼠标点击。GUI 显示可通过拖动边缘进行扩展。但是,X's & O's 的文本大小保持不变。我正在尝试查找可以与我的文本相关联的事件或更改侦听器或类似代码类型 - 当用户展开和折叠显示框时,根据显示的大小展开和折叠文本大小。

这是我迄今为止完成的代码:

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
}
4

1 回答 1

2
  • 一个猜测:您正在使用空布局并setBounds(...)定位您的组件。
  • 解决方案:不要。使用布局管理器,一切都会迎刃而解。使用容器的 GridLayout(3, 3) 可以很好地保存 3 x 3 的 JButton 网格,并允许按钮相对于容器重新调整大小。
  • 如果要在按钮调整大小时更改文本的字体大小,请考虑将 ComponentListener 添加到保存文本的 JButton 并在componentResized(...)方法中更改按钮字体的大小。您可能需要使用 FontMetrics 类才能使其正常工作。
于 2013-04-22T23:12:15.273 回答