我正在尝试让我的zoomIn / zoomOut按钮在我的GUI上运行。我有2个(我有其他但不需要发布)类和actionlistener /按钮在一个,以及缩放方法。
按下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);
}
答案 0 :(得分: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这样长的变量,但这只是为了展示如何使用对象。由您决定如何使用变量。我希望这会有所帮助。