类(class)
class MyComponent extends JComponent {
public void paint(Graphics g) {
g.fillRect(30, 30, 100, 100);
}
}
Jbutton 对此执行操作
jButton2.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
jPanel4.add(new MyComponent());
addComponent(new MyComponent());
}
});
jButton1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
jPanel4.add(new MyComponent1());
addComponent(new MyComponent1());
}
});
我还有其他一些 Jbutton 操作。现在我想检测面板中的形状。然后我想对面板当前形状执行一些操作。
jButton5.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if(e.getSource()==jButton1)
{
jPanel4.removeAll();
jPanel4.updateUI();
jPanel4.add(new MyComponent11());
addComponent(new MyComponent11());
}
}
});
但是这段代码没有检测形状。任何解决方案,请
最佳答案
我使用 Area
编写了一个通用实现,您可以 find here 。具体来说,AreaManager 中的 main 方法提供了有关它们如何协同工作的信息。
如果您只是想检测边界 (30,30,100,100) 内的鼠标点击,您可以在常规 MouseListener/Adapter
中更轻松地实现。但听起来您对完整的解决方案感兴趣,或者?
关于java - 我如何检测 jpanel 内部的形状并将其与另一个按钮一起使用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27021227/