3
Q
复杂的按键布局
A
回答
1
我意识到这个问题,有人问相当长一段时间以前,但我会创建呈现为按钮,然后检查被点击图像的哪一部分的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。如果不是内部的,但它是的较大的椭圆形里面,那么我们就知道点击在擂台。然后我们检查看看椭圆的哪一侧被点击并通知适当的动作监听器。
此外,请注意:PERCENT_PADDING_MIDDLE
和PERCENT_PADDING_EDGE
是特定于您链接到的图像,并假定图像周围有相同的填充。这取决于图像上填充像素的数量并将其除以图像的宽度。
相关问题
- 1. Android布局复杂按钮
- 2. 复杂的布局
- 3. 构建复杂的布局
- 4. Android的复杂布局
- 5. WPF - 复杂TextBlock的布局
- 6. 复杂heirarchical树布局
- 7. Android复杂表格布局
- 8. Android(复杂)日历布局
- 9. Ruby GUI(非复杂布局)
- 10. 对象复杂的按键
- 11. 引导程序中的复杂布局
- 12. 应对Android上的复杂布局
- 13. Android:需要复杂布局的指导
- 14. 如何实现复杂的CSS布局?
- 15. 复杂布局的ViewController设计问题
- 16. 复杂的表或ListView布局
- 17. 复杂的页面布局和设计
- 18. 复杂的布局绝对定位
- 19. 具有复杂数据和每行复杂布局的复杂ListView示例?
- 20. Dom遍历复杂表格布局
- 21. Twitter Bootstrap复杂表单布局
- 22. 碎片与单一复杂布局
- 23. 如何从复杂的按键
- 24. Android - 如何膨胀复杂的布局并添加到其他布局?
- 25. 按键布局没有显示
- 26. 如何在Android的一个复杂的Layout布局
- 27. 复杂的面向列的表格布局
- 28. QT:复杂的布局和无缝的窗口背景图像
- 29. Android的复杂播放器的布局设计
- 30. Ajax驱动的布局复杂的RIA和MVC框架
你能否找到你的换档键,并始终如一地用它来开始句子和单词'I'?我试着阅读那些乱七八糟的信件,但一直迷迷糊糊。另外*“.. surimposed按钮..”*要么仔细打字或得到一个拼写检查。 –
顺便说一句 - 那形象是一个按钮,但你一直提到“按钮”(复数 - 很多)。 DYM单个按钮还是多个按钮? –
顺便说一句,感谢真棒教程:)) – COD3BOY