ActionListener在一个单独的类中

时间:2015-04-07 04:23:54

标签: java swing class

首先,这是一项家庭作业,因此解释和指示优于平面解决方案。我们正在学习摇摆并正在练习单独的类ActionListeners(奖励问题,为什么你会在内部类上使用单独的类,看起来内部类更简单,更不容易出错而不会失去任何真正的能力)。我遇到的问题是将Frame作为参数传递,以便单独的类可以访问它需要的工具,然后使用单独的类来实际更改显示。该项目应该像幻灯片一样工作,有一个定时器作为默认开关,但也实现按钮手动移动。

import javax.swing.*;

import java.awt.BorderLayout;
import java.awt.Image;
import java.awt.event.*;
import java.io.File;

public class SliderFrame extends JFrame{

    public SliderFrame(){
        File file1 = new File("images");  //change as necessary
        File file = new File("images\\CMU");
        File[] paths;
        paths = file.listFiles();

        //file1
        ImageIcon left = new ImageIcon("backward.png");
        ImageIcon right = new ImageIcon("forward.png");

        JButton btnLeft = new JButton(left);
        btnLeft.addActionListener(new MyActionListener(this));

        JButton btnRight = new JButton(right);
        btnRight.addActionListener(new MyActionListener(this));

        JTextField jtfTitle = new JTextField("Welcome to CMU!");
        JLabel jlbMain = new JLabel();

        new Timer(2000, new MyActionListener(this)).start();

        JPanel panel = new JPanel();
        panel.setLayout(new BorderLayout());
        panel.add("PAGE_START", jtfTitle);
        panel.add("Center", jlbMain);
        panel.add("LINE_START", btnLeft);
        panel.add("LINE_END", btnRight);

        add(panel);

        setTitle("CPS240 SlideShow");
        setLocationRelativeTo(null);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        pack();
        setVisible(true);

    }
    public static void main(String[] args) {
        JFrame frame = new SliderFrame();
        btnRight.addActionListener(new MyActionListener(frame));
    }

}

然后是我的ActionListener类

import java.awt.event.*;

import javax.swing.*;
//does it need to extend SliderFrame?  Originally I thought it would help with some of my errors
public class MyActionListener extends SliderFrame implements ActionListener {
JFrame frame;

    public MyActionListener(JFrame frame) {
        this.frame = frame;
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        if(e.getSource() instanceof Timer){
            //here's where I need to be able to change the 'main' label in the frame
        } else if(e.getSource() == btnRight){
            //trying to figure out if the left or right button was pushed
        } else{

        }
    }

}

我不确定我的错误的根源在于我如何设置格式以及如果我没有得到某些东西。任何帮助或意见将不胜感激。

1 个答案:

答案 0 :(得分:2)

  

红利问题,为什么你会在一个内部类中使用一个单独的类,看起来内部类更简单,更不容易出错而不会失去任何真正的能力

最初,你别无选择,因为你不能拥有内部类,但是,有时候功能很常见且很容易重复,例如," Open File&#34 ;动作,由工具栏按钮,菜单项和键盘快捷方式管理......


首先,您的ActionListener不需要从SliderFrame延伸,而是可能需要引用SliderFrame的实例...

public class MyActionListener extends SliderFrame implements ActionListener {

应该更像是

public class MyActionListener implements ActionListener {

您需要传递JFrame的引用,而不是传递SliderFrame的引用。话虽如此,我不知道btnRight在哪里,但我很确定它不应该在main方法中维护,而是在SliderFrame本身内维护...

public class SliderFrame extends JFrame{
    public SliderFrame(){
        //...
        btnRight.addActionListener(new MyActionListener(this));

ActionListener也应该期待SliderFrame

的实例
public class MyActionListener extends SliderFrame implements ActionListener {
    private SliderFrame frame;

    public MyActionListener(SliderFrame frame) {

这样,您的ActionListener就可以使用SliderFrame定义的功能,而JFrame

的实例无法使用该功能

接下来,您希望在SliderFrame中提供可用于更新幻灯片放映状态的功能...

public class SliderFrame extends JFrame{
    //...
    public void nextSlide() {
        //...
    }

    public void previousSlide() {
        //...
    }

然后,当您的ActionListener被触发时,您只需在SliderFrame上调用相应的方法

public class NextSlideActionListener extends SliderFrame implements ActionListener {
    //...    
    @Override
    public void actionPerformed(ActionEvent e) {
        frame.nextSlide();
    }

}

(ps-以上示例可由Timer和" next"按钮使用,因为两者的功能相同)