我在一个更改JFrame中的面板的程序中遇到此问题。我知道如何使用动作侦听器,但我已经有了一个用于我的图像的组件。有人能帮助我吗?
package javaapplication3;
import java.awt.*;
import java.awt.event.*;
import java.awt.image.*;
import java.io.*;
import javax.imageio.*;
import javax.swing.*;
public class JavaApplication3 extends Component {
BufferedImage img;
public JavaApplication3(){
try {img = ImageIO.read(new File("a.jpg"));}catch (IOException e) {}}
public static void main(String[] args) {
JFrame frame = new JFrame();
JButton b = new JButton();
JPanel p = new JPanel();
JPanel p1 = new JPanel();
p.add(b);
p1.add(new JavaApplication3());
frame.add(p);
frame.setSize(150,150);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}}
答案 0 :(得分:1)
对于类,您可以同时具有多个接口的扩展和实现,因此它将是
class JavaApplication3 extends Component implements ActionListener
有关java中接口的参考 http://docs.oracle.com/javase/tutorial/java/concepts/interface.html
Vs
java中类的继承 http://docs.oracle.com/javase/tutorial/java/IandI/subclasses.html