我是第一次与Swing合作,但遇到了一些问题。我所拥有的是一个分为四个JPanels的JFrame。 JFrame上有一个MouseListener,其作用如下单击时,如果单击位于左侧栏中,请确定选择了13个图标中的哪一个。如果单击位于右侧“游戏窗格”内并且已选择一个图标,则将其放置在单击的位置。在这里完成@Overridepublic void mouseClicked(MouseEvent event) { // TODO Auto-generated method stub xPos = event.getX(); yPos = event.getY()-25; //If click is inside tool bar if(xPos<=75){ if(yPos>-1 && yPos<48) //First tool image image = new ImageIcon(getClass().getResource(_image path_)).getImage(); else if(yPos>=48 && yPos<96) //Second tool image image = new ImageIcon(getClass().getResource(_image path_)).getImage(); else if(yPos>=96 && yPos<144) //Third tool image image = new ImageIcon(getClass().getResource(_image path_)).getImage(); else if(yPos>=144 && yPos<192) //Fourth tool image image = new ImageIcon(getClass().getResource(imagepath)).getImage(); else if(yPos>=192 && yPos<240) //Fifth tool image image = new ImageIcon(getClass().getResource(imagepath)).getImage(); else if(yPos>=240 && yPos<288) //Sixth tool image image = new ImageIcon(getClass().getResource(imagepath)).getImage(); else if(yPos>=288 && yPos<336) //Seventh tool image image = new ImageIcon(getClass().getResource(imagepath)).getImage(); else if(yPos>=336 && yPos<384) //First NPC image image = new ImageIcon(getClass().getResource(imagepath)).getImage(); else if(yPos>=384 && yPos<432) //second NPC image image = new ImageIcon(getClass().getResource(imagepath)).getImage(); else if(yPos>=432 && yPos<480) //Third NPC image image = new ImageIcon(getClass().getResource(imagepath)).getImage(); else if(yPos>=480 && yPos<528) //First Decoration image image = new ImageIcon(getClass().getResource(imagepath)).getImage(); else if(yPos>=528 && yPos<576) //Second Decoration image image = new ImageIcon(getClass().getResource(imagepath)).getImage(); else if(yPos>=576 && yPos<=625) //Third Decoration image image = new ImageIcon(getClass().getResource(imagepath)).getImage(); } //If click is within Game Pane else if (xPos>75 && yPos<625){ //A tool has been selected if(image!=null){ placedTool = this.image; this.image = null; placeable = true; } } //An image and location on the game pane has been selected if(placeable && this.image==null){ ImageInfo newImg = new ImageInfo(xPos, yPos, image); gamePane.additions.add(newImg); gamePane.repaint(); System.out.println("IMAGE PLACED @ " + xPos + ", " + yPos); placeable = false; } System.out.println("CLICK: (" + xPos + "," + yPos +")");}其中imagepath是50x50图标的路径。此部分正常工作,没有错误。但是gamePane无法正确重新绘制。gamePane现在仅具有背景图像。添加组件后,应该将它们绘制在顶部。绘制的只是背景图像。有没有办法使用Graphics.drawImage()指定Z组件;这是我对gamePane的paintComponent函数所拥有的(被合并,因为这是主要问题)@Overrideprotected void paintComponent(Graphics g) { g.drawImage(backgroundImg, 0, 0, null); for(ImageInfo add : additions){ g.drawImage(add.getImage(), add.getX(), add.getY(), null); }}这样定义加法的地方列表添加= new ArrayList();并且ImageInfo类仅包含图像,x坐标和y坐标public class ImageInfo {private int x;private int y;private Image image;public int getX() { return x;}public int getY() { return y;}public Image getImage() { return image;}public ImageInfo(int x, int y, Image image) { super(); this.x = x; this.y = y; this.image = image;}}固定:谢谢你mKorbel。通过在mouseClicked方法之外定义所有图像Image tool1 = new ImageIcon(getClass().getResource(toolBar.TOOL1)).getImage();Image tool2 = new ImageIcon(getClass().getResource(toolBar.TOOL2)).getImage();Image tool3 = new ImageIcon(getClass().getResource(toolBar.TOOL3)).getImage();Image tool4 = new ImageIcon(getClass().getResource(toolBar.TOOL4)).getImage();Image tool5 = new ImageIcon(getClass().getResource(toolBar.TOOL5)).getImage();Image tool6 = new ImageIcon(getClass().getResource(toolBar.TOOL6)).getImage();Image tool7 = new ImageIcon(getClass().getResource(toolBar.TOOL7)).getImage();Image npc1 = new ImageIcon(getClass().getResource(toolBar.NPC1)).getImage();Image npc2 = new ImageIcon(getClass().getResource(toolBar.NPC2)).getImage();Image npc3 = new ImageIcon(getClass().getResource(toolBar.NPC3)).getImage();Image decor1 = new ImageIcon(getClass().getResource(toolBar.DECOR1)).getImage();Image decor2 = new ImageIcon(getClass().getResource(toolBar.DECOR2)).getImage();Image decor3 = new ImageIcon(getClass().getResource(toolBar.DECOR3)).getImage();和执行mouseClicked函数@Overridepublic void mouseClicked(MouseEvent event) { // TODO Auto-generated method stub xPos = event.getX(); yPos = event.getY()-25; //If click is inside tool bar if(xPos<=75){ if(yPos>-1 && yPos<48) //First tool image image = tool1; else if(yPos>=48 && yPos<96) //Second tool image image = tool2; else if(yPos>=96 && yPos<144) //Third tool image image = tool3; else if(yPos>=144 && yPos<192) //Fourth tool image image = tool4; else if(yPos>=192 && yPos<240) //Fifth tool image image = tool5; else if(yPos>=240 && yPos<288) //Sixth tool image image = tool6; else if(yPos>=288 && yPos<336) //Seventh tool image image = tool7; else if(yPos>=336 && yPos<384) //First NPC image image = npc1; else if(yPos>=384 && yPos<432) //second NPC image image = npc2; else if(yPos>=432 && yPos<480) //Third NPC image image = npc3; else if(yPos>=480 && yPos<528) //First Yard Decoration image image = decor1; else if(yPos>=528 && yPos<576) //Second Yard Decoration image image = decor2; else if(yPos>=576 && yPos<=625) //Third Yard Decoration image image = decor3; } //If click is within Game Pane else if (xPos>75 && yPos<625){ //A tool has been selected if(image!=null){ placedTool = this.image; this.image = null; placeable = true; } } if(placeable && this.image==null){ GamePiece newImg = new GamePiece(placedTool, xPos, yPos); gamePane.additions.add(newImg); gamePane.repaint(); System.out.println("IMAGE PLACED @ " + xPos + ", " + yPos); placeable = false; } System.out.println("CLICK: (" + xPos + "," + yPos +")");}通过先前提供的paintComponent方法,将图像添加到背景图像的顶部。它们的位置有些偏离,但仍然可见。 最佳答案 只有评论发布SSCCE短期可运行,可编译为什么会有yPos = event.getY()-25;表示integer at -25的逻辑将所有ImageIcons存储在局部变量中,将那些ImageIcons存储在任何数组或List中,如果存在固定逻辑而不是获取坐标,然后加载用image = new ImageIcon(getClass().getResource(绘制的paintComponent,则不提供任何在运行时创建由FileIO放置的JLabel的网格,将GridLayout添加到每个MouseListener,从JLabel更改JLabel.setIcon(myImageIcon),其余逻辑在没有SSCCE的情况下会丢失例如import java.awt.BorderLayout;import java.awt.Component;import java.awt.GridLayout;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;import java.awt.event.MouseAdapter;import java.awt.event.MouseEvent;import javax.swing.Icon;import javax.swing.JButton;import javax.swing.JFrame;import javax.swing.JLabel;import javax.swing.JPanel;import javax.swing.UIManager;public class ChessBoard extends JFrame { private JFrame frame = new JFrame(); private JPanel panel = new JPanel(); private Icon errorIcon = UIManager.getIcon("OptionPane.errorIcon"); private Icon infoIcon = UIManager.getIcon("OptionPane.informationIcon"); private Icon warnIcon = UIManager.getIcon("OptionPane.warningIcon"); private Icon questnIcon = UIManager.getIcon("OptionPane.questionIcon"); private JButton button = new JButton("Reset my board"); public ChessBoard() { panel.setLayout(new GridLayout(8, 8, 0, 0)); for (int i = 0; i < 64; i++) { final JLabel label = new JLabel(); label.setIcon(errorIcon); label.addMouseListener(new MouseAdapter() { @Override public void mouseClicked(MouseEvent e) { if (e.getButton() == MouseEvent.BUTTON3) { label.setIcon(infoIcon); } else { label.setIcon(warnIcon); } } @Override public void mouseEntered(MouseEvent me) { label.setIcon(questnIcon); } }); panel.add(label); } button.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { for (Component c : panel.getComponents()) { if (c instanceof JLabel) { JLabel label = (JLabel) c; label.setIcon(errorIcon); } } } }); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.add(panel); frame.add(button, BorderLayout.SOUTH); frame.pack(); frame.setVisible(true); } public static void main(String[] args) { java.awt.EventQueue.invokeLater(new Runnable() { @Override public void run() { new ChessBoard(); } }); }}
10-01 00:18