import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.border.*;
import java.util.Random;
/**
*
*/
public class GUI
{
private static final int BTN_MAX = 8;
private JFrame frame;
private JPanel panel;
private JPanel scores;
private JButton[] buttons;
private ImageIcon[] icons;
private Random rand;
private ImageIcon beach;
private ImageIcon lips;
private ImageIcon discoball;
private ImageIcon flowers;
private ImageIcon blank;
/**
*
*/
public GUI()
{
beach = new ImageIcon("beach.jpg");
lips = new ImageIcon("lips.jpg");
discoball = new ImageIcon ("discoball.jpg");
flowers = new ImageIcon ("flowers.jpg");
blank = new ImageIcon ("blank.jpg");
buttons = makeButtons();
rand = new Random();
startingCondition();
icons = new ImageIcon[] { beach, lips, discoball, lips, beach, flowers, discoball, flowers};
makeFrame();
makeMenuBar(frame);
frame.pack();
frame.setVisible(true);
}
/**
* Makes the frame for the gui, inclusive of adding all components.
*/
private void makeFrame()
{
int horizGap = 25; // Using this for spaces between the grid layout components
int vertGap = 25; // Using this for the spaces between the grid layout componeents
frame = new JFrame("Noughts and Crosses");
Container contentPane = frame.getContentPane();
contentPane.setLayout(new GridLayout(4,2));//set layout of frame to BorderLayout
for (int i = 0; i < BTN_MAX; i++){
contentPane.add(buttons[i]);
}
}
/**
*
*/
private void makeMenuBar(JFrame frame)
{
JMenuBar menubar = new JMenuBar();
frame.setJMenuBar(menubar);
JMenu menu;
JMenuItem item;
menu = new JMenu("File");
menubar.add(menu);
item = new JMenuItem("Reset Entire Game");
item.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
}
});
menu.add(item);
item = new JMenuItem("Reset This Game");
item.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
}
});
menu.add(item);
item = new JMenuItem("Quit");
item.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
}
});
menu.add(item);
frame.addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{
}
});
menu = new JMenu("About");
menubar.add(menu);
item = new JMenuItem("About The Game");
item.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
}
});
menu.add(item);
}
/**
*
*/
private JButton[] makeButtons()
{
final JButton button[] = new JButton[8];
for (int i = 0; i < BTN_MAX; i++)
{
button[i] = new JButton("");
button[i].setPreferredSize(new Dimension(200, 100));
final int tmp = i;
button[i].addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
checkForPair();
takeGo();
}
});
}
return button;
}
/**
*
*/
private void startingCondition()
{
for (int i = 0; i < 8; i++){
buttons[i].setIcon(blank);
}
}
/**
*
*/
public int returnButtonNumber()
{
return 0;
// need to tell which button in the array has been clicked
//so i can send a value to the takeGo method
//appropriatley
}
public ImageIcon getIcon(int number)
{
return icons[number];
}
/**
*
*/
public boolean checkForPair()
{
return false;
}
/**
*
*/
public void takeGo()
{
int i = returnButtonNumber();
buttons[i].setIcon(getIcon(i));
}
}
嗨,我只是想知道是否有人可以帮助我确定按下哪个按钮编号的方法,然后我可以将此值发送到另一种方法以显示适当的图像,欢迎所有想法,尽管可以你请尽量避免完整的代码,因为我想尝试自己做,例子会很好的
谢谢
答案 0 :(得分:3)
与您尽可能少地提供信息; - )...尝试查看JButton.setActionCommand()
此外,如果您要为每个按钮使用相同的ActionListener并检查按下了哪个按钮,您应该只将ActionListener实例化一次并将其添加到每个按钮,而不是为每个按钮创建一个按钮。
答案 1 :(得分:1)
您可以将ActionEvent.getSource()与JButton []按钮结合使用,以找出触发该actionPerformed的按钮的索引。我希望这足够神秘:)
答案 2 :(得分:1)
为每个不同的操作使用不同的ActionListener
实例。它可能是同一个类,可能与同一参数化方法中的其他东西一起构造。
如:
addItem("Quit", quitCommand);
...
private void addItem(String text, final Runnable command) {
JMenuItem item = new JMenuItem(text);
item.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent event) {
command.run();
}
});
menu.add(item);
}