java 执行了 JButton 操作?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/27255807/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-02 11:30:24  来源:igfitidea点击:

JButton Action Performed?

javaarraysjbutton

提问by 3eternus

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.

我创建了一个带有 10x10 网格的 Jframe/按钮。每个 jbutton 都是网格的一部分。我正在尝试如何影响通过 JFrame/button 按下的每个按钮,因为我想最终将其制作成战舰游戏。

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.

例如,我正在尝试一段基本的代码,看看我是否可以通过单击将 Jframe 的颜色更改为红色,但它似乎不起作用。

 public void actionPerformed(ActionEvent e){
           if( e.getSource() instanceof JButton) {
               ((JButton)e.getSource()).setBackground(Color.red);
           }
        }

Anyone got any ideas?

有人有任何想法吗?

回答by Dacotah

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.

我通过单独创建 JButton 而不是作为网格的一部分来完成这项工作,但总体思路是相同的。你不能像你拥有的那样调用 actionPerformed,你必须有一个实现 ActionListener 的类,然后有一个方法 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.

您需要为每个 JButton 添加一个动作侦听器。在这种情况下,由于您想将相同的侦听器应用于多个按钮,因此您需要在 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 的颜色

    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.

我知道这不是您问题的最直接答案,但我希望我至少能帮助您理清颜色。

回答by Olavi Mustanoja

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):

假设我们有一个按钮:JButton button。要在按下按钮时调用动作,必须向其添加动作侦听器。有两种方法可以做到这一点(我知道):

ActionListener

动作监听器

I think this is more often used than the second method. It's also easier and faster to write IMO:

我认为这比第二种方法更常用。编写 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:

另一种动作监听器。功能和用途有些不同。但是,要实现行为类似于 的按钮ActionListener,请执行以下操作:

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 innerclasses. In most cases, using a named inner class or even an external class is more preferable.

请注意,在两个示例中,我都使用匿名 类。在大多数情况下,使用命名的内部类甚至外部类更可取。



Choosing between an ActionListenerand an Actiondepends 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

在 anActionListener和 an之间进行选择Action取决于情况(一如既往......叹息),恐怕我不能对这个问题透露太多。谷歌是你的朋友。快速搜索提供了这篇来自 SO 的帖子:链接

回答by Carlos Rojas

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]);
    }
}