Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/user-interface/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 从另一个类向另一个类中的操作侦听器调用方法_Java_User Interface_Actionlistener - Fatal编程技术网

Java 从另一个类向另一个类中的操作侦听器调用方法

Java 从另一个类向另一个类中的操作侦听器调用方法,java,user-interface,actionlistener,Java,User Interface,Actionlistener,我正在尝试使我的缩放/缩放按钮在GUI上工作。我有2个类(我还有其他类,但不需要发布),actionlistener/按钮在一个类中,zoom方法 按下zoomIn/zoomOut按钮时,调用缩放方法时遇到问题 第1课:(我已经导入GUI了,不想占用这里的空间) @SuppressWarnings(“串行”) 公共类PartyParkingGUIPanel扩展JPanel实现ActionListener { 专用最终整数默认值_宽度=800; 专用最终int默认值_高度=600; 私有最终整数默

我正在尝试使我的缩放/缩放按钮在GUI上工作。我有2个类(我还有其他类,但不需要发布),actionlistener/按钮在一个类中,zoom方法

按下zoomIn/zoomOut按钮时,调用缩放方法时遇到问题

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

@SuppressWarnings(“串行”)
公共类PartyParkingGUIPanel扩展JPanel实现ActionListener
{
专用最终整数默认值_宽度=800;
专用最终int默认值_高度=600;
私有最终整数默认网格宽度=默认高度-50;
//面板
private JPanel mapppanel=new JPanel();
private JPanel inputPanel=new JPanel();
private JPanel setupPanel=new JPanel();
private JPanel statusPanel=new JPanel();
私有JPanel setupButtonsPanel=新JPanel();
私有JPanel setupTextFieldPanel=新JPanel();
private JPanel controlPanel=new JPanel();
//标签
私有JLabel gridSizeLabel=新JLabel(“网格大小:”);
私人JLabel numCarsLabel=新JLabel(“#辆车:”);
私人JLabel numParkingSpotsLabel=新JLabel(“停车位”);
//钮扣
私有JButton randomGrid=新JButton(“随机网格”);
私有JButton readFile=新JButton(“读取文件”);
私有JButton resetGrid=新JButton(“重置网格”);
私有JButton zoomIn=新JButton(“放大”);
私有JButton zoomOut=新JButton(“缩小”);
私有JButton start=新JButton(“start”);
私有JButton步骤=新JButton(“步骤”);
私有JButton pause=新JButton(“pause”);
//带滚动窗格的文本区域和城市地图
私人JTextArea状态=新JTextArea(“输入网格大小,#车辆,#停车位”+
“\n单击“随机”按钮生成随机车辆和点”+
“\n单击重置网格以清除网格”,20,20);
私人城市地图城市地图;
私有JScrollPane mapScroller=新JScrollPane(城市地图);
私有JScrollPane statusScroller=新JScrollPane(状态);
//文本字段
私有JTextField gridSizeField=新JTextField(“10”,12);
私有JTextField numParkingSpotsField=新JTextField(“0”,12);
私有JTextField numCarsField=新JTextField(“0”,12);
private int gridSize=ParkingSimulation.DEFAULT_GRID_SIZE;
私人国际货币基金组织;
私人int numSpots;
私家车;
私人停车模拟模拟器;
私有布尔显示路径=true;
私有整数速度因子=1;
私有布尔动画=false;
私有整数延迟=500;//毫秒
/**
*设置面板。
*/
公共PartyParkingGUIPanel(){
setPreferredSize(新尺寸(1200700));
模拟器=新的驻车模拟();
模拟器.setGridSize(gridSize);
cars=模拟器.getCars();
城市地图=新城市地图(ParkingSimulation.DEFAULT\u GRID\u SIZE,DEFAULT\u GRID\u WIDTH,simulator);
城市地图设置展示路径(展示路径);
zoomIn.addActionListener(此);
zoomOut.addActionListener(此);
startAnimation();
}
....
/*(非Javadoc)
*@see java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent)
*/
@凌驾
已执行的公共无效操作(操作事件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);
}
公共类城市地图扩展了JPanel
{
私有整数网格大小;
私有最终整数默认网格宽度=1024;
private int displaySize=默认网格宽度;
私有整数块大小;
私家车大小;
私有整数偏移;
私用锁;
私人停车模拟模拟器;
私家车;
私人点阵;
私有布尔显示路径=false;
私有布尔标志=true;
私有最终整数路径_厚度=2;
/**
*画一个城市网格。
*@param gridSize
*@param displaySize
*/
公共城市地图(整数网格大小、整数显示大小、停车模拟模拟器)
{
超级();
this.simulator=模拟器;
this.cars=simulator.getCars();
this.spots=simulator.getSpots();
this.displaySize=displaySize;
this.gridSize=gridSize;
设置参数(网格大小);
挫折地面(颜色:白色);
setPreferredSize(新维度(displaySize,displaySize));
setDoubleBuffered(true);//使绘图更平滑
}
/**
*设置用于绘制栅格的参数
*@param大小
*/
私有void setParameters(int size)
{
numBlocks=大小;
blockSize=displaySize/(numBlocks+1);
偏移量=块大小;
carSize=blockSize/4;
}
/**
*按系数缩放(1及更高)
*/
公共空间缩放(整数因子){
设置参数(网格大小/系数);
}

好的,我注意到两个类都扩展了JPanel。我不完全确定,但我认为这可能会在程序中导致某种形式的运行时错误,因为它会产生干扰,但如果这不是您的问题,请忽略它。为了让您从一个类到另一个类调用方法或任何公共对象,您必须创建一个实例v该类的变量,或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这样的长变量,但这只是为了说明如何使用对象。如何使用变量取决于您。我希望这会有所帮助。

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

  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这样的长变量,但这只是为了说明如何使用对象。如何使用变量取决于您。我希望这会有所帮助。

请解释两个JPanel扩展类如何“相互干扰”,因为我对此一点都不熟悉。我想