在哪里以及如何使用两个按钮的监听器?

时间:2016-12-11 00:57:57

标签: java

我想将侦听器添加到两个“清单”和“统计信息”按钮以打开新帧。我一直在四处寻找,似乎无法理解在什么情况下将它们放在哪里以及为什么我尝试过的都没有工作。

我应该将它们放在哪里,以使按钮正常工作?

Incassable.java

class Incassable extends tiles { 
 public static void main(String args[]) { 
  JFrame frame = new JFrame();
  frame.setResizable(false);
  JPanel main = new JPanel();
  GridLayout grid = new GridLayout(0,15);
  GridLayout bot = new GridLayout(0,2);  
  main.setLayout(grid);
  frame.add(main,BorderLayout.PAGE_START);

  int[][] map = {
    { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, },
    { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, },
    { 0, 0, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 0, 0, },
    { 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, },
    { 0, 0, 0, 0, 0, 1, 1, 1, 0, 1, 0, 0, 0, 0, 0, },
    { 0, 0, 0, 0, 0, 1, 0, 1, 0, 1, 0, 0, 0, 0, 0, },
    { 0, 0, 0, 0, 0, 1, 0, 1, 0, 1, 0, 0, 0, 0, 0, },
    { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, },
    { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, },
    { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, }
};  
  createMap(main, map);

  JPanel menu = new JPanel();
  menu.setLayout(bot);
  frame.add(menu,BorderLayout.PAGE_END);
  JButton inv = new JButton("Inventory");
  JButton stats = new JButton("Statistics");
  menu.add(inv);
  menu.add(stats);

  frame.pack();
  frame.setVisible(true);
 }
}

tiles.java

public class tiles {
    public static void addGrass(JPanel panel, int amount) {
      for (int n = 0; n < amount; n++) {
       ImageIcon icon = new ImageIcon("grass.png");
       JLabel label = new JLabel(icon);
       panel.add(label);
      }
    }

public static void addBlack(JPanel panel, int amount) {
  for (int n = 0; n < amount; n++) {
   ImageIcon icon = new ImageIcon("black.jpg");
   JLabel label = new JLabel(icon);
   panel.add(label);
  }
}

public static void createMap(JPanel panel, int[][] map) {
    for (int n = 0; n < map.length; n++) {
      for (int k = 0; k < 15; k++) {
          if (map[n][k] == 0) { addGrass(panel,1); }
          if (map[n][k] == 1) { addBlack(panel,1); }
      }
    }    
  }
}

0 个答案:

没有答案