1

我使用了Hovercraft Full Of Eels中的代码,并对其进行了一些操作,以获得我想要的编号。现在看起来不错,我可以读取状态,但我不知道如何使用表单上的按钮操作各个复选框。有人可以帮忙吗?

package and.lotto;
import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import javax.swing.*;

@SuppressWarnings("serial")
public class NewLotto extends JPanel {
   public static final int GRID_PANEL_ROWS = 3;
   public static final int GRID_PANEL_COLS = 4;
   public static final int PANELS = 12;
   public static final int BOXES = 12;
   private static final int GAP = 1;
   private CheckBoxGrid[] checkBoxGrid = new CheckBoxGrid[PANELS];

   public NewLotto() {
      setLayout(new GridLayout(GRID_PANEL_ROWS, GRID_PANEL_COLS, GAP, GAP));
      for (int rows = 0; rows < BOXES; rows++) {
            checkBoxGrid[rows] = new CheckBoxGrid(rows,0);
            add(checkBoxGrid[rows]);
         }
   }

   private static void createAndShowGui() {
      NewLotto mainPanel = new NewLotto();
      ButtonPanel buttons = new ButtonPanel();

      JFrame frame = new JFrame("Lotto");
      frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      frame.setLayout(new BorderLayout());
      frame.add(buttons,BorderLayout.NORTH);
      frame.add(mainPanel, BorderLayout.SOUTH);
      frame.pack();
      frame.setLocationByPlatform(true);
      frame.setVisible(true);
   }

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

         }
      });
   }
}
@SuppressWarnings("serial")
class ButtonPanel extends JPanel {
    JButton generate;
    JButton clear;
    private ActionListener actionListener = new MyButtonListener();

    public ButtonPanel(){
        generate = new JButton("Generate numbers");
        generate.addActionListener(actionListener);
        clear = new JButton("Clear All");
        clear.addActionListener(actionListener);
        add(generate);
        add(clear);
    }

    private class MyButtonListener implements ActionListener {

        @Override
        public void actionPerformed(ActionEvent e) {
            System.out.println("Testing");

        }

    }
}

@SuppressWarnings("serial")
class CheckBoxGrid extends JPanel {
   private static final int CHECK_BOXES = 35;
   private static final int CHECK_BOX_COLS = 6;
   private static final int CHECK_BOX_ROWS = 6;
   private static final int GAP = -5;
   private JCheckBox[] checkBoxes = new JCheckBox[CHECK_BOXES];
   private int gridIndex;
   private ItemListener itemListener = new MyCheckBoxListener();
   private int row;
   private int col;

