hey everyone,
can someone help me on this problem
this is the problem, i have one instance of JFrame that implements an ActionListener
and from this JFrame i need to call one instance of JPanel that also implements an ActionListener
i have write these code below for testing purpose, and i still dont know what wrong with these code
somehow i was unable to call the JPanel into the JFrame
AL.java
import java.awt.*; import java.awt.event.*; import javax.swing.*; public class AL extends JFrame implements ActionListener { public JMenuBar menuBar; public JMenu fileMenu, helpMenu; public JMenuItem newAction, exitAction, tombolAction; public AL() { menuBar = new JMenuBar(); setJMenuBar(menuBar); fileMenu = new JMenu("File"); menuBar.add(fileMenu); newAction = new JMenuItem("New"); exitAction = new JMenuItem("Exit"); fileMenu.add(newAction); fileMenu.addSeparator(); fileMenu.add(exitAction); newAction.addActionListener(this); exitAction.addActionListener(this); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setSize(300, 300); setLocationRelativeTo(null); setTitle("Save The Villages"); setResizable(false); setVisible(true); } public void actionPerformed(ActionEvent event) { if(event.getSource()==newAction) { System.out.println("You have clicked on the new action"); add(new AL_test()); } if(event.getSource()==exitAction) { System.exit(0); } } public static void main(String[] args) { new AL(); } }
AL_test.java
import java.awt.*; import java.awt.event.*; import javax.swing.*; public class AL_test extends JPanel implements ActionListener { JButton btn1 = new JButton(); public AL_test() { btn1 = new JButton("baru"); add(btn1); btn1.addActionListener(this); } public void actionPerformed(ActionEvent event) { if(event.getSource()==btn1) { System.out.println("You have clicked on button"); } } }
can someone please enlighten me, what am i doing wrong
and sorry for the bad English