gpt4 book ai didi

java - 如何询问 JButton 是否是 JButton 数组的一部分?

转载 作者:行者123 更新时间:2023-12-01 14:27:01 26 4
gpt4 key购买 nike

如何询问数组中的某个按钮是否属于矩阵?例如这个。

JButton matrix = new JButton[8][8];

如何询问数组中的某个按钮是否属于矩阵?例如这个。

matrix[i+2][j-1]

任何帮助将不胜感激,我对java真的很陌生,并且没有向我解释任何内容。所以我一直在挣扎。

最佳答案

最好的解决方案是仅将按钮数组类型的 ActionListener(或更好的 AbstractAction)提供给实际上位于数组中的 JButton。换句话说,单独的 ActionListener 或 AbstractActions 用于单独的行为。分而治之!

例如:

import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.awt.Window;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.*;

@SuppressWarnings("serial")
public class GuiEg extends JPanel {
private static final String[][] BUTTON_TXTS = {
{ "1", "2", "3" },
{ "4", "5", "6" },
{ "7", "8", "9" },
{ "", "0", "" } };
private JButton[][] calcButtons = new JButton[BUTTON_TXTS.length][BUTTON_TXTS[0].length];

public GuiEg() {
// get rid of magic numbers
setLayout(new BorderLayout(5, 5));
add(createEastPanel(), BorderLayout.EAST);
add(createCenterPanel(), BorderLayout.CENTER);
}

private JComponent createEastPanel() {
JPanel eastPanel = new JPanel(new GridLayout(0, 1));
eastPanel.add(new JButton(new AbstractAction("Start") {

@Override
public void actionPerformed(ActionEvent e) {
System.out.println("Start has been pressed!");
}
}));
eastPanel.add(new JButton(new AbstractAction("Stop") {

@Override
public void actionPerformed(ActionEvent e) {
System.out.println("Stop has been pressed!");
}
}));
eastPanel.add(new JButton(new AbstractAction("Pause") {

@Override
public void actionPerformed(ActionEvent e) {
System.out.println("Pause has been pressed!");
}
}));
eastPanel.add(new JButton(new AbstractAction("Exit") {

@Override
public void actionPerformed(ActionEvent e) {
Window win = SwingUtilities.getWindowAncestor(GuiEg.this);
win.dispose();
}
}));
return eastPanel;
}

private JComponent createCenterPanel() {
int rows = BUTTON_TXTS.length;
int cols = BUTTON_TXTS[0].length;
JPanel centerPanel = new JPanel(new GridLayout(rows , cols));
ActionListener calcListener = new CalcListener();
for (int r = 0; r < BUTTON_TXTS.length; r++) {
for (int c = 0; c < BUTTON_TXTS[r].length; c++) {
String text = BUTTON_TXTS[r][c];
if (!text.isEmpty()) {
calcButtons[r][c] = new JButton(text);
calcButtons[r][c].addActionListener(calcListener);
centerPanel.add(calcButtons[r][c]);
} else {
centerPanel.add(new JLabel());
}
}
}
return centerPanel;
}

private class CalcListener implements ActionListener {
@Override
public void actionPerformed(ActionEvent evt) {
System.out.println(evt.getActionCommand() + " button pressed!");
}
}

private static void createAndShowGui() {
GuiEg mainPanel = new GuiEg();

JFrame frame = new JFrame("GuiEg");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add(mainPanel);
frame.pack();
frame.setLocationByPlatform(true);
frame.setVisible(true);
}

public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGui();
}
});
}
}

关于java - 如何询问 JButton 是否是 JButton 数组的一部分?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17128308/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com