java - Java Swing - 从处理程序类重新绘制

83 1

SquareSimp.java





import 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);



 // Exercise 2.


 //Anonymous implementations of listeners are very efficient when you do not need to pass parameters to the


 // constructor of the implemented listener.


 butMessage.addActionListener(new ActionListener()


 // An anonymous function. Creates an actionListener that shows a dialog.


 {


 @Override


 public void actionPerformed(ActionEvent e)


 {


 JOptionPane.showMessageDialog(null,"Hiiii");


 }


 });



 }



}



class SquarePanel extends JPanel


{


 FilledFrame theApp;



 SquarePanel(FilledFrame app)


 {


 theApp = app;


 }



 public void paintComponent(Graphics g)


 {


 super.paintComponent(g);


 g.setColor(Color.green);


 g.fillRect(20, 20, theApp.size, theApp.size);


 }


}



ButtonHandler1.java


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) {


 }



}




到目前为止,这一切都很好,有人可以向我解释一下我的buttonHandler实际上在做什么?

非常感谢您!

Brandon

时间: 原作者:

140 3

*Listener的任何处理程序都用于处理从被监听的对象生成的events

anonymous classes是实现侦听器的一种可以接受的方法,但是,我更喜欢使用inner classes,因为它是清洁的(恕见谅),仍然可以访问enclosing classstate

这是您的处理程序类的外观:


// 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));



原作者:
100 4

  • 完全删除ButtonHandler1类并在匿名类中实现所有逻辑:
  • 
     //FilledFrame.java
    
    
    
     butSmall.addActionListener(new ActionListener(){
    
    
     @Override
    
    
     public void actionPerformed(ActionEvent actionEvent) {
    
    
     FilledFrame.this.size = 200;
    
    
     panel.repaint();
    
    
     }
    
    
     });
    
    
    
    
    
  • 可以在FilledFrame类上添加一些getter和setter方法,并在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();
    
    
     }
    
    
    
     }
    
    
    
    

原作者:
143 3

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


 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));



原作者:
...