目前,我遇到了这个代码的问题,我希望每次我都向地图添加一个点,将JPanel添加到数组Panels(这是一个JPanel数组)。
我无法使用for语句,因为我想每次调用添加1个Jpanel(addPanel()),而且我还面临动作侦听器的问题,它只识别第一个元素数组是正常的,因为JPanel没有添加到数组中。
import java.awt.Color;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.*;
import javax.swing.border.EmptyBorder;
import java.awt.BorderLayout;
public class TestGrid02 extends JPanel {
private static final long serialVersionUID = 1L;
protected static final Color SELECTION_COLOR = Color.pink;
private Color originalColor = new Color(238,238,238) ;
private static final Dimension PREF_SIZE = new Dimension(50, 50);
public JPanel panel_2;
public JPanel panel_3;
public JFrame frame;
public JTextField textField;
public int maxX =10;
public int maxY =10;
public String name;
JPanel[] panels = new JPanel[100];
public TestGrid02() {
initUI();
createMap(panel_2,maxX ,maxY );
}
public final void initUI() {
setLayout(new BorderLayout(0, 0));
panel_2 = new JPanel();
add(panel_2, BorderLayout.WEST);
panel_2.setLayout(new GridLayout(10, 10, 1, 1));
panel_2.setBackground(Color.GRAY);
panel_3 = new JPanel(new BorderLayout(4, 4));
add(panel_3, BorderLayout.LINE_END);
panel_3.setBackground(Color.GREEN);
JPanel controls = new JPanel(new GridLayout(0, 1, 0, 25));
panel_3.add(controls, BorderLayout.PAGE_START);
controls.setBackground(Color.CYAN);
controls.setBorder(new EmptyBorder(40, 20, 20, 20));
controls.add(new JTextField(10));
controls.add(new JTextField(10));
controls.add(new JButton("OK"));
}
public void createMap(JPanel a,int maxX,int maxY) {
String [ ][ ] map = new String [maxX][maxY];
for (int x = maxX-1; x>=0 ; x--) {
for (int y = 0; y<maxY ; y++) {
int i=x-1;
int j=y-1;
map [x][y] = i + ","+ j ;
System.out.println( map [x][y]);
addPanel(a,i,j);
}}}
public void addPanel(JPanel a,int c, int d) {
int i =0;
panels[i] = new JPanel();
a.add(panels[i]);
panels[i].setPreferredSize(PREF_SIZE);
name = String.format("[%d, %d]", c,d);
panels[i].setBackground(originalColor);
panels[i].setName(name);
System.out.println( "PanelName:"+panels[i].getName());
//panels[i] =panels[i];
//I need this statement to add the panel to the array panels
panels[i].addMouseListener(new MouseAdapter() {
@Override
public void mouseEntered(MouseEvent e) {
panels[i].setBackground(Color.pink);
panels[i].add(new JLabel(panels[i].getName()));
panels[i].repaint();
panels[i].revalidate();
}
@Override
public void mouseExited(MouseEvent e) {
panels[i].setBackground(originalColor);
panels[i].repaint();
panels[i].removeAll();
}
});
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
JFrame frame = new JFrame("Astar");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add(new TestGrid02());
frame.setLocationRelativeTo(null);
frame.pack();
frame.setVisible(true);
}
});
}
}
答案 0 :(得分:0)
它只识别数组中的第一个元素
这是因为你所有的代码都引用了变量&#34; i&#34;这似乎永远不会改变。
从可见GUI添加(或删除)组件时,基本代码为:
panel.add(...);
panel.revalidate(); // to invoke the layout manager
panel.repaint();
不要使用阵列来固定面板。你正在限制自己的硬编码大小。而是使用ArrayList
。或者真正的问题是你甚至需要一个数组来保存面板以供将来处理吗?
您的MouseListener代码错误。要引用生成事件的组件,您的代码应该是:
JPanel panel = (JPanel)e.getSource();
panel.setBackground(...);