如何询问数组中的某个按钮是否属于矩阵?例如这个。
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();
}
});
}
}
简单的嵌套for循环?
public boolean hasButton(JButton button)
{
for(int i = 0; i < matrix.length; i++)
{
if(matrix[i] != null) for(int j = 0; j < matrix[i].length; j++)
{
if(button == matrix[i][j]) return true;
}
}
return false;
}