Java:可点击元素的网格

时间:2011-11-05 21:37:35

标签: java swing interface grid

我打算为我当前的项目制作一个编辑器,并且需要一些java摇摆。

基本上我需要某种框架内的网格;应该可以通过点击选择网格的单个元素,并使用下拉/选择器元素,您应该能够更改网格元素的颜色

谁能告诉我我需要哪些摆动部分呢?任何帮助都会非常感激;)

编辑:让我们进入细节

这个编辑器计划生成我与我的一些朋友开发的Android策略游戏的地图 我们假设我们有一个16x16字段的正方形字段,默认情况下都是绿色的。 通过选择单个字段,我希望能够将此字段的颜色更改为其他字段。

此外,每个字段都应该能够返回它的值(即颜色)

1 个答案:

答案 0 :(得分:10)

你的问题在细节上有点简短,但也许你想要一个使用GridLayout的JPanel并保存一个其opaque属性为true的JLabel数组。你可以将一个MouseListener添加到JLabel,它显示一个显示可能颜色的JPopupMenu,然后根据选择使用它来设置JLabel的背景颜色(显示它已经变得不透明)。

例如:

Main.java

import javax.swing.JFrame;
import javax.swing.SwingUtilities;

public class Main {
   private static void createAndShowGui() {
      int rows = 20;
      int cols = 40;
      int cellWidth = 20;
      ColorGrid mainPanel = new ColorGrid(rows, cols, cellWidth);

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

ColorGrid.java

import java.awt.Dimension;
import java.awt.GridLayout;
import javax.swing.*;

@SuppressWarnings("serial")
public class ColorGrid extends JPanel {
   private MyColor[][] myColors;
   private JLabel[][] myLabels;

   public ColorGrid(int rows, int cols, int cellWidth) {
      myColors = new MyColor[rows][cols];
      myLabels = new JLabel[rows][cols];

      MyMouseListener myListener = new MyMouseListener(this);
      Dimension labelPrefSize = new Dimension(cellWidth, cellWidth);
      setLayout(new GridLayout(rows, cols));
      for (int row = 0; row < myLabels.length; row++) {
         for (int col = 0; col < myLabels[row].length; col++) {
            JLabel myLabel = new JLabel();
            myLabel = new JLabel();
            myLabel.setOpaque(true);
            MyColor myColor = MyColor.GREEN;
            myColors[row][col] = myColor;
            myLabel.setBackground(myColor.getColor());
            myLabel.addMouseListener(myListener);
            myLabel.setPreferredSize(labelPrefSize);
            add(myLabel);
            myLabels[row][col] = myLabel;
         }
      }
   }

   public MyColor[][] getMyColors() {
      return myColors;
   }

   public void labelPressed(JLabel label) {
      for (int row = 0; row < myLabels.length; row++) {
         for (int col = 0; col < myLabels[row].length; col++) {
            if (label == myLabels[row][col]) {
               MyColor myColor = myColors[row][col].next();
               myColors[row][col] = myColor;
               myLabels[row][col].setBackground(myColor.getColor());
            }
         }
      }
   }
}

MyColor.java

import java.awt.Color;

public enum MyColor {
   GREEN(Color.green, "Green", "g"), RED(Color.red, "Red", "r"), 
   BLUE(Color.blue, "Blue", "b"), YELLOW(Color.yellow, "Yellow", "y");
   private Color color;
   private String name;
   private String shortName;

   private MyColor(Color color, String name, String shortName) {
      this.color = color;
      this.name = name;
      this.shortName = shortName;
   }

   public MyColor next() {
      int index = 0;
      for (int i = 0; i < MyColor.values().length; i++) {
         if (MyColor.values()[i] == this) {
            index = (i + 1) % MyColor.values().length;
         }
      }
      return MyColor.values()[index];
   }

   public Color getColor() {
      return color;
   }

   public String getName() {
      return name;
   }

   public String getShortName() {
      return shortName;
   }

   @Override
   public String toString() {
      return shortName;
   }

}

MyMouseListener.java

import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.JLabel;

public class MyMouseListener extends MouseAdapter {
   private ColorGrid colorGrid;

   public MyMouseListener(ColorGrid colorGrid) {
      this.colorGrid = colorGrid;
   }

   @Override
   public void mousePressed(MouseEvent e) {
      if (e.getButton() == MouseEvent.BUTTON1) {
         colorGrid.labelPressed((JLabel)e.getSource());
      } else if (e.getButton() == MouseEvent.BUTTON3) {
         MyColor[][] myColors = colorGrid.getMyColors();
         for (int row = 0; row < myColors.length; row++) {
            for (int col = 0; col < myColors[row].length; col++) {
               System.out.print(myColors[row][col] + " ");
            }
            System.out.println();
         }
         System.out.println();
      }
   }
}