gpt4 book ai didi

java - 复杂的按钮布局

转载 作者:塔克拉玛干 更新时间:2023-11-02 20:14:38 25 4
gpt4 key购买 nike

我想实现一个看起来像这样的按钮 button :

Round button w/metallic border

但将外环分成四个部分,这样我就有四个图像来制作它的按钮,加上中间图像作为中间按钮。对于四个按钮的划分,想象有一个十字或一个 X。我该如何布局按钮才能实现这样的组件?

我尝试使用 BorderLayoutGridBagLayout,但是由于 Swing 按钮的矩形形状导致每个按钮图像之间的空间太大,因此看起来不太好。我现在正在考虑 JLayeredPane 来叠加按钮,但我认为会有一个问题,因为如果另一个按钮在按钮上,按钮的某些部分将无法点击。

是否可以用我想要的功能(5 个按钮)实现这种形状的组件?

最佳答案

我意识到很久以前就有人问过这个问题,但我会创建一个 JComponent 来呈现该按钮,然后检查图像的哪一部分被单击了。像这样:

import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.geom.Ellipse2D;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.LinkedList;
import java.util.List;

import javax.swing.JComponent;
import javax.swing.SwingConstants;

public class FivePartCircleButton extends JComponent implements SwingConstants
{
private static final float PERCENT_PADDING_MIDDLE = 0.1571428571428571f;
private static final float PERCENT_PADDING_EDGE = 0.0535714285714286f;
private static Image button;

private List<ActionListener> topListeners = new LinkedList<ActionListener>();
private List<ActionListener> rightListeners = new LinkedList<ActionListener>();
private List<ActionListener> bottomListeners = new LinkedList<ActionListener>();
private List<ActionListener> leftListeners = new LinkedList<ActionListener>();
private List<ActionListener> middleListeners = new LinkedList<ActionListener>();
private String actionCommand;

public FivePartCircleButton()
{
try
{
if (button == null)
button = Toolkit.getDefaultToolkit().createImage(new URL("http://mygimptutorial.com/preview/round-web20-button-with-metal-ring.jpg"));
}
catch (MalformedURLException e) {e.printStackTrace();}

this.setPreferredSize(new Dimension(280, 280));
this.addMouseListener(mouseAdapter);
}

private MouseAdapter mouseAdapter = new MouseAdapter()
{
@Override
public void mouseClicked(MouseEvent e)
{
Ellipse2D innerCircle = getShapeOfOval(PERCENT_PADDING_MIDDLE);
Ellipse2D outerCircle = getShapeOfOval(PERCENT_PADDING_EDGE);

if (innerCircle.contains(e.getPoint())) //clicked in the inner circle
processClick(middleListeners);
else if (outerCircle.contains(e.getPoint())) //clicked in the outer ring
{
float lineFromTopLeftToBottomRight = e.getY() * ((float)getWidth() / (float)getHeight()); //if we split this button diagonally (top left to bottom right), then this is the x position of that line at this y point
float lineFromTopRightToBottomLeft = getWidth() - lineFromTopLeftToBottomRight; // the same line as tlBrDividerX but mirrored

if (e.getX() < lineFromTopLeftToBottomRight) //clicked on the bottom left half of the ring
{
if (e.getX() < lineFromTopRightToBottomLeft) //clicked on the left quadrant of the ring
processClick(leftListeners);
else //clicked on the bottom quadrant of the ring
processClick(bottomListeners);
}
else //clicked on the top right half of the ring
{
if (e.getX() < lineFromTopRightToBottomLeft) //clicked on the top quadrant of the ring
processClick(topListeners);
else //clicked on the right quadrant of the ring
processClick(rightListeners);
}
}
}
};

/**
* Informs all of the listeners that an action has been performed
* @param listeners - which set of listeners to inform
*/
private void processClick(List<ActionListener> listeners)
{
for (ActionListener l : listeners)
l.actionPerformed(new ActionEvent(this, ActionEvent.ACTION_PERFORMED, actionCommand));
}

/**
* @param listener - the listener to add
* @param side - one of SwingConstants.TOP, SwingConstants.RIGHT, SwingConstants.BOTTOM, SwingConstants.LEFT, SwingConstants.CENTER,
*/
public void addActionListener(ActionListener listener, int side)
{
switch (side)
{
case TOP:
topListeners.add(listener);
break;
case RIGHT:
rightListeners.add(listener);
break;
case BOTTOM:
bottomListeners.add(listener);
break;
case LEFT:
leftListeners.add(listener);
break;
case CENTER:
middleListeners.add(listener);
break;
}
}

/**
* Creates an oval based on the size of this component with the given padding percentage
* @param percentPadding
* @return an oval with the given padding
*/
private Ellipse2D getShapeOfOval(float percentPadding)
{
float x = getWidth() * percentPadding;
float y = getHeight() * percentPadding;
float w = getWidth() - x - x;
float h = getHeight() - y - y;

Ellipse2D circle = new Ellipse2D.Float(x, y, w, h);
return circle;
}

@Override
protected void paintComponent(Graphics g)
{
g.drawImage(button, 0, 0, this.getWidth(), this.getHeight(), this);
}

/**
* Sets the action command for this button.
* @param actionCommand - the action command for this button
*/
public void setActionCommand(String actionCommand)
{
this.actionCommand = actionCommand;
}
}

简而言之,按钮绘制为您链接到的图像。然后,当您单击图像时,它会检查您单击了图像的哪一部分。这是通过创建两个与图像上的椭圆相匹配的椭圆来完成的。如果您单击的点在较小的椭圆内,那么它会通知中间的 ActionListeners。如果它不在那个里面,但它更大的椭圆里面,那么我们就知道点击是在环中。然后我们检查椭圆的哪一侧被点击并通知适当的 Action 监听器。

另外,请注意:PERCENT_PADDING_MIDDLEPERCENT_PADDING_EDGE 特定于您链接到的图像,并假设图像周围的填充相等。这是通过将图像上的填充像素数除以图像宽度来确定的。

关于java - 复杂的按钮布局,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8932940/

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