如果这是一个非常简单的解决方案或愚蠢的错误,请原谅我 - 这是我第一次尝试用Java实现图形! :)
我正在尝试制作一块瓷砖板,每块瓷砖都是一个瓷砖对象,瓷砖的所有位置都存储在一个名为'content'(content [] [] [])的三层Tiles中。
为了使每个瓷砖“可点击”,我基本上是为每个瓷砖图标创建一个标签,并根据瓷砖对象的x,y坐标将该瓷砖定位在电路板上。我为内容数组中的每个非null Tile对象执行此操作。
当我使用graphics.drawImage函数时,这工作正常,但是当我使用setBorders()函数定位每个标签时:
和
我正在调用的函数的代码是:
public void paintComponent(Graphics graphics) {
// let superclass paint to fill in background
super.paintComponent(graphics);
Tile[][][] content = b.getContent();
if (content==null || tileImages==null) {
return;
}
/* Set dummy previous label */
prevT.setBounds(-1,-1,1,1);
// draw tiles back to front
for (int i = 0; i<content.length; i++) {
for (int y = 0; y<content[i].length; y++) {
for (int x = 0; x<content[i][y].length; x++) {
final Tile t = content[i][y][x];
if (t!=null) {
if (y>0 && content[i][y-1][x]!=null && t.equals(content[i][y-1][x])) {
continue;
}
if (x>0 && content[i][y][x-1]!=null && t.equals(content[i][y][x-1])) {
continue;
}
Image image = tileImages[t.getValue()][t.getSubindex()];
if (b.free(t)) {
image = tileImagesHL[t.getValue()][t.getSubindex()];
}
/* Mouse event magic */
graphics.drawImage(image, x*TILEW+TILEW/2+i*TILESKEW, (y+1)*TILEH/2-i*TILESKEW, null);
/* Create icon to be displayed */
ImageIcon icon = new ImageIcon(image);
/* Label that acts as the clickable tile */
final JLabel label = new JLabel();
/* Associate image with label */
label.setIcon(icon);
/* Allow mouse events to interact with label */
label.addMouseListener(this);
/* Position labels according to tile coordinates */
label.setBounds(x*TILEW+TILEW/2+i*TILESKEW, (y+1)*TILEH/2-i*TILESKEW, image.getWidth(null), image.getHeight(null));
/* Associate label with specified tile */
t.setLabel(label);
/* Add label to list*/
labels.add(label);
this.setVisible(true);
this.add(label);
}
}
}
}
}
对于为什么会发生这种情况的任何解释都将非常感激!我已经尝试多次重写这个功能,而且我没有想法!
谢谢! :)
答案 0 :(得分:3)
1)不要在Object
内创建paintComponent(Graphics graphics)
,在
Object
我的观点,简单的集合鼠标事件魔法没有MouseListener
2)创建JPanel
并放置JButton#setBackground(someColor)
,然后添加JButton#setRolloverIcon(someIcon)
,不再需要任何MouseListener
3)如果您创建Tiles,则查找GridLayout
4)准备Icons
并将这些Icon
添加到JButton
,其他任何地方,Array
Objects
{}},没有比我描述的更多的代码< / p>
import java.awt.*;
import javax.swing.*;
public class TilesWithButton {
private Icon warningIcon = UIManager.getIcon("OptionPane.warningIcon");
private Icon infoIcon = UIManager.getIcon("OptionPane.informationIcon");
private Icon errorIconRoll = UIManager.getIcon("OptionPane.errorIcon");
private JPanel myPanel = new JPanel();
public TilesWithButton() {
myPanel.setLayout(new GridLayout(1, 2, 1, 1));
JButton btn = new JButton();
btn.setBackground(Color.white);
btn.setIcon(infoIcon);
btn.setRolloverIcon(errorIconRoll);
btn.setFocusPainted(false);
myPanel.add(btn);
JButton btn1 = new JButton();
btn1.setBackground(Color.white);
btn1.setIcon(warningIcon);
btn1.setRolloverIcon(errorIconRoll);
btn1.setFocusPainted(false);
myPanel.add(btn1);
JFrame frame = new JFrame("Tiles");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(myPanel);
frame.pack();
frame.setLocation(150, 100);
frame.setVisible(true);
}
public static void main(final String[] args) {
java.awt.EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
TilesWithButton tilesWithButton = new TilesWithButton();
}
});
}
}