숙제를 맡고 있는데 다소 이상한 문제가 생겼습니다. 클릭 할 때 메소드를 호출하기 위해 2 개의 JMenuItem을 얻으 려하지만 첨부 된 actionListeners가 작동하지 않는 이유가 있습니다.JMenuItem의이 ActionListener가 실행되지 않는 이유는 무엇입니까?
재미있는 행동 이었지만 그 코드도 실행되지 않는 메소드 호출이라고 가정 할 때 actionListeners에 System.out.println을 추가했습니다.
다른 액션 리스너와 비슷한 문제에 대해 Stack Overflow를 통해 살펴본 결과 제대로 리스너 파트를 수행하고 있다고 생각합니다.
내 코드가 이렇게 처음으로 모듈화 된 것이므로 그와 관련이 있다고 가정합니다.
어디에서 잘못했을 수 있습니까?
클래스 2 여분의 JMenuItem가 필요한 것보다 더 많은 개체를 가질 수 있도록 당신의 JMenuItem를 변수를 미행하고
import java.awt.*;
import java.awt.event.*;
import java.io.IOException;
import java.net.URL;
import javax.imageio.ImageIO;
import javax.swing.*;
import javax.swing.event.*;
class Test100 extends JFrame implements ActionListener
{
//Setup Globals
public static JPanel panelInnerFrame = new JPanel();
public static JDesktopPane desktop = new JDesktopPane();
public static int internalFrameCounter;
public static int xOffset = 15, yOffset = 15, offSetIncrease = 15;//Windows position offset
public static JMenuBar menuTop = new JMenuBar();
public static JMenu apps = new JMenu();
public static JMenuItem fontApp, imageApp = new JMenuItem();
public Test100()
{
panelMaker(); //Make our panel
menuBuilder(); //Call menu builder to build our menu
}
public void panelMaker()
{
//Desktop Pane, this is like a virtual desktop within our pane
desktop = new JDesktopPane();
//Set Desktop Pane to act as our Content Pane
setContentPane (desktop);
//Set properties of our window
setTitle ("Test100");
setSize (1024, 768);
setVisible (true);
//Create a new JPanel,this will sit in the innerframe, we can add things here.
panelInnerFrame = new JPanel();
}
//Method - menuBuilder - A method to build JMenus
public void menuBuilder()
{
//Create a new menu bar
JMenuBar menuTop = new JMenuBar();
setJMenuBar (menuTop); //We need to use the setJMenuBar with a JDesktopPane()
//Add a JMenu
JMenu apps = new JMenu("Apps");
menuTop.add (apps); //Add Jmenu to the menu bar
//Add JMenu Item
JMenuItem fontApp = new JMenuItem("Font App");
apps.add (fontApp); //Add Menu items to our JMenu
//Add JMenu Item
JMenuItem imageApp = new JMenuItem("Image App");
apps.add (imageApp); //Add Menu items to our JMenu
//Add listeners
fontApp.addActionListener (this);
imageApp.addActionListener (this);
}
//Method - frameFontMaker - A method used to build an internal frame displaying fonts
public void frameFontMaker()
{
//Internal Frame, this is our floating box, it sits on our virtual desktop and takes multiple arguments
JInternalFrame innerframe = new JInternalFrame
( "Font App", //frame name
true, //resizable
true, //closable
true, //maximizable
true //iconifiable
);
//Set properties of our innerframe
innerframe.setSize (300, 300);
innerframe.setVisible (true);
innerframe.setLocation (xOffset, yOffset);
//Add a JPanel to our innerframe
innerframe.add (panelInnerFrame);
//Add the innerframe to the desktop
desktop.add (innerframe);
}
//Method - frameImageMaker - A method used to build an internal frame displaying images
public void frameImageMaker()
{
//Internal Frame, this is our floating box, it sits on our virtual desktop and takes multiple arguments
JInternalFrame innerframe = new JInternalFrame
( "Image App", //frame name
true, //resizable
true, //closable
true, //maximizable
true //iconifiable
);
//Set properties of our innerframe
innerframe.setSize (300, 300);
innerframe.setVisible (true);
innerframe.setLocation (xOffset, yOffset);
//Add a JPanel to our innerframe
innerframe.add (panelInnerFrame);
//Add the innerframe to the desktop
desktop.add (innerframe);
}
public void actionPerformed (ActionEvent e)
{
//If fontApp menu item selected
if (e.getSource() == fontApp)
{
frameFontMaker();
//System.out.println ("Test");
}
//If imageApp menu item selected
if (e.getSource() == imageApp)
{
frameImageMaker();
//System.out.println ("Test");
}
}
public static void main (String[] args)
{
new Test100();
}//End Main
}//End Class
이것이 내가 멍청한 놈이라는 이유로 자주 downvoted 되더라도 Stack Overflow에 온 이유입니다. O 완벽하게 작동했습니다! – Ninja2k
@ Ninja2k : 제대로 작동했기 때문에 기쁩니다.하지만 더 중요한 것은 왜 원래 코드가 작동하지 않는지 이해할 수 있습니까? 이 작업을 다시하지 않으려면 개념을 이해해야합니다. –
예, 필자는 본질적으로 하나의 전역 객체와 하나의 로컬 객체를 만들고 그 다음에 로컬 객체 객체를 참조했습니다. – Ninja2k