Java 扩展JPanel与可观测

Java 扩展JPanel与可观测,java,swing,observable,observers,Java,Swing,Observable,Observers,我是Java新手,我有一个问题 我有一个扩展JPanel的类。它包含一个JButton。如果我按下这个按钮,我希望另一个类注意到这一点。通过这样做,我读到我必须使用Observer/Observable 唯一的问题是,我只能扩展一次。。有什么建议吗 两类: public class ControlsPanel extends JPanel{ private JButton start; private JButton stop; private int animatio

我是Java新手,我有一个问题

我有一个扩展JPanel的类。它包含一个JButton。如果我按下这个按钮,我希望另一个类注意到这一点。通过这样做,我读到我必须使用Observer/Observable

唯一的问题是,我只能扩展一次。。有什么建议吗

两类:

public class ControlsPanel extends JPanel{

    private JButton start;
    private JButton stop;
    private int animationSpeed = 5;
    private boolean startIsPressed;
    private CashRegistersPanel cr_panel;

    public ControlsPanel(final ParametersPanel panel) {

        start       = new JButton("Start");
        stop        = new JButton("Stop");

        start.setFont(new Font("Arial", Font.BOLD, 14));
        stop.setFont(new Font("Arial", Font.BOLD, 14));

        this.setLayout(null);
        this.setBackground(new Color(199,202,255));

        this.add(start);
        this.add(stop);

        start.setBounds(10, 10, 280, 30);
        stop.setBounds(10, 50, 280, 30);

        stop.setEnabled(false);

        start.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent arg0) {

            if (start.getText().equals("Start")) {
                start.setText("Pause");
                stop.setEnabled(true);
                startIsPressed = true;
            }

            }
        });

        //For the STOP BUTTON
        stop.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                if (stop.isEnabled()) {
                    stop.setEnabled(false);
                    start.setText("Start");
                    startIsPressed = false;
                }
            }
        });
    }

    public boolean StartisPressed() {
        return startIsPressed;
    }
}


您可以在类中实现ActionListener,假设MyPanel正在扩展JPanel。然后可以在按钮的ActionListener中传递MyPanel的对象

class MyPanel extends JPanel implements ActionListener {

    @Override
    public void actionPerformed(ActionEvent e) {
       // Your Drawing Code
    }
    //your other jpanel code
} 
button.addActionListener(myPanel1);
创建自定义JPanel的对象

 MyPanel myPanel1;
现在可以将myPanel1对象作为按钮的ActionListener传递

class MyPanel extends JPanel implements ActionListener {

    @Override
    public void actionPerformed(ActionEvent e) {
       // Your Drawing Code
    }
    //your other jpanel code
} 
button.addActionListener(myPanel1);

您应该将ActionListener添加到要监视的Jbutton

不要扩展JPanel,没有一个理由可以扩展它。现在,如果您想让另一个类注意到,您应该使用控制器作为MVC模式的排序版本

你为什么要扩展JPanel?对Swing类进行子类化只能用于添加行为,而不是控制布局。我有一个JFrame类和两个JPanel类。在第一个JPanel类中,我有一个按钮。如果按下此按钮,我希望第二个JPanel绘制一个圆。使用PropertyChangeEvent,如图所示。感谢您的回复。我有一个ActionListener,如果我按下按钮,布尔值isPressed变为true。我希望我的第二堂课注意到这一变化,并用图形画一个圆圈。请参阅我的更新答案。如果你有任何问题,告诉我。这对我似乎不起作用。。等一下,让我的学生粘贴一点我的代码和类。我真的被困在这里,希望你能帮助我,这不会起作用,因为是的,我可以向JButton添加ActionListener,我已经添加了,但是第二个类中的JPanel看不到按钮是否被按下。你可以在任何类上调用方法,通知JButton已被按下。我已经上传了一些代码。谢谢,我将查看如何使用控制器