|
||
|
|||
|
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 Java Code
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();
}
}
Java Code
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");
}
}
}
![]() and sorry for the bad English
|
|
|||
|
JFrame.show is deprecated and a bit overkill, you can just call pack() and the JFrame will repack all of its container components.
|
|
|||
|
Quote:
so can i stop it from resizing itself? and just a quick question, can i use KeyListener in AL_test.java? because when i try it, its not working ![]() ![]() ![]() Java Code
public AL_test() {
btn1 = new JButton("baru");
add(btn1);
btn1.addActionListener(this);
KeyListener listener = new KeyListener() {
public void keyPressed(KeyEvent e) {
int key = e.getKeyCode();
if (key == KeyEvent.VK_SPACE) {
System.out.println("You have clicked on SPACE");
}
}
public void keyReleased(KeyEvent e) {}
public void keyTyped(KeyEvent e) {}
};
addKeyListener(listener);
}
|
|
|||
|
It will resize because it recalculates the layout based upon the preferred sizes of all it components. You can play with setting the preferred size of the panel to prevent this.
The keyListener code won't compile because you have not implemented the entire interface. You can use a KeyAdapter which makes implementing the interface easier. In addition, the component you wish to listen for event must have focus, so after you call pack you need to call requestFocus on the appropriate component |
| The Following User Says Thank You to copeg For This Useful Post: | ||
amahara (03-02-2010) | ||
![]() |
| Thread Tools | |
| Display Modes | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Question about ActionListener | TimW | AWT / Java Swing | 6 | 04-11-2009 04:00 PM |
| How to Add ActionListener to a JButton. Java Swing | JavaPF | Java Code Snippets and Tutorials | 3 | 20-10-2009 11:15 AM |
| SOMEONE PLEASE HELP ME ADD THE ACTIONLISTENER INTERFACE... | beginning2Understand | AWT / Java Swing | 5 | 30-06-2009 05:42 AM |
| How to write above a JPanel | bruno88 | AWT / Java Swing | 4 | 23-06-2009 11:16 PM |
| Creating, and displaying a JPanel inside another JPanel | JayDuck | AWT / Java Swing | 1 | 07-04-2009 01:02 PM |