我正在尝试创建一个GUI,作为其中的一部分,我想在创建一个类的新实例时让JPanel获得一个图像。
通过按下addBox.addActionListener(new addParcelListener(session,1));
按钮创建新的类实例,在此按钮中标识要使用的Imageicon icon = new ImageIcon(this.getClass().getResource("images/box.png"));
。
如果bay1.addImage(icon)
位于ImageIcon
内,我可以如何制作public void actionPerformed(ActionEvent event)
?
主要代码
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");}
}
}
公共类Box Code
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谢谢你下次我提问时我会记住你的注意事项
答案 0 :(得分:1)
由于您的代码无法编译,因此很难向您发送可编译的答案。
问题是JPanel bay1 = new JPanel();是一个局部变量,actionPerformed没有它的可见性。
改为提取JPanel bay1 = new JPanel();作为一个领域:
public class DispatchDepot
{
final private JPanel bay1 = new JPanel();
您可以在私人内部班级中看到bay1。