1

我试图让我的放大/缩小按钮在我的 GUI 上工作。我有 2 个(我有其他但不需要发布)类,动作监听器/按钮合二为一,还有缩放方法。

按下 zoomIn/zoomOut 按钮时,我无法调用缩放方法。

第 1 类:(我已经导入了 GUI,不想在这里占用空间)

@SuppressWarnings("serial")
public class PartyParkingGUIPanel extends JPanel implements ActionListener
{
private final int DEFAULT_WIDTH = 800;
private final int DEFAULT_HEIGHT = 600;
private final int DEFAULT_GRID_WIDTH = DEFAULT_HEIGHT - 50;

//panels
private JPanel mapPanel = new JPanel();
private JPanel inputPanel = new JPanel();
private JPanel setupPanel = new JPanel();
private JPanel statusPanel = new JPanel();
private JPanel setupButtonsPanel = new JPanel();
private JPanel setupTextFieldPanel = new JPanel();
private JPanel controlPanel = new JPanel();

//labels
private JLabel gridSizeLabel = new JLabel("grid size:");
private JLabel numCarsLabel = new JLabel("#of cars:");
private JLabel numParkingSpotsLabel = new JLabel("#parking spots:");

//buttons
private JButton randomGrid = new JButton("Random Grid");
private JButton readFile = new JButton("Read File");
private JButton resetGrid = new JButton("Reset Grid");
private JButton zoomIn = new JButton("Zoom In");
private JButton zoomOut = new JButton("Zoom Out");
private JButton start = new JButton("Start");
private JButton step = new JButton("Step");
private JButton pause = new JButton("Pause");


//text area and cityMap with scrollpane
private JTextArea status = new JTextArea("Enter grid size, #cars, #parking spots" +
        " \nClick on random button to generate random cars and spots" +
        " \nClick on reset grid to clear the grid",20, 20);
private CityMap cityMap;
private JScrollPane mapScroller = new JScrollPane(cityMap);
private JScrollPane statusScroller = new JScrollPane(status);

//text fields
private JTextField gridSizeField = new JTextField("10",12);
private JTextField numParkingSpotsField = new JTextField("0",12);
private JTextField numCarsField = new JTextField("0",12);

private int gridSize = ParkingSimulation.DEFAULT_GRID_SIZE;
private int numCars;
private int numSpots;
private ArrayList<Car> cars;
private ParkingSimulation simulator;

private boolean showPaths = true;
private int speedFactor = 1;
private boolean animate = false;
private int delay = 500; // milliseconds

/**
 * Set up the panel.
 */
public PartyParkingGUIPanel() {
    setPreferredSize(new Dimension(1200, 700));
    simulator = new ParkingSimulation();
    simulator.setGridSize(gridSize);
    cars = simulator.getCars();
    cityMap = new  CityMap(ParkingSimulation.DEFAULT_GRID_SIZE, DEFAULT_GRID_WIDTH, simulator);
    cityMap.setShowPaths(showPaths);

    zoomIn.addActionListener(this);
    zoomOut.addActionListener(this);


    startAnimation();
}


....

/* (non-Javadoc)
 * @see java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent)
 */
@Override
public void actionPerformed(ActionEvent e) 
{
    if(e.getSource() == zoomIn){
        ??
    }
}

....

具有缩放方法的 2 类:

 public class CityMap extends JPanel
{
private int gridSize;
private final int DEFAULT_GRID_WIDTH = 1024;
private int displaySize = DEFAULT_GRID_WIDTH;
private int blockSize;
private int carSize;
private int offset;
private int numBlocks;

private ParkingSimulation simulator;
private ArrayList<Car> cars;
private ArrayList<ParkingSpot> spots;

private boolean showPaths = false;
private boolean flag = true;
private final int PATH_THICKNESS = 2;


/**
 * Draw a city grid.
 * @param gridSize
 * @param displaySize
 */
public CityMap(int gridSize, int displaySize, ParkingSimulation simulator)
{
    super();
    this.simulator = simulator;
    this.cars = simulator.getCars();
    this.spots = simulator.getSpots();

    this.displaySize = displaySize;
    this.gridSize = gridSize;
    setParameters(gridSize);
    setBackground(Color.white);
    setPreferredSize(new Dimension(displaySize, displaySize));
    setDoubleBuffered(true); //makes drawing smoother
}


/**
 * Sets parameters for drawing the grid
 * @param size
 */
private void setParameters(int size) 
{
    numBlocks = size;
    blockSize = displaySize/(numBlocks + 1);
    offset = blockSize;
    carSize = blockSize/4;  
}


/**
 * Zoom by a factor (1 and higher)
 */
public void zoom(int factor) {
    setParameters(gridSize/factor);
}
4

1 回答 1

1

好的,我注意到的一件事是这两个类都扩展了 JPanel。我不完全确定,但我认为这可能会在程序中导致某种形式的运行时错误,因为它会产生干扰,但如果这不是你的问题,请忽略它。为了让您从一个类调用一个方法或任何公共的东西到另一个类,您必须创建该类的实例变量,或 AKA,该类的对象。为此,只需键入您正在使用的类的名称,例如

  CityMap objectOfCityMap = new CityMap();

然后这样做:

 @Override
 public void actionPerformed(ActionEvent e) 
 {
    if(e.getSource() == zoomIn){
        // using the object to access the method or function from the other class
        objectOfCityMap.zoom(// factor amount);
    }
 }

现在,通常,您不会有像 objectOfCityMap 这样长的变量,但这只是为了说明如何使用对象。如何使用变量取决于您。我希望这有帮助。

于 2013-05-05T22:29:45.547 回答