gpt4 book ai didi

java - 使 JPanel 图像从 actionPerformed() 事件发生变化

转载 作者:行者123 更新时间:2023-11-30 11:03:30 24 4
gpt4 key购买 nike

我正在尝试制作一个 GUI,作为其中的一部分,我想让 JPanel 在创建类的新实例时获得图像。按下按钮 addBox.addActionListener(new addParcelListener(session,1)); 创建新的类实例,在此期间它标识要使用的 Imageicon icon = new ImageIcon(this.getClass ().getResource("images/box.png"));.

ImageIcon 位于 public void actionPerformed(ActionEvent event) 内时,我将如何制作它以便我可以执行 bay1.addImage(icon) ?

主要代码

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

public class DispatchDepot
{
public static void main(String[] args)
{
DispatchDepot mainScreen = new DispatchDepot();
mainScreen.createGUI();
}
private void createGUI()
{
DepotPackages session = new DepotPackages();
//Main Window
JFrame theDepot = new JFrame("Dispatch Depot");
theDepot.setResizable(false);
theDepot.setLayout(new BorderLayout());
//Button Panel
JPanel buttonPanel = new JPanel();
buttonPanel.setLayout(new GridLayout(8, 1));
buttonPanel.setPreferredSize(new Dimension(200, 800));
buttonPanel.setMaximumSize(new Dimension(200, 800));
//Buttons
JButton addBox = new JButton("Add a Box");
buttonPanel.add(addBox);
theDepot.add(buttonPanel, BorderLayout.EAST);
//Bay Panel
JPanel bayPanel = new JPanel();
bayPanel.setLayout(new GridLayout(2, 3));
bayPanel.setPreferredSize(new Dimension(800, 800));
bayPanel.setMaximumSize(new Dimension(800, 800));
Border bayBorder = new LineBorder(Color.BLUE, 2);
//Bay 1
JPanel bay1 = new JPanel();
JLabel label1 = new JLabel("Bay 1", JLabel.CENTER);
bay1.setName("Bay 1");
bay1.setBorder(bayBorder);
bay1.add(label1);
//GUI Construction
bayPanel.add(bay1);
theDepot.add(bayPanel, BorderLayout.WEST);
//Display GUI
theDepot.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
theDepot.pack();
theDepot.setLocationRelativeTo(null);
theDepot.setVisible(true);
//Event Listeners - Buttons
addBox.addActionListener(new addParcelListener(session,1));
}
private class addParcelListener implements ActionListener
{
final private DepotPackages session;
final private int type;
public addParcelListener(DepotPackages session, int type)
{
this.session=session;
this.type=type;
}
@Override
public void actionPerformed(ActionEvent event)
{
if(session.checkBaySpace())
{
int zone = zoneEntryGUI();
if(session.checkZoneSpace(zone))
{
char zoneChar;
switch(zone)
{
case 1: zoneChar = 'a';break;
case 2: zoneChar = 'b';break;
default: zoneChar = 'c';break;
}
int id = idEntryGUI();
int bay;
ImageIcon icon;
switch(type)
{

case 1: {Parcel parcel = boxEntryGUI(id,zoneChar); icon=parcel.getImage(); session.newEntry(zone, parcel); break;}
case 2: {Parcel parcel = tubeEntryGUI(id,zoneChar); icon=parcel.getImage(); session.newEntry(zone, parcel); break;}
default: {Parcel parcel = envelopeEntryGUI(id,zoneChar); icon=parcel.getImage(); session.newEntry(zone, parcel); break;}
}
}
else{System.out.println("No Zone Space");}
}
else{System.out.println("No Bay Space");}
}
}

公共(public)课箱码

public ImageIcon getImage()
{
ImageIcon icon;
switch(this.zone)
{
case 'a': {icon = new ImageIcon(this.getClass().getResource("images/box.png")); break;}
case 'b': {icon = new ImageIcon(this.getClass().getResource("images/box.png")); break;}
case 'c': {icon = new ImageIcon(this.getClass().getResource("images/box-large.png")); break;}
default: {icon=null; break;}
}
return icon;
}

如果您觉得我的问题难以理解,我深表歉意。谢谢

谢谢 Ezequiel 和 user1803551,您的回答非常有用@Andrew Thompson 谢谢你,下次我提问时我会记住你的笔记

最佳答案

由于您的代码不可编译,因此很难向您发布可编译的答案。

问题是 JPanel bay1 = new JPanel();是局部变量,actionPerformed 对其不可见。

而是提取 JPanel bay1 = new JPanel();作为一个领域:

public class DispatchDepot
{
final private JPanel bay1 = new JPanel();

并且您将在私有(private)内部类中看到 bay1。

关于java - 使 JPanel 图像从 actionPerformed() 事件发生变化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30353152/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com