我正在研究一个java教程,看到在GridLayout中找到JButton的x/y索引的方法是遍历与布局关联的按钮b的二维数组,并检查是否
b[i][j] == buttonReference.
@Override
public void actionPerformed(ActionEvent ae) {
JButton bx = (JButton) ae.getSource();
for (int i = 0; i < 5; i++)
for (int j = 0; j < 5; j++)
if (b[i][j] == bx)
{
bx.setBackground(Color.RED);
}
}
Run Code Online (Sandbox Code Playgroud)
是否有更简单的方法来获取按钮的X/Y索引?
就像是:
JButton button = (JButton) ev.getSource();
int x = this.getContentPane().getComponentXIndex(button);
int y = this.getContentPane().getComponentYIndex(button);
Run Code Online (Sandbox Code Playgroud)
this是一个GameWindow实例,ev当用户按下按钮时触发ActionEvent.

在这种情况下,它应该得到:x == 2,y == 1
@ GameWindow.java:
package javaswingapplication;
import java.awt.Color;
import java.awt.GridLayout;
import java.awt.event.*;
import javax.swing.*;
public class …Run Code Online (Sandbox Code Playgroud) 以下代码呈现JButton无文本:
public abstract class Test {
public static void main(String... args) {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException e) {
e.printStackTrace();
}
SwingUtilities.invokeLater(() -> {
JFrame frame = new JFrame();
JPanel panel = new JPanel();
String text = "Invisible";
JButton button = new JButton(text); // blank button rendered ???
System.out.println(button.getText()); // prints Invisible
button.setAction(new AbstractAction() {
@Override
public void actionPerformed(ActionEvent arg0) {
// do nothing
}
});
System.out.println(button.getText()); // prints null ??? …Run Code Online (Sandbox Code Playgroud) 我怎样才能给出附在他们身上JMenuItem的名字ActionListener?
我有一个由单个处理的菜单系统,ActionListener这些菜单中的一些项目重复名称.这不是用户端的问题,因为它显然是做什么的; 事实上,如果他们有不同的名字会更加混乱.但是,在我的最后,我想要唯一地标记每个项目.
创建我的项目的部分如下所示:
String label = getLabel(forThisItem);
JMenuItem item = new JMenuItem(label);
item.setName(parentMenu.getName() + "_" + label);
item.addActionListener(actionListener);
parentmenu.add(item);
Run Code Online (Sandbox Code Playgroud)
使用getName()在事后(在此方法的范围之外)询问项目,给出我给它的名称,因为它应该,但输出
public void actionPerformed(ActionEvent ae) {
String actionPerformed = ae.getActionCommand();
System.out.println("actionPerformed: " + actionPerformed);
}
Run Code Online (Sandbox Code Playgroud)
是用户看到的,可能是重复的名称,由label我指定的名称,而不是我给它的唯一名称.
如何将正确的信息提供给ActionListener?
我有一个30个按钮[]的数组.我有一个变量buttonClicked.当我按下按钮时,如何获取索引并将索引号存储在buttonClicked中?
谢谢 :)
JButton [] buttons = new JButton[30];
for(int i = 1; i <= 30; i++)
{
int btnNumber = (i > 10 && i <= 20) ? (31 - i) : i;
System.out.printf("i = %d, btnNumber = %d%n", i, btnNumber);
buttons[btnNumber - 1] = new JButton("label " + btnNumber);
//buttons[btnNumber - 1].setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
buttons[btnNumber - 1].setBorder(BorderFactory.createEtchedBorder());
buttons[btnNumber - 1].setOpaque(true);
buttons[btnNumber - 1].setBackground(Color.white);
//Puts the player 1 piece on button 1,3,5,7,9 and player 2 piece on button …Run Code Online (Sandbox Code Playgroud)