from csdn: http://topic.csdn.net/u/20110211/22/a019b9c2-61d3-4f99-a3d4-29aa33e8c636.html
the first one is the JButton program:
package ccc; import java.awt.BasicStroke; import java.awt.BorderLayout; import java.awt.Color; import java.awt.EventQueue; import java.awt.Graphics; import java.awt.Graphics2D; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.event.MouseEvent; import java.awt.event.MouseListener; import javax.swing.BorderFactory; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JPanel; public class Test extends JFrame implements Runnable { int main_x = 15; int main_y = 12; int main_toleft = 5; int main_totop = 5; int toppanel_height = 40; int height = main_y * 20 + toppanel_height + 68; int width = main_x * 20 + 15; public static void main(String[] args) { EventQueue.invokeLater(new Runnable() { public void run() { try { Test frame = new Test(); frame.setVisible(true); new Thread(frame).start(); } catch (Exception e) { e.printStackTrace(); } } }); } public Test() { super(); setBounds(0, 0, width, height); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); getContentPane().setLayout(new BorderLayout()); DrawPanel mainpanel = new DrawPanel(); getContentPane().add(mainpanel, BorderLayout.CENTER); } class DrawPanel extends JPanel implements MouseListener, ActionListener { final JButton btn_arr[][] = new JButton[100][100]; public DrawPanel() { super(); setLayout(null); setBorder(BorderFactory.createLoweredBevelBorder()); for (int xx = 0; xx < main_x; xx++) { for (int yy = 0; yy < main_y; yy++) { btn_arr[xx][yy] = new JButton(); btn_arr[xx][yy].setBounds(xx * 20 + 5, yy * 20 + 5, 20, 20); add(btn_arr[xx][yy]); btn_arr[xx][yy].setBackground(Color.DARK_GRAY); btn_arr[xx][yy].setBorder(BorderFactory .createRaisedBevelBorder()); btn_arr[xx][yy].addMouseListener(this); btn_arr[xx][yy].setFocusable(false); } } } public void paint(Graphics g) { super.paint(g); Graphics2D g2 = (Graphics2D) g; g2.setColor(Color.GRAY); g2.setStroke(new BasicStroke(2)); for (int xx = 0; xx < main_x; xx++) { for (int yy = 0; yy < main_y; yy++) { g2.drawLine(xx * 20 + 5, 5, xx * 20 + 5, width - 10); g2.drawLine(5, yy * 20 + 5, height - 10, yy * 20 + 5); } } } public void actionPerformed(ActionEvent e) { } public void mouseClicked(MouseEvent e) { } public void mouseEntered(MouseEvent e) { this.repaint(); } public void mouseExited(MouseEvent e) { } public void mousePressed(MouseEvent e) { } public void mouseReleased(MouseEvent e) { if (e.isPopupTrigger()) { JButton e_btn = ((JButton) e.getSource()); int x = (e_btn.getLocation().x - main_toleft) / 20;// 获取按钮位置确定按钮下标X int y = (e_btn.getLocation().y - main_totop) / 20;// 获取按钮位置确定按钮下标Y for (int loop_x = x - 1; loop_x <= x + 1; loop_x++) { for (int loop_y = y - 1; loop_y <= y + 1; loop_y++) { if ((loop_x < main_x && loop_y < main_y) && (loop_x >= 0 && loop_y >= 0)) { // 越界检查btn_arr_bln组 btn_arr[loop_x][loop_y].doClick(0); } } } } } } public void run() { } }
yes, though the doClick's pressTime is set to zero, the final effect isn't as good as what I wish, it flashes a lot, and cannot last for some time.
here is another program illustrated by JLabel:
package test; import java.awt.Color; import java.awt.Dimension; import java.awt.event.MouseAdapter; import java.awt.event.MouseEvent; import javax.swing.BorderFactory; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JPanel; import javax.swing.SwingUtilities; @SuppressWarnings("serial") public class Test extends JPanel { private static final int ROW_COUNT = 10; private static final int COLUMN_COUNT = 10; private JLabel[][] btns = new JLabel[ROW_COUNT][COLUMN_COUNT]; private static final Color DEFAULT_COLOR = new Color(220, 220, 220); private static final Color PRESSED_COLOR = Color.GREEN; public Test() { int width = 400; int height = 400; setLayout(null); setPreferredSize(new Dimension(width, height)); int w = width / COLUMN_COUNT; int h = height / ROW_COUNT; for (int y = 0; y < COLUMN_COUNT; ++y) { for (int x = 0; x < ROW_COUNT; ++x) { JLabel btn = new JLabel(); btn.setOpaque(true); btn.setBackground(DEFAULT_COLOR); btn.setName(y + "," + x); btn.setBorder(BorderFactory.createEtchedBorder()); btn.setBounds(y * h, x * w, w, h); add(btn); btns[y][x] = btn; handleButtonEvent(btn); } } } private void handleButtonEvent(final JLabel btn) { btn.addMouseListener(new MouseAdapter() { @Override public void mousePressed(MouseEvent e) { String[] coord = btn.getName().split(","); int y = Integer.parseInt(coord[0]); int x = Integer.parseInt(coord[1]); clickButton(y, x); clickButton(y - 1, x - 1); clickButton(y - 1, x); clickButton(y - 1, x + 1); clickButton(y, x - 1); clickButton(y, x + 1); clickButton(y + 1, x - 1); clickButton(y + 1, x); clickButton(y + 1, x + 1); } @Override public void mouseReleased(MouseEvent e) { String[] coord = btn.getName().split(","); int y = Integer.parseInt(coord[0]); int x = Integer.parseInt(coord[1]); releaseButton(y, x); releaseButton(y - 1, x - 1); releaseButton(y - 1, x); releaseButton(y - 1, x + 1); releaseButton(y, x - 1); releaseButton(y, x + 1); releaseButton(y + 1, x - 1); releaseButton(y + 1, x); releaseButton(y + 1, x + 1); } }); } private void clickButton(int y, int x) { if (y < 0 || y >= btns.length || x < 0 || x >= btns[0].length) { return; } btns[y][x].setBackground(PRESSED_COLOR); } private void releaseButton(int y, int x) { if (y < 0 || y >= btns.length || x < 0 || x >= btns[0].length) { return; } btns[y][x].setBackground(DEFAULT_COLOR); } private static void createGuiAndShow() { JFrame frame = new JFrame(""); frame.getContentPane().add(new Test()); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.pack(); frame.setResizable(false); frame.setAlwaysOnTop(true); frame.setLocationRelativeTo(null); frame.setVisible(true); } public static void main(String[] args) { SwingUtilities.invokeLater(new Runnable() { @Override public void run() { createGuiAndShow(); } }); } }
this one looks better, but it doesn't use the button, which lacks the button's press and release effect, is there some better one?