1
我试图添加事件侦听器到我的“播放”按钮,但每当我在播放按钮中添加一个消失或我得到和错误。将事件侦听器添加到JFrame
package hamster.race;
import java.awt.*;
import javax.swing.*;
public class HamsterRace extends JFrame {
public HamsterRace(){
super("HamsterRace");
setLookAndFeel();
setSize(350*3, 100*5);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
FlowLayout flo = new FlowLayout();
setLayout(flo);
setVisible(true);
JButton Play = new JButton("Play");
add(Play);
add(Play);
}
private void setLookAndFeel() {
try{
UIManager.setLookAndFeel{
"com.sun.java.swing.plaf.nimbus.NimbusLookAndFeel"
};
}catch (Exception exc) {
//ignore error
}
}
public static void main(String[] args) {
HamsterRace frame = new HamsterRace();
}
}
'调用setVisible(真);'应该是在构造函数中的最后一次通话。 – user1803551