0
을 선택했지만 종료 할 MenuListener가있는 JMenu를 만들려고했지만 프로그램이 종료되지 않았습니다.MenuListener가 작동하지 않습니다.
컴파일러에 오류 메시지가 표시되지 않습니다. e.getsource()
이 작동하지 않거나 다른 것이라면 확실하지 않습니다.
미리 감사드립니다. 질문에
import java.awt.*;
import javax.swing.*;
import javax.swing.event.MenuEvent;
import javax.swing.event.MenuListener;
public class entree implements MenuListener{
JFrame frame;
JMenu exit,teach;
entree(){
Font font=new Font("SansSerif",Font.BOLD,22);
JFrame frame=new JFrame();
ImageIcon icon=new ImageIcon("D:\\Capture_aurora.png");
JLabel bg=new JLabel(icon);
JMenuBar mb=new JMenuBar();
JMenu teach=new JMenu("Teach");
JMenu exit =new JMenu("Exit");
teach.setFont(font);exit.setFont(font);exit.addMenuListener(this);teach.addMenuListener(this);
mb.add(teach);mb.add(Box.createHorizontalGlue());mb.add(exit);
JButton button1=new JButton("Start");
button1.setFont(font);
button1.setBounds(870,820,150,45);
frame.setJMenuBar(mb);
frame.add(button1);
frame.add(bg,BorderLayout.CENTER);
frame.setExtendedState(JFrame.MAXIMIZED_BOTH);
frame.setUndecorated(true);
frame.setVisible(true);
}
public static void main(String[]args) {new entree();}
public void menuSelected(MenuEvent e) {
if(e.getSource()==exit) {
System.exit(0);frame.dispose();}// Code supposed to work here, but the program won't exit
if(e.getSource()==teach) {}
}
public void menuDeselected(MenuEvent e) {
}
public void menuCanceled(MenuEvent e) {
}
}
메뉴 자체가 아닌 MenuItems에 리스너가 추가되어서는 안됩니까? – Stultuske
난 그냥 MenuItems에 대한 ActionListener 필요할 것이라고 생각하고 그것은 잘 작동 Wud, ActionListener 캔트 JMenus 그게 MenuListener를 사용하여 그게 구현 될 수 없습니다. – Spots
하지 마십시오. 메뉴에 리스너를 추가하지 말고 메뉴 항목에 추가해야합니다. 메뉴가 원하는 "클릭 가능한"요소가 아니므로 MenuItem은 – Stultuske