一个简单的Java程序-向面板添加形状时遇到问题

一个简单的Java程序-向面板添加形状时遇到问题,java,Java,我是Java语言和堆栈溢出的新手。我想将形状添加到面板以模拟地图。标题中解释了这个问题-我不知道如何将形状添加到我的面板中。请注意,我大约完成了这个程序的一半,这意味着它还没有我想要的所有功能。这个程序有两个类-Map.java和MapShapes.java 我的第一节课。 您需要将一个ActionListener添加到您的addButton,当该按钮被触发时,将从其他字段收集所需的信息,并将其添加到shapeArray中,并在屏幕上可见的绘图面板的实例上调用repaint 有关更多详细信息,请

我是Java语言和堆栈溢出的新手。我想将形状添加到面板以模拟地图。标题中解释了这个问题-我不知道如何将形状添加到我的面板中。请注意,我大约完成了这个程序的一半,这意味着它还没有我想要的所有功能。这个程序有两个类-
Map.java
MapShapes.java

我的第一节课。


您需要将一个
ActionListener
添加到您的
addButton
,当该按钮被触发时,将从其他字段收集所需的信息,并将其添加到
shapeArray
中,并在屏幕上可见的
绘图面板的实例上调用
repaint


有关更多详细信息,请参见和

非常感谢您!我最好下次再对这么简单的事情提高警惕!
/** Program that maps a room in the house and sees how things will look from an overhead, 2d perspective. ;
 * @author: James ;
 * 
* */

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class Map extends JPanel{ 
  MapShapes[] shapeArray = new MapShapes[20];
  int count = 0;
  private JLabel askLength, askWidth, askX, askY;
  private JTextField length, width, x, y; 
  private JButton addButton;
  private DrawingPanel drawPanel = new DrawingPanel();

 public static void main(String[] args){
JFrame frame = new JFrame("Map");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

Map panel = new Map();
frame.getContentPane().add(panel);

frame.pack();
frame.setVisible(true);
}// end of main;

 public Map(){
  JPanel controlPanel = new JPanel();

addButton = new JButton("Add a shape");

askX = new JLabel("X coordinate: ");
askY = new JLabel("Y coordinate: ");
askLength = new JLabel("Length: ");
askWidth = new JLabel("Width: ");

x = new JTextField(3);
y = new JTextField(3);
length = new JTextField(3);
width = new JTextField(3);

length.addActionListener(new Listener());
width.addActionListener(new Listener());
x.addActionListener(new Listener());
y.addActionListener(new Listener());

controlPanel.add(askX);
controlPanel.add(x);
controlPanel.add(askY);
controlPanel.add(y);
controlPanel.add(askLength);
controlPanel.add(length);
controlPanel.add(askWidth);
controlPanel.add(width);
controlPanel.add(addButton);
controlPanel.setPreferredSize(new Dimension (100, 400));

add(controlPanel);   
add(drawPanel);

 }// end of constructor;

private class DrawingPanel extends JPanel{

/** This public DrawingPanel() constructor.* */
public DrawingPanel(){

  setPreferredSize(new Dimension(400, 400));
  setBackground(Color.pink);

} // End of DrawingPanel() method.

/** This public void paintComponent(Graphics g) method* */
public void paintComponent(Graphics g){
  super.paintComponent(g);

  for(int index = 0; index<count; index++)
    shapeArray[index].display(g);       

} // End of paintComponent(Graphics g) method.

}// End of private class;

  /** This class allows actions to be added to the buttons and text fields;*/
  private class Listener implements ActionListener{

public void actionPerformed(ActionEvent event){
  int lengthVal, widthVal, yVal, xVal;

  JButton button = (JButton) event.getSource () ;

  String textX = x.getText();
  String textY = y.getText();
  String textLength = length.getText();
  String textWidth = width.getText();

  xVal = Integer.parseInt(textX);
  yVal = Integer.parseInt(textY);
  lengthVal = Integer.parseInt(textLength);
  widthVal = Integer.parseInt(textWidth);

  if(button.getText().equals("Add a shape")){
    if(count<shapeArray.length){
      shapeArray[count] = new MapShapes(lengthVal, widthVal, xVal, yVal);
      count++;
    } // end of nested if block;

   } // End of if block;

  repaint();

}// end of method;

}// end of private class;

}// end of class;
/**  This class makes the rectangles for the Map program. ;
 * @author: James ;
*/

import java.util.*; // So that we can use the Random class.
import java.awt.*; // So that we can use the Color class and the Graphics class.

 /** This class makes rectangles.*/
  public class MapShapes{

 private int x;
 private int y;
 private int length;
 private int width;
 private Color colour;

public MapShapes(int length, int width, int x, int y){
this.length = length;
this.width = width;
this.x = x;
this.y = y;
this.colour = new Color(randInt(0,254), randInt(0,254), randInt(0,254));               
 }

  /** This randInt(int lowest, int highest) method:
* Returns a random value within particular limits to instantiate the colour data field.
* */
 public int randInt( int lowest, int highest){
Random generator = new Random(); 
int randomNum = generator.nextInt(highest - lowest) + lowest;
return randomNum;
}

public void display(Graphics j){
  j.setColor(colour);
  j.fillRect(x, y, length, width);
}
}// end of class;