我有以下问题。我想在自定义jpanel上绘制一些东西,然后在绝对位置设置自定义按钮。这些按钮应该听取点击然后提供他们的位置。但我尝试的一切都没有用。当我使用add();
时,按钮不显示 public class IslePanel extends JPanel {
private static final long serialVersionUID = 1L;
private Color backgroundColor = new Color(90, 74, 66);
private Color strokeColor = new Color(254, 254, 254);
private Color lightGrayColor = new Color(220, 220, 220);
/**
* Constructor of the GameView
*
* @param game
* @param controller
*/
public IslePanel() {
setBackground(backgroundColor);
SiteButton sb = new SiteButton();
sb.setXPos(100);
sb.setYPos(100);
sb.setSiteRadius(20);
add(sb);
//example: i plan to draw a circle formed button on absolute Position (100,100) radius 20
}
/**
* paintComponent-method for ALL drawing functions
*
* @param Graphics
* g
*
**/
public void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2 = (Graphics2D) g;
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
g2.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR);
try {
drawTile(g, tile);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
sitebutton superclass:
public abstract class CustomButton extends JPanel implements MouseListener {
private Vector listeners = null;
boolean hit = false;
public CustomButton(String title) {
super();
this.title = title;
listeners = new Vector();
addMouseListener(this);
}
// public Dimension getPreferredSize() {
// return new Dimension(120, 80);
// }
public void mousePressed(MouseEvent e) {
}
public void mouseReleased(MouseEvent e) {
}
public void mouseClicked(MouseEvent e) {
fireEvent(new ActionEvent(this, 0, title));
System.out.println("bauen");
}
public void mouseEntered(MouseEvent e) {
}
public void mouseExited(MouseEvent e) {
}
public void addActionListener(ActionListener listener) {
listeners.addElement(listener);
}
public void removeActionListener(ActionListener listener) {
listeners.removeElement(listener);
}
private void fireEvent(ActionEvent event) {
for (int i = 0; i < listeners.size(); i++) {
ActionListener listener = (ActionListener) listeners.elementAt(i);
listener.actionPerformed(event);
}
;
}
}
按钮类:
public class SiteButton extends CustomButton {
/**
*
*/
private static final long serialVersionUID = 1L;
private double xpos,ypos;
private Site site;
private int siteRadius;
@Override
public void paintComponent(Graphics g) {
// super.paintComponent(g);
Graphics2D g2D = (Graphics2D) g;
g2D.setColor(Color.BLACK);
g2D.fillOval((int)xpos-siteRadius/2, (int)ypos-siteRadius/2, siteRadius, siteRadius);
}
@Override
public void mouseClicked(MouseEvent e) {
System.out.println(""+xpos + ypos);
}
答案 0 :(得分:1)
使用ActionListener
而不是MouseListener
可能会更好。它不仅可以为您节省几行,而且似乎总能为我工作。 mouseClicked
仅在用户单击按钮上的LMB时调用(显然导致按钮被按下,但不是直接按钮)。只要用户在按钮上执行actionPerformed
(即直接单击按钮),就会调用ActionEvent
。
更不用说,按钮默认情况下触发ActionEvents
The MouseListener is intended to determine if you're holding down the mouse button, if you've released the mouse button, or if you've just "clicked" (or double clicked) the mouse button - regardless of where the mouse is in the listening component.
此外,如果您在按钮上单击LMB,然后在释放LMB之前拖动鼠标,则按钮仍将执行。
关于您的问题,您是否尝试过使用revalidate();
?
revalidate()
使每个组件无效,然后验证它们。
问题是,当您立即致电JPanel.add(button);
时invalidate
Container
,或者JPanel
。要解决此问题,您可以致电revalidate();
或JPanel.validate();
。不同之处在于revalidate()
,就像我之前所说的那样,针对 每个 组件而不是validate()
制定了 一个 组件。