Java 单独类中的ActionListener
首先,这是一个家庭作业,所以解释和指针比简单的解决方案更受欢迎。我们正在学习swing,并且正在练习单独的类ActionListeners(另外一个问题是,为什么要使用单独的类而不是内部类,似乎内部类更简单,更不容易出错,而且不会失去任何实际能力)。我遇到的问题是将框架作为参数传递,以便单独的类可以访问它所需的工具,然后使用单独的类实际更改显示。该项目应该像幻灯片一样工作,有一个计时器作为默认开关,但也实现了手动移动的按钮Java 单独类中的ActionListener,java,swing,class,Java,Swing,Class,首先,这是一个家庭作业,所以解释和指针比简单的解决方案更受欢迎。我们正在学习swing,并且正在练习单独的类ActionListeners(另外一个问题是,为什么要使用单独的类而不是内部类,似乎内部类更简单,更不容易出错,而且不会失去任何实际能力)。我遇到的问题是将框架作为参数传递,以便单独的类可以访问它所需的工具,然后使用单独的类实际更改显示。该项目应该像幻灯片一样工作,有一个计时器作为默认开关,但也实现了手动移动的按钮 import javax.swing.*; import java.a
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{
}
}
}
我不确定我的错误来源是否在于我如何设置开始的格式,或者我只是没有得到一些东西。任何帮助或意见都将不胜感激
还有一个问题,为什么要使用单独的类而不是内部类,看起来内部类更简单,更不容易出错,而且不会丢失任何真正的能力
起初,您别无选择,因为您无法拥有内部类,但是,在某些情况下,该功能很常见,并且很容易重复,例如,“打开文件”操作,由工具栏按钮、菜单项和键盘快捷方式管理
首先,您的
ActionListener
不需要从SliderFrame
扩展,而是可能希望引用SliderFrame
的实例
这个
应该更像
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) {
public class NextSlideActionListener extends SliderFrame implements ActionListener {
//...
@Override
public void actionPerformed(ActionEvent e) {
frame.nextSlide();
}
}
这允许您的ActionListener
使用由SliderFrame
定义的功能,该功能在JFrame
public class MyActionListener extends SliderFrame implements ActionListener {
private SliderFrame frame;
public MyActionListener(SliderFrame frame) {
public class NextSlideActionListener extends SliderFrame implements ActionListener {
//...
@Override
public void actionPerformed(ActionEvent e) {
frame.nextSlide();
}
}
接下来,您希望在您的幻灯片框架中提供可用于更新幻灯片放映状态的功能
public class SliderFrame extends JFrame{
//...
public void nextSlide() {
//...
}
public void previousSlide() {
//...
}
然后,当触发ActionListener
时,您只需在SliderFrame
public class MyActionListener extends SliderFrame implements ActionListener {
private SliderFrame frame;
public MyActionListener(SliderFrame frame) {
public class NextSlideActionListener extends SliderFrame implements ActionListener {
//...
@Override
public void actionPerformed(ActionEvent e) {
frame.nextSlide();
}
}
(ps-上述示例可由定时器和“下一步”按钮使用,因为这两个按钮的功能相同)这大部分都是有意义的,并且已经实现了,但是我在尝试实现从SliderFrame类到MyActionListener类的各种组件以发送回SliderFrame类时仍然遇到错误。例如,我需要能够使用路径[]发送到SF类中的下一个/上一个幻灯片方法,以了解它在哪里以及它要去哪里。这仅仅是错误引入SF的问题吗?因此,path
应该是一个实例字段。当前幻灯片的索引也应该是一个实例字段。有关更多详细信息,请参阅