import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class CSE141HW5 implements ActionListener
{
public static void main (String[] args)
{
new CSE141HW5();
}
/* Instance Variables */
private JFrame gameWindow = new JFrame ("Tic-Tac-Toe");
private int [][] winningCombinations = new 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}};
private JButton buttons[] = new JButton [9];
private int count = 0;
private String mark = "";
private boolean win = false;
public CSE141HW5()
{
/* Creating gameWindow */
Container con = gameWindow.getContentPane();
con.setBackground(Color.WHITE);
gameWindow.setVisible (true);
gameWindow.setSize (220,220);
gameWindow.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
gameWindow.setLayout (new GridLayout (3, 3));
gameWindow.setLocation (830, 400);
gameWindow.setBackground(Color.WHITE);
/* Adding Buttons */
for (int i = 0; i <= 8; i++)
{
buttons[i] = new JButton ();
gameWindow.add (buttons [i]);
buttons[i].addActionListener (this);
buttons[i].setBackground(Color.WHITE);
}
}
/* When an object clicked... */
public void actionPerformed (ActionEvent click)
{
JButton pressedButton = (JButton)click.getSource();
/* Whose turn? */
if ((count % 2) == 0)
{
mark = "O";
pressedButton.setBackground(Color.CYAN);
}
else
{
mark = "X";
pressedButton.setBackground(Color.yellow);
}
/* Write the letter to the button and deactivate it */
pressedButton.setText (mark);
pressedButton.setEnabled (false);
/* Determining that who won */
for (int i = 0; i <= 7; i++)
{
if (buttons[winningCombinations[i][0]].getText().equals(buttons[winningCombinations[i][1]].getText())
&& buttons[winningCombinations[i][1]].getText().equals(buttons[winningCombinations[i][2]].getText())
&& buttons[winningCombinations[i][0]].getText() != "") win = true;
}
/* Ending Dialog */
if (win == true)
{
byte response = (byte) (JOptionPane.showConfirmDialog(null, mark + " won!\nPlay again?", "Homework 5", JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE));
if (response == 0) new CSE141HW5();
else System.exit(0);
}
else if (count == 8 && win == false)
{
byte response = (byte) (JOptionPane.showConfirmDialog(null, "Draw!\nPlay again?", "Homework 5", JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE));
if (response == 0) new CSE141HW5();
else System.exit(0);
}
count++;
}
}
我是编程新手,用 Java 编写了一个井字游戏。我想改进这个程序,但有些事情我无法处理。
我已将按钮颜色更改为默认颜色,例如 Color.yellow 等。如何使用更详细的颜色?
当游戏结束时,程序会要求重新玩。如果用户选择是,则会出现新的游戏窗口,但旧窗口仍然存在,这是我不喜欢的。我希望关闭旧窗口,但找不到实现方法。
如果您在我的程序中发现任何您认为不必要的代码,或者如果您认为有比我所做的更好的方法,请告诉我。这样我就可以学习了。
最佳答案
I have changed buttons colors with default colors, like Color.yellow etc. How can i use more detailed color?
您可以使用自己的按钮,比方说public class XButton extends JButton
。为了更好地自定义您的按钮,请覆盖方法 public void paintComponent(Graphics g)
。然后你可以使用 fillRect
用标准颜色填充按钮的矩形 OR 将对象 Graphics g
转换为 Graphics2D
,然后设置对象 GradientPaint()
,它用线性颜色渐变填充形状。
public class XButton extends JButton {
public XButton() {
super();
}
public void paintComponent(Graphics g){
Graphics2D g2d = (Graphics2D)g;
GradientPaint gp = new GradientPaint(0, 0, Color.RED, 30, 30, Color.cyan, true);
g2d.setPaint(gp);
g2d.fillRect(0, 0, this.getWidth(), this.getHeight());
}
}
结果如下图:
PS:在添加按钮时,将 JButton 替换为 Xbutton 即可。
When game overs, program asks for re-play. If user selects Yes, then new game window appears, but old window still remains, which i didn't like. I want that old windows to be closed, but couldn't find how to implement it.
您只需在 JFrame 对象 gameWindow
上使用方法 dispose()
,因此您的代码中有两个地方您应该放置:
if (response == 0){
gameWindow.dispose();
new CSE141HW5();
}
有关How to programmatically close a JFrame的更多详细信息.
If you find any code on my program, which you think unnecessary or if you think that there is a better way than what i do, please tell me. So i can learn.
嗯,你的代码很好。如果你想改进它。 StackOverflow 不是这样做的正确地方。你最好检查一下:codereview.stackexchange.com
希望我回答了你的问题。
关于java - 新手的几个问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8448337/