   public CheckBoxGrid(int row, int col) {
      this.row = row;
      this.col = col;
      gridIndex = row + col + 1;
      setBorder(BorderFactory.createTitledBorder(String.valueOf(gridIndex)));
      setLayout(new GridLayout(CHECK_BOX_ROWS, CHECK_BOX_COLS, GAP, GAP));

      for (int cbRow = 0; cbRow < checkBoxes.length; cbRow++) { 
            JCheckBox checkBox = new JCheckBox();
            checkBox.addItemListener(itemListener);
            add(checkBox);
            checkBox.setRolloverEnabled(false);
            checkBox.setRequestFocusEnabled(false);
            checkBoxes[cbRow] = checkBox;
      }
      checkBoxes[0].setIcon(new ImageIcon(NewLotto.class.getResource("/and/lotto/img/red01.png")));
      checkBoxes[0].setSelectedIcon(new ImageIcon(NewLotto.class.getResource("/and/lotto/img/red01sel.png")));

      checkBoxes[1].setIcon(new ImageIcon(NewLotto.class.getResource("/and/lotto/img/red02.png")));
      checkBoxes[1].setSelectedIcon(new ImageIcon(NewLotto.class.getResource("/and/lotto/img/red02sel.png")));

      checkBoxes[2].setIcon(new ImageIcon(NewLotto.class.getResource("/and/lotto/img/red03.png")));
      checkBoxes[2].setSelectedIcon(new ImageIcon(NewLotto.class.getResource("/and/lotto/img/red03sel.png")));

      checkBoxes[3].setIcon(new ImageIcon(NewLotto.class.getResource("/and/lotto/img/red04.png")));
      checkBoxes[3].setSelectedIcon(new ImageIcon(NewLotto.class.getResource("/and/lotto/img/red04sel.png")));

      checkBoxes[4].setIcon(new ImageIcon(NewLotto.class.getResource("/and/lotto/img/red05.png")));
      checkBoxes[4].setSelectedIcon(new ImageIcon(NewLotto.class.getResource("/and/lotto/img/red05sel.png")));

      checkBoxes[5].setIcon(new ImageIcon(NewLotto.class.getResource("/and/lotto/img/red06.png")));
      checkBoxes[5].setSelectedIcon(new ImageIcon(NewLotto.class.getResource("/and/lotto/img/red06sel.png")));

      checkBoxes[6].setIcon(new ImageIcon(NewLotto.class.getResource("/and/lotto/img/red07.png")));
      checkBoxes[6].setSelectedIcon(new ImageIcon(NewLotto.class.getResource("/and/lotto/img/red07sel.png")));

      checkBoxes[7].setIcon(new ImageIcon(NewLotto.class.getResource("/and/lotto/img/red08.png")));
      checkBoxes[7].setSelectedIcon(new ImageIcon(NewLotto.class.getResource("/and/lotto/img/red08sel.png")));

      checkBoxes[8].setIcon(new ImageIcon(NewLotto.class.getResource("/and/lotto/img/red09.png")));
      checkBoxes[8].setSelectedIcon(new ImageIcon(NewLotto.class.getResource("/and/lotto/img/red09sel.png")));

      checkBoxes[9].setIcon(new ImageIcon(NewLotto.class.getResource("/and/lotto/img/red10.png")));
      checkBoxes[9].setSelectedIcon(new ImageIcon(NewLotto.class.getResource("/and/lotto/img/red10sel.png")));

      checkBoxes[10].setIcon(new ImageIcon(NewLotto.class.getResource("/and/lotto/img/red11.png")));
      checkBoxes[10].setSelectedIcon(new ImageIcon(NewLotto.class.getResource("/and/lotto/img/red11sel.png")));

      checkBoxes[11].setIcon(new ImageIcon(NewLotto.class.getResource("/and/lotto/img/red12.png")));
      checkBoxes[11].setSelectedIcon(new ImageIcon(NewLotto.class.getResource("/and/lotto/img/red12sel.png")));

      checkBoxes[12].setIcon(new ImageIcon(NewLotto.class.getResource("/and/lotto/img/red13.png")));
      checkBoxes[12].setSelectedIcon(new ImageIcon(NewLotto.class.getResource("/and/lotto/img/red13sel.png")));

      checkBoxes[13].setIcon(new ImageIcon(NewLotto.class.getResource("/and/lotto/img/red14.png")));
      checkBoxes[13].setSelectedIcon(new ImageIcon(NewLotto.class.getResource("/and/lotto/img/red14sel.png")));

      checkBoxes[14].setIcon(new ImageIcon(NewLotto.class.getResource("/and/lotto/img/red15.png")));
      checkBoxes[14].setSelectedIcon(new ImageIcon(NewLotto.class.getResource("/and/lotto/img/red15sel.png")));

      checkBoxes[15].setIcon(new ImageIcon(NewLotto.class.getResource("/and/lotto/img/red16.png")));
      checkBoxes[15].setSelectedIcon(new ImageIcon(NewLotto.class.getResource("/and/lotto/img/red16sel.png")));

      checkBoxes[16].setIcon(new ImageIcon(NewLotto.class.getResource("/and/lotto/img/red17.png")));
      checkBoxes[16].setSelectedIcon(new ImageIcon(NewLotto.class.getResource("/and/lotto/img/red17sel.png")));

      checkBoxes[17].setIcon(new ImageIcon(NewLotto.class.getResource("/and/lotto/img/red18.png")));
      checkBoxes[17].setSelectedIcon(new ImageIcon(NewLotto.class.getResource("/and/lotto/img/red18sel.png")));

      checkBoxes[18].setIcon(new ImageIcon(NewLotto.class.getResource("/and/lotto/img/red19.png")));
      checkBoxes[18].setSelectedIcon(new ImageIcon(NewLotto.class.getResource("/and/lotto/img/red19sel.png")));

      checkBoxes[19].setIcon(new ImageIcon(NewLotto.class.getResource("/and/lotto/img/red20.png")));
      checkBoxes[19].setSelectedIcon(new ImageIcon(NewLotto.class.getResource("/and/lotto/img/red20sel.png")));

      checkBoxes[20].setIcon(new ImageIcon(NewLotto.class.getResource("/and/lotto/img/red21.png")));
      checkBoxes[20].setSelectedIcon(new ImageIcon(NewLotto.class.getResource("/and/lotto/img/red21sel.png")));

      checkBoxes[21].setIcon(new ImageIcon(NewLotto.class.getResource("/and/lotto/img/red22.png")));
      checkBoxes[21].setSelectedIcon(new ImageIcon(NewLotto.class.getResource("/and/lotto/img/red22sel.png")));

      checkBoxes[22].setIcon(new ImageIcon(NewLotto.class.getResource("/and/lotto/img/red23.png")));
      checkBoxes[22].setSelectedIcon(new ImageIcon(NewLotto.class.getResource("/and/lotto/img/red23sel.png")));

      checkBoxes[23].setIcon(new ImageIcon(NewLotto.class.getResource("/and/lotto/img/red24.png")));
      checkBoxes[23].setSelectedIcon(new ImageIcon(NewLotto.class.getResource("/and/lotto/img/red24sel.png")));

      checkBoxes[24].setIcon(new ImageIcon(NewLotto.class.getResource("/and/lotto/img/red25.png")));
      checkBoxes[24].setSelectedIcon(new ImageIcon(NewLotto.class.getResource("/and/lotto/img/red25sel.png")));

      checkBoxes[25].setIcon(new ImageIcon(NewLotto.class.getResource("/and/lotto/img/red26.png")));
      checkBoxes[25].setSelectedIcon(new ImageIcon(NewLotto.class.getResource("/and/lotto/img/red26sel.png")));

      checkBoxes[26].setIcon(new ImageIcon(NewLotto.class.getResource("/and/lotto/img/red27.png")));
      checkBoxes[26].setSelectedIcon(new ImageIcon(NewLotto.class.getResource("/and/lotto/img/red27sel.png")));

      checkBoxes[27].setIcon(new ImageIcon(NewLotto.class.getResource("/and/lotto/img/red28.png")));
      checkBoxes[27].setSelectedIcon(new ImageIcon(NewLotto.class.getResource("/and/lotto/img/red28sel.png")));

      checkBoxes[28].setIcon(new ImageIcon(NewLotto.class.getResource("/and/lotto/img/red29.png")));
      checkBoxes[28].setSelectedIcon(new ImageIcon(NewLotto.class.getResource("/and/lotto/img/red29sel.png")));

      checkBoxes[29].setIcon(new ImageIcon(NewLotto.class.getResource("/and/lotto/img/red30.png")));
      checkBoxes[29].setSelectedIcon(new ImageIcon(NewLotto.class.getResource("/and/lotto/img/red30sel.png")));

      checkBoxes[30].setIcon(new ImageIcon(NewLotto.class.getResource("/and/lotto/img/red31.png")));
      checkBoxes[30].setSelectedIcon(new ImageIcon(NewLotto.class.getResource("/and/lotto/img/red31sel.png")));

      checkBoxes[31].setIcon(new ImageIcon(NewLotto.class.getResource("/and/lotto/img/red32.png")));
      checkBoxes[31].setSelectedIcon(new ImageIcon(NewLotto.class.getResource("/and/lotto/img/red32sel.png")));

      checkBoxes[32].setIcon(new ImageIcon(NewLotto.class.getResource("/and/lotto/img/red33.png")));
      checkBoxes[32].setSelectedIcon(new ImageIcon(NewLotto.class.getResource("/and/lotto/img/red33sel.png")));

      checkBoxes[33].setIcon(new ImageIcon(NewLotto.class.getResource("/and/lotto/img/red34.png")));
      checkBoxes[33].setSelectedIcon(new ImageIcon(NewLotto.class.getResource("/and/lotto/img/red34sel.png")));

      checkBoxes[34].setIcon(new ImageIcon(NewLotto.class.getResource("/and/lotto/img/red35.png")));
      checkBoxes[34].setSelectedIcon(new ImageIcon(NewLotto.class.getResource("/and/lotto/img/red35sel.png")));
   }

   private class MyCheckBoxListener implements ItemListener {

      @Override
      public void itemStateChanged(ItemEvent itemEvt) {
         JCheckBox source = (JCheckBox) itemEvt.getSource();
         boolean selected = source.isSelected();
         int cbRow = -1;

         for (int r = 0; r < checkBoxes.length; r++) {
              if (source.equals(checkBoxes[r])) {
                  cbRow = r;  
              }
         }

         String text = String.format("Grid %d, selected: %b, at %d", 
               (row + col + 1), selected, cbRow);
         System.out.println(text);
      }

   }

}

这是原始问题:

我正在尝试创建一个包含 12 个 JPanel 的数组的布局,每个 JPanel 包含一个包含 35 个 JCheckbox 的数组。我遇到的问题是,尽管面板和复选框在表单上都显示良好,但我无法访问各个复选框的属性。这是我使用的部分代码:

JPanel panel_south = new JPanel();
contentPane.add(panel_south, BorderLayout.SOUTH);
panel_south.setLayout(new GridLayout(3, 4, 1, 1));

for(Integer i =0; i<12;i++){
     Integer title = i+1;
     EtchedBorder border = new EtchedBorder(EtchedBorder.LOWERED, null, null);
     TitledBorder titled = new TitledBorder(border,title.toString());
     row[i] = new JPanel();
     row[i].setBorder(titled);
     row[i].setLayout(new GridLayout(6, 6, -6, -5));
     panel_south.add(row[i]);
     JCheckBox[] rad = new JCheckBox[35];

     for (int j = 0;j<35;j++){
    rad[j] = new JCheckBox();
    row[i].add(rad[j]);
     }

当我尝试在 for 循环中使用这样的行时:

行[i].rad[j].setSelected(true);

我收到错误: rad 无法解决 我在这里做错了什么?

4

1 回答 1

3

您应该重构包含 JTextField 网格的 JPanel,为此创建一个单独的类,该类接受一个数字作为其索引,并包含 JCheckBox 网格或 JTable。

例如,

import java.awt.GridLayout;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import javax.swing.*;

@SuppressWarnings("serial")
public class CheckBoxGridMain extends JPanel {
   public static final int GRID_PANEL_ROWS = 3;
   public static final int GRID_PANEL_COLS = 4;
   private static final int GAP = 1;
   private CheckBoxGrid[][] checkBoxGrid = new CheckBoxGrid[GRID_PANEL_ROWS][GRID_PANEL_COLS];

   public CheckBoxGridMain() {
      setLayout(new GridLayout(GRID_PANEL_ROWS, GRID_PANEL_COLS, GAP, GAP));
      for (int row = 0; row < checkBoxGrid.length; row++) {
         for (int col = 0; col < checkBoxGrid[row].length; col++) {
            checkBoxGrid[row][col] = new CheckBoxGrid(row, col);
            add(checkBoxGrid[row][col]);
         }
      }
   }

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

      JFrame frame = new JFrame("CheckBox Grid");
      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();
         }
      });
   }
}

@SuppressWarnings("serial")
class CheckBoxGrid extends JPanel {
   private static final int CHECK_BOX_ROWS = 6;
   private static final int CHECK_BOX_COLS = 6;
   private static final int GAP = -5;
   private JCheckBox[][] checkBoxes = new JCheckBox[CHECK_BOX_ROWS][CHECK_BOX_COLS];
   private int gridIndex;
   private ItemListener itemListener = new MyCheckBoxListener();
   private int row;
   private int col;

   public CheckBoxGrid(int row, int col) {
      this.row = row;
      this.col = col;
      gridIndex = row + col + 1;
      setBorder(BorderFactory.createTitledBorder(String.valueOf(gridIndex)));
      setLayout(new GridLayout(CHECK_BOX_ROWS, CHECK_BOX_COLS, GAP, GAP));

      for (int cbRow = 0; cbRow < checkBoxes.length; cbRow++) {
         for (int cbCol = 0; cbCol < checkBoxes[cbRow].length; cbCol++) {
            JCheckBox checkBox = new JCheckBox();
            checkBox.addItemListener(itemListener);
            add(checkBox);
            checkBoxes[cbRow][cbCol] = checkBox;
         }
      }
   }

   private class MyCheckBoxListener implements ItemListener {

      @Override
      public void itemStateChanged(ItemEvent itemEvt) {
         JCheckBox source = (JCheckBox) itemEvt.getSource();
         boolean selected = source.isSelected();
         int cbRow = -1;
         int cbCol = -1;
         for (int r = 0; r < checkBoxes.length; r++) {
            for (int c = 0; c < checkBoxes[r].length; c++) {
               if (source.equals(checkBoxes[r][c])) {
                  cbRow = r;
                  cbCol = c;
               }
            }
         }

         String text = String.format("Grid %d, selected: %b, at [%d, %d]", 
               (row + col + 1), selected, cbCol, cbRow); // corrected row/col order
         System.out.println(text);
      }

   }

}

哪个显示
在此处输入图像描述

于 2013-03-31T17:37:20.947 回答