【发布时间】:2014-09-05 11:39:07
【问题描述】:
在下面的代码中,JFrame是先导入后扩展的。已经导入后扩展它的原因是什么?为什么我们不能直接在导入时使用它,比如 Scanner ?
import java.awt.FlowLayout;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.JFrame;
import javax.swing.JButton;
import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JOptionPane;
public class tuna extends JFrame{
private JButton reg;
private JButton custom;
public tuna(){
super("The Title");
setLayout(new FlowLayout());
reg = new JButton("reg Button");
add(reg);
Icon x = new ImageIcon(getClass().getResource("xxx.png"));
Icon y = new ImageIcon(getClass().getResource("yyy.png"));
custom = new JButton("custom button" , x);
custom.setRolloverIcon(y);
add(custom);
handler thehandler = new handler();
reg.addActionListener(thehandler);
custom.addActionListener(thehandler);
}
public class handler implements ActionListener{
public void actionPerformed(ActionEvent event){
JOptionPane.showMessageDialog(null, String.format("%s", event.getSource()));
}
}
}
提前致谢!
【问题讨论】:
-
不要扩展JFrame,创建这个Object作为局部变量
-
你不必扩展
JFrame.你的问题是基于一个错误的前提。
标签: java swing inheritance import