为了账号安全,请及时绑定邮箱和手机立即绑定

Java Swing - 从处理程序类重绘

Java Swing - 从处理程序类重绘

红颜莎娜 2023-12-21 10:44:30
因此,我首先将我的两个类的代码放在这里。SquareSimp.javaimport javax.swing.*;import java.awt.*;import java.awt.event.*;public class SquareSimp{    public static void main( String[] args )    {        FilledFrame frame = new FilledFrame();        frame.setVisible( true );    }}class FilledFrame extends JFrame{    int size = 400;    public FilledFrame()    {        JButton butSmall   = new JButton("Small");        JButton butMedium  = new JButton("Medium");        JButton butLarge   = new JButton("Large");        JButton butMessage = new JButton("Say Hi!");        SquarePanel panel = new SquarePanel(this);        JPanel butPanel = new JPanel();        butSmall.addActionListener(new ButtonHandler1(this, 200){            @Override            public void actionPerformed(ActionEvent actionEvent) {                size = 200;                panel.repaint();            }        });        butMedium.addActionListener(new ButtonHandler1(this, this.size){            @Override            public void actionPerformed(ActionEvent actionEvent) {                size = 300;                panel.repaint();            }        });        butLarge.addActionListener(new ButtonHandler1(this, this.size){            @Override            public void actionPerformed(ActionEvent actionEvent) {                size = 400;                panel.repaint();            }        });        butPanel.add(butSmall);        butPanel.add(butMedium);        butPanel.add(butLarge);        butPanel.add(butMessage);        add(butPanel, BorderLayout.NORTH);        add(panel, BorderLayout.CENTER);        setSize( size+100, size+100 );        setDefaultCloseOperation(EXIT_ON_CLOSE);    }}到目前为止,一切正常,这很棒。然而,根据要求,我被要求为每个类制作一个按钮处理程序。有人可以向我解释一下我的 ButtonHandler 在这里实际上在做什么吗?因为我觉得我可以用更好的方法来完成它(在按钮处理程序类中创建事件并根据按下的按钮影响其大小),而不是创建匿名函数并覆盖 actionPerformed 事件。我不知道该怎么做,所以任何解释的帮助都会很棒!
查看完整描述

3 回答

?
烙印99

TA贡献1829条经验 获得超13个赞

ButtonHandler1并没有真正使用,因为传递给它的参数从未使用过,并且在构造它时它的单个方法被覆盖。

所以这:


     butSmall.addActionListener(new ButtonHandler1(this, 200){

            @Override

            public void actionPerformed(ActionEvent actionEvent) {

                size = 200;

                panel.repaint();

            }

      });

无需构建即可编写ButtonHandler1:


    butSmall.addActionListener(new ActionListener(){

        @Override

        public void actionPerformed(ActionEvent actionEvent) {

            size = 200;

            panel.repaint();

        }

    });

或者使用 lambda 表达式:


    butSmall.addActionListener(actionEvent -> {

        size = 200;

        panel.repaint();

    });

有很多方法可以实现您想要的功能。根据您所写的内容,您可以ButtonHandler1这样定义:


class ButtonHandler1 implements ActionListener{

    private final FilledFrame theApp;

    private final int theSize;

    ButtonHandler1(FilledFrame app, int size){

        theApp = app;

        theSize = size;

    }


    @Override

    public void actionPerformed(ActionEvent actionEvent) {

        theApp.size = theSize; //better use a setter in FilledFrame

        theApp.repaint();

    }

}

并像这样使用它:


    butSmall.addActionListener(new ButtonHandler1(this, 200));


    butMedium.addActionListener(new ButtonHandler1(this, 300));


    butLarge.addActionListener(new ButtonHandler1(this, 400));

创建ButtonHandler1一个内部类FilledFrame使事情变得更简单:


class ButtonHandler1 implements ActionListener{

    private final int theSize;

    ButtonHandler1(int size){

        theSize = size;

    }


    @Override

    public void actionPerformed(ActionEvent actionEvent) {

        size = theSize;

        repaint();

    }

}

通过以下方式使用它:


    butSmall.addActionListener(new ButtonHandler1(200));


    butMedium.addActionListener(new ButtonHandler1(300));


    butLarge.addActionListener(new ButtonHandler1(400));


查看完整回答
反对 回复 2023-12-21
?
慕后森

TA贡献1802条经验 获得超5个赞

a 的任何处理程序*Listener都用于处理events从正在侦听的对象生成的处理程序。在主程序中,似乎有几个可以改变窗口大小的按钮。


对于实现 ButtonHandler 的类,您没有执行任何操作,因为您的actionPerformed方法没有执行任何操作。


并且anonymous classes是实现侦听器的可接受的方式。但是,我更喜欢使用,inner classes因为它们更干净(恕我直言)并且仍然可以state访问enclosing class.


您的处理程序类应如下所示:


// This is a class whose object will handle the event.

class ButtonHandler1 implements ActionListener {

   private FilledFrame theApp;

   private int         theSize;


   ButtonHandler1(FilledFrame app, int size) {

      theApp = app;

      theSize = size;

   }


   public void actionPerformed(ActionEvent actionEvent) {

      theApp.size = theSize;

      theApp.repaint();

   }

}

以下是将该处理程序添加到按钮的调用。


      butSmall.addActionListener(new ButtonHandler1(this, 200));

      butMedium.addActionListener(new ButtonHandler1(this, 300));

      butLarge.addActionListener(new ButtonHandler1(this, 400));


查看完整回答
反对 回复 2023-12-21
?
慕森卡

TA贡献1806条经验 获得超8个赞

我不确定我是否理解这个问题,但有两种方法可以重构代码:


完全删除您的ButtonHandler1类并实现匿名类中的所有逻辑:

    //FilledFrame.java


    butSmall.addActionListener(new ActionListener(){

                @Override

                public void actionPerformed(ActionEvent actionEvent) {

                    FilledFrame.this.size = 200;

                    panel.repaint();

                }

            });


您可以在类上添加一些 getter 和 setter 方法FilledFrame,并调用它们ButtonHandler1.actionPerformed来实现其中的逻辑,如下所示:

    package Lab2;


    import javax.swing.*;

    import java.awt.event.ActionEvent;

    import java.awt.event.ActionListener;

    // This is a class whose object will handle the event.

    public class ButtonHandler1 implements ActionListener{

        private FilledFrame theApp;

        private int theSize;

        ButtonHandler1(FilledFrame app, int size){

            theApp = app;

            theSize = size;

        }


        public void actionPerformed(ActionEvent actionEvent) {

            theApp.setSizeMember(200); //do not use theApp.setSize(), create a method with a different name

            theApp.getSquarePanel().repaint();

        }


    }

我将创建 getter/setter 的工作留给FilledFrame您。


查看完整回答
反对 回复 2023-12-21
  • 3 回答
  • 0 关注
  • 83 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信