Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/353.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 在我自己的组件中的一个面板中有两个不同的按钮_Java_Netbeans_Components - Fatal编程技术网

Java 在我自己的组件中的一个面板中有两个不同的按钮

Java 在我自己的组件中的一个面板中有两个不同的按钮,java,netbeans,components,Java,Netbeans,Components,我正在尝试用两个不同的按钮创建JPanel,其中一个增加文本或窗口的大小,另一个减小文本或窗口的大小。我有一个按钮声明类。当我把这些按钮分别放在JFrame上时,一切都正常。 我不知道如何在每个按钮的JPanel中获取动作侦听器。我所能做的就是监听鼠标点击JPanel。。。 你能帮我吗?我真的很不懂编码,所以请有礼貌:] public class ButtonMy extends Component { private ButtonIncrease increase; private Prope

我正在尝试用两个不同的按钮创建JPanel,其中一个增加文本或窗口的大小,另一个减小文本或窗口的大小。我有一个按钮声明类。当我把这些按钮分别放在JFrame上时,一切都正常。 我不知道如何在每个按钮的JPanel中获取动作侦听器。我所能做的就是监听鼠标点击JPanel。。。 你能帮我吗?我真的很不懂编码,所以请有礼貌:]

public class ButtonMy extends Component {
private ButtonIncrease increase;
private PropertyChangeSupport propertyChangeSupport;

public ButtonMy() {
    setPreferredSize(new Dimension(30,30));
    kolor = Color.blue;
    setForeground(kolor);


    propertyChangeSupport = new PropertyChangeSupport(this);
    increase = ButtonIncrease.Powieksz;

}   

public ButtonIncrease getIncrease() {
    return increase;
}

public void setIncrease(ButtonIncrease increase) {
    ButtonIncrease oldIncrease = this.increase;
    this.increase = increase;
    propertyChangeSupport.firePropertyChange("increase", oldIncrease, increase);
}

public void addPropertyChangeListener(PropertyChangeListener l) {
    propertyChangeSupport.addPropertyChangeListener(l);
}

public void removePropertyChangeListener(PropertyChangeListener l) {
    propertyChangeSupport.removePropertyChangeListener(l);
}


}
有用于绑定2个按钮的JPanel。最大的问题是:/我缺乏想法

public class ButtonB extends JPanel implements ActionListener{

public ButtonMy b1 = new ButtonMy();
public ButtonMy b2 = new ButtonMy();

public ButtonB (){

    init();
}
public final void init(){
    setLayout(new GridLayout(1,2));
    this.przycisk1.setIncrease(ButtonIncrease.Powieksz);
    this.przycisk2.setIncrease(ButtonIncrease.Zmniejsz);
    add(b1);
    add(b2);


}      

}
我测试这个组件的JFrame非常常见。下面的代码仅显示单击单独按钮时inc和dec大小的功能(不在JPanel中)


我没有贴满我的代码。还有一些我认为这里不需要的数学函数(例如setSize)。

我不确定我是否正确理解了这个问题,但我认为在actionListener类下,应该有一个名为actionPerformed的方法&它会说,如果单击button1,增加数字,如果单击按钮2,则减少数字:

public void actionPerformed( ActionEvent event ) {
if (event.getSource()== b1) // your "increase size" code

if(event.getSource()== b2)// your "decrease size" code 
}
按钮侦听器实际上不同于鼠标侦听器;按钮实现ActionListeners,并具有带有事件变量的actionPerformed方法。您可以通过以下方式处理该事件: getSource()-此方法继承自java.util.EventObject,并返回事件最初发生的对象(按钮本身) 或通过getActionCommand()-此方法可用于操作事件,或从ActionEvent继承并返回与此操作关联的命令字符串的任何事件。 然而,鼠标侦听器实现了MouseListener,并且根据鼠标的操作(按下、单击、释放等)有很多方法

public void actionPerformed( ActionEvent event ) {
if (event.getSource()== b1) // your "increase size" code

if(event.getSource()== b2)// your "decrease size" code 
}