问题
I have created a Jframe/button with 10x10 grid. Each jbutton is apart of the grid. I am trying to how to affect each button pressed through JFrame/button, as I want to eventually make it into a battleships games.
frame.setLayout(new GridLayout(width,length));
grid=new JButton[width][length];
for(int y=0; y<length; y++){
for(int x=0; x<width; x++){
grid[x][y]=new JButton("("+x+","+y+")");
frame.add(grid[x][y]);
}
}
For example I am trying a basic piece of code to see if i can change the color of the Jframe to red by clicking it but it doesn't seem to be working.
public void actionPerformed(ActionEvent e){
if( e.getSource() instanceof JButton) {
((JButton)e.getSource()).setBackground(Color.red);
}
}
Anyone got any ideas?
回答1:
I made this work by creating the JButtons separately rather than as part of a grid, but the general idea is the same. You cannot call actionPerformed like you have it, you must have a class that implements ActionListener and then has an override for the method actionPerformed.
You need to add a actionlistener to each of the JButtons. In this case since you want to apply the same listener to multiple buttons you want a separate class underneath your main.
class buttonListener implements ActionListener {
@Override
public void actionPerformed (ActionEvent e) {
((JButton)e.getSource()).setBackground(Color.red);
}
}
The reason why the button was not changing colours is because you need to add the following in order to change the colour of a JButton
JButton j = new JButton("test");
j.setSize(100, 100);
j.setContentAreaFilled(true);
j.setOpaque(true);
j.addActionListener(new buttonListener());
I know this isn't the most direct answer to your question but I hope I at least helped get the colours sorted out.
回答2:
Let's say we have a button: JButton button. To invoke an action when the button is pressed, an action listener must be added to it. There are two ways of doing this (that I know of):
ActionListener
I think this is more often used than the second method. It's also easier and faster to write IMO:
JButton button = new JButton("Click me");
button.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
System.out.println("button was clicked!");
}
}
Action
Another kind of action listener. The functionality and use is somewhat different. However to achieve a button that behaves simarly to an ActionListener, do this:
Action buttonAction = new AbstractAction("Click me") {
@Override
public void actionPerformed(ActionEvent e) {
System.out.println("button was clicked!");
}
}
JButton button = new JButton(action);
Note that in both examples I'm using anonymous inner classes. In most cases, using a named inner class or even an external class is more preferable.
Choosing between an ActionListener and an Action depends a little on the situation (as always... sigh), and I'm afraid I cannot shed too much light on this matter. Google is your friend here. A fast search provided this post from SO: link
回答3:
you can create your own action listener:
class MyActionListener implements ActionListener {
private int x;
private int y;
public MyActionListener(int x, int y) {
this.x = x;
this.y = y;
}
public void actionPerformed(ActionEvent e) {
DoSomething(x, y);
}
}
...
grid = new JButton[wid][len];
for(int y = 0; y < len; y++){
for(int x = 0; x < wid; x++){
grid[x][y]=new JButton("("+x+","+y+")");
grid[x][y].addActionListener(new MyActionListener(x, y));
frame.add(grid[x][y]);
}
}
来源:https://stackoverflow.com/questions/27255807/jbutton-action-performed