因此,我将从将我的代码放到我的两个班级开始。
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实际上在做什么吗?就像我不想创建匿名函数并覆盖actionPerformed事件一样,我可以做一个更好的方法(在buttonhandler类中创建事件,并根据按下的按钮从那里影响大小)。我不知道该怎么做,所以对解释的任何帮助都很好!
非常感谢!
布兰登
*Listener
的任何处理程序也用于处理从正在监听的对象生成的events
。在主程序中,似乎您有几个按钮可以改变窗口的大小。
对于实现buttonHandler的类,您没有做任何事情,因为您的actionPerformed
方法没有做任何事情。
并且anonymous classes
是实现侦听器的可接受方法。但是,我更喜欢使用inner classes
,因为它们更干净(imho),并且仍然可以访问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));
我不确定我是否理解这个问题,但是有两种方法可以重构您的代码:
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();
}
}
我将在FilledFrame
上创建吸气剂/设置器留给您。
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));