如何将按钮更改为图像? [英] How can I change the button into the image?

查看:65
本文介绍了如何将按钮更改为图像?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class TicTacToe implements ActionListener
{
//Instance Variables
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}, //virticle wins
{0, 4, 8}, {2, 4, 6} //diagonal wins
};
private JFrame window = new JFrame("Tic-Tac-Toe");
private JButton buttons[] = new JButton[9];
private int count = 0;
private String letter = "";
private boolean win = false;
private static int startCount =0;
JMenuBar menu = new JMenuBar();
JMenuItem newGame = new JMenuItem("New Game"),
instr = new JMenuItem("Instructions"),
exit = new JMenuItem("Exit"),
name = new JMenuItem("Change Name");
static String x = "X";
static String y = "Y";

public TicTacToe()
{
//Create Window
window.setSize(370,370);
window.setLocationRelativeTo(null);
window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
window.setLayout(new GridLayout(3,3));

//Add Buttons To The Window
for(int i=0; i<=8; i++)
{
buttons[i] = new JButton();
window.add(buttons[i]);
buttons[i].addActionListener(this);
}

menu.add(newGame);
menu.add(name);
menu.add(instr);
menu.add(exit);

name.addActionListener(this);
newGame.addActionListener(this);
exit.addActionListener(this);
instr.addActionListener(this);
window.setJMenuBar(menu);
//Make The Window Visible
window.setVisible(true);
}

public void setName()
{
x = JOptionPane.showInputDialog(null, "Enter Name of player X: ", "", 1);
y = JOptionPane.showInputDialog(null, "Enter Name of player O: ", "", 1);
if(x==null)
{
x = "X";
}
if(y==null)
{
y = "O";
}
if(x.length()==0)
{
x = "X";
}
if(y.length()==0)
{
y = "O";
}
JOptionPane.showMessageDialog(null, "Your names have been set\nTo change your names click on the Change name button in the menu bar","Name Changed!!!",JOptionPane.INFORMATION_MESSAGE);
}

/**
When an object is clicked, perform an action.
@param a action event object
*/
public void actionPerformed(ActionEvent a)
{
Object source = a.getSource();
//System.out.println(str1+"\t"+str2);

if(source == newGame)
{
int answer = JOptionPane.showConfirmDialog(null, "Your current game will not be saved...\nContinue Anyways??", "Do you want to start a new game?", JOptionPane.YES_NO_OPTION);

if (answer == JOptionPane.YES_OPTION)
{
this.clearIt();
}

}
else if(source == name)
{
this.setName();

}
else if(source == instr)
{
JOptionPane.showMessageDialog(null, "Your goal is to be the first player to get 3 X's or O's in a row. (horizontally, diagonally, or vertically)","Instructions",JOptionPane.INFORMATION_MESSAGE);
}
else if(source == exit)
{
int answer = JOptionPane.showConfirmDialog(null, "EXIT", "Are You sure you want to exit??", JOptionPane.YES_NO_OPTION);

if (answer == JOptionPane.YES_OPTION)
{
JOptionPane.showMessageDialog(null, "Thank you " + x+ " and " + y + " for playing");
System.exit(0);
}
}
else
{
count++;

/*Calculate whose turn it is*/
if(count % 2 == 0)
{
letter = "O";
}
else
{
letter = "X";
}

/*Write the letter to the button and deactivate it*/
JButton pressedButton = (JButton)source;
pressedButton.setText(letter);
pressedButton.setEnabled(false);

/*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;
}
}

/*Show a dialog when game is over*/
if(win == true)
{
if(letter.equals("X"))
letter = x;
else
letter = y;
JOptionPane.showMessageDialog(null, letter + " wins the game!");
int answer = JOptionPane.showConfirmDialog(null, "Start", "Do you want to start a new game", JOptionPane.YES_NO_OPTION);

if (answer == JOptionPane.YES_OPTION)
{
this.clearIt();
}
else
{
JOptionPane.showMessageDialog(null, "Thank you " + x+ " and " + y + " for playing");
System.exit(0);
}

}
else if(count == 9 && win == false)
{
JOptionPane.showMessageDialog(null, "The game was tie!");
int answer = JOptionPane.showConfirmDialog(null, "Start", "Do you want to start a new game", JOptionPane.YES_NO_OPTION);
if (answer == JOptionPane.YES_OPTION)
{
this.clearIt();
}
else
{
JOptionPane.showMessageDialog(null, "Thank you " + x+ " and " + y + " for playing");
System.exit(0);
}

}
}
}

public void clearIt()
{

window.setVisible(false);
//this.window = null;
this.startIt();
}

public void startIt()
{
new TicTacToe();

}

public static void main(String[] args)
{

TicTacToe starter = new TicTacToe();
starter.setName();
}
}



添加了代码块[/编辑]



Code block added[/Edit]

推荐答案

您需要创建JLabel而不是JButton.然后将相应的图像添加到JLabel. JLabel/Image上的鼠标侦听器将捕获单击它的鼠标事件.

如何使用图标 [如何编写鼠标侦听器 [
Instead of JButtons you need to create JLabels. Then add the corresponding image to the JLabel. A mouse Listener on the JLabel/Image will capture the mouse event of clicking on it.

How to use Icons[^]

How to Write a Mouse Listener[^]

A bit more OOP would help though.


这篇关于如何将按钮更改为图像?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