【发布时间】:2014-04-25 08:50:31
【问题描述】:
private class MyCustomButton extends JButton{...}
private class MyCustomButton2 extends MyCustomButton{...}
public class Example1 extends JPanel{
Example1{
MyCustomButton b1=new MyCustomButton("0");
MyCustomButton2 b2=new MyCustomButton1("b2");
}
private class ButtonListener implements ActionListener//, KeyListener
{
public void actionPerformed(ActionEvent e)
{
System.out.println(e);
}
}
在上面的示例中,我有 2 个 JButton,一个是自定义的,第二个扩展了第一个。
java.awt.event.ActionEvent[ACTION_PERFORMED,cmd=0,when=1395217216471,modifiers=Button1] on **Example1.MyCustomButton**[,165,0,55x55,alignmentX=0.0,alignmentY=0.5,border=javax.swing.plaf.synth.SynthBorder@8a13863,flags=16777504,maximumSize=,minimumSize=,preferredSize=,defaultIcon=pressed.png,disabledIcon=,disabledSelectedIcon=,margin=javax.swing.plaf.InsetsUIResource[top=0,left=0,bottom=0,right=0],paintBorder=true,paintFocus=true,pressedIcon=,rolloverEnabled=true,rolloverIcon=,rolloverSelectedIcon=,selectedIcon=,text=0,defaultCapable=true]
为了实现我的动作监听器,我从打印输出中知道 java 能够返回我按下的按钮的类,我该怎么做?
编辑 1: 我的目标是实现一个具有 2 类按钮的 gui,如果单击一个按钮,我有一组针对该类型按钮的操作,反之亦然,希望它能简化我的操作侦听器实现。
【问题讨论】:
-
你应该考虑使用actions API,依赖类类型是错误的...
-
无关:不要扩展任何 JSomething,它们是为按原样使用而设计的
标签: java swing awt paintcomponent