Java TictaToe:相对于显示扩展的文本扩展?改变听众?
我已经为一个项目创建了一个基于Java的tic-tac-toe游戏。游戏有一个Jframe显示屏,带有Java TictaToe:相对于显示扩展的文本扩展?改变听众?,java,swing,jframe,actionlistener,changelistener,Java,Swing,Jframe,Actionlistener,Changelistener,我已经为一个项目创建了一个基于Java的tic-tac-toe游戏。游戏有一个Jframe显示屏,带有Jbutton,通过事件监听器响应鼠标点击。GUI显示可通过拖动边缘进行扩展。但是,X和O的文本大小保持不变。我试图找到一个事件或更改侦听器或类似的代码类型,它们可以绑定到我的文本-当用户展开和折叠显示框时,根据显示的大小展开和折叠文本大小 以下是我迄今为止完成的代码: package mytictactoe; import java.awt.Color; import java.awt.Fo
Jbutton
,通过事件监听器响应鼠标点击。GUI显示可通过拖动边缘进行扩展。但是,X
和O
的文本大小保持不变。我试图找到一个事件或更改侦听器或类似的代码类型,它们可以绑定到我的文本-当用户展开和折叠显示框时,根据显示的大小展开和折叠文本大小
以下是我迄今为止完成的代码:
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
}
包装桃金娘;
导入java.awt.Color;
导入java.awt.Font;
导入java.awt.GridLayout;
导入java.awt.event.ActionEvent;
导入java.awt.event.ActionListener;
导入javax.swing.AbstractButton;
导入javax.swing.JButton;
导入javax.swing.JFrame;
导入javax.swing.JOptionPane;
公共类TictoEV1实现ActionListener
{
/*创建一个实例变量*/
私有int[][]winCombinations=新int[][]
{
{0,1,2},{3,4,5},{6,7,8},//横向获胜
{0,3,6},{1,4,7},{2,5,8},//垂直获胜
{0,4,8},{2,4,6}//对角赢
};//结束WinCombination
//创建其余的实例变量
私人字符串字母=”;
专用JFrame窗口=新JFrame(“Tic Tac Toe”);
私有JButton按钮[]=新JButton[9];
私有整数计数=0;
私有布尔赢=假;
//创建窗口显示
公共交通1(
{
设置窗口大小(300300);
window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLayout(新的GridLayout(3,3));
//MAC OS特定的半透明设置
window.getRootPane().putClientProperty(“window.alpha”,新浮点(0.95f));
//设置窗口显示颜色
窗户。背景(颜色。黄色);
//屏幕中央显示
window.setLocationRelativeTo(空);
window.setVisible(true);
//使用鼠标单击事件侦听器在窗口中安装按钮
对于(int i=0;i
- 猜测:您正在使用null布局和
setBounds(…)
来定位组件
- 解决方案:不要。使用布局管理器,所有问题都会得到解决。使用容器的GridLayout(3,3)可以很好地容纳3×3的JButton网格,并允许按钮根据容器重新调整大小
<> LI>如果要更改按钮大小的文本大小,请考虑将一个组件监听器添加到保存文本的JButton中,并在组件大小(…)>代码>方法中更改按钮字体的大小。您可能需要使用FutToMeCube类来使其工作。
我添加了代码以供审阅。很抱歉遗漏了它。我有点像JAVA noob…@user2309191:请查看我上面答案中新添加的最后一个项目符号。这是您应该尝试使用的。我已经添加了代码。对于疏忽,我感到抱歉。我以前从未发布过问题。。。