gpt4 book ai didi

java - TicTacToe : text expansion relative to display expansion? 更改监听器?

转载 作者:行者123 更新时间:2023-12-02 07:05:34 26 4
gpt4 key购买 nike

我为一个项目创建了一个基于 Java 的 tic-tac-toe 游戏。该游戏有一个带有 Jbutton 的 Jframe 显示,可通过事件监听器响应鼠标点击。 GUI 显示可通过拖动边缘来扩展。但是,XO 的文本大小保持固定。我正在尝试查找可以与我的文本关联的事件或更改监听器或类似的代码类型 - 当用户展开和折叠显示框时,根据显示的大小展开和折叠文本大小。

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

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
}

最佳答案

  • 猜测:您正在使用 null 布局和 setBounds(...) 来定位组件。
  • 解决方案:不要。使用布局管理器,一切都将得到解决。使用容器的 GridLayout(3, 3) 可以很好地容纳 3 x 3 的 JButton 网格,并且允许按钮相对于容器重新调整大小。
  • 如果您想在调整按钮大小时更改文本的字体大小,请考虑向保存文本的 JButton 添加 ComponentListener,并在 componentResized(...) 方法中更改按钮字体大小。您可能需要使用 FontMetrics 类才能使其正常工作。

关于java - TicTacToe : text expansion relative to display expansion? 更改监听器?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16158256/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com