我正在尝试编写一个程序,其中JFrame上有两个按钮,用于单击它们的操作不同。但是当我运行我的程序时,只有一个按钮,一个" no"当我想要一个是和否按钮时按钮。我的代码:
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.JButton;
import javax.swing.JPanel;
import javax.swing.JFrame;
public class YesNoButton extends JButton{
private static final int HEIGHT = 400;
private static final int WIDTH = 400;
public static void main(String[] args){
JFrame jf = new JFrame();
JButton yesButton = new JButton("YES");
JButton noButton = new JButton("NO");
jf.add(yesButton);
jf.add(noButton);
class PushListener implements ActionListener{
public void actionPerformed(ActionEvent e){
if (e.getSource() == yesButton) {
System.out.println("YOU CLICKED YES");
} else if (e.getSource() == noButton) {
System.out.println("YOU CLICKED NO");
}
}
}
ActionListener listen = new PushListener();
noButton.addActionListener(listen);
yesButton.addActionListener(listen);
jf.setSize(WIDTH,HEIGHT);
jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
jf.setVisible(true);
}
}
我想要一个"是"和"不"按钮。