gpt4 book ai didi

java - 在自定义 JPanel 上添加按钮

转载 作者:行者123 更新时间:2023-12-01 12:07:08 28 4
gpt4 key购买 nike

我有以下问题。我想在自定义 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 父类(super class):

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);
}

最佳答案

您最好使用 ActionListenerMouseListener 。它不仅会节省你几行代码,而且似乎总是对我有用。 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()它仅作用于一个组件。

关于java - 在自定义 JPanel 上添加按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27511977/

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