Well, this is a very newie cuestion. Im stating to write by myself the code of my GUI applications with the help of window builder, i have decided to stop using netbeans couse ive read some peope in here that said that would be good. You may think i havent investigate, but trust me, i did my homework...
I tryed the way oracle says:
- Declare an event handler class and specify that the class either implements an ActionListener interface or extends a class that implements an ActionListener interface. For example:
public class MyClass implements ActionListener {
- Register an instance of the event handler class as a listener on one or more components. For example:
someComponent.addActionListener(instanceOfMyClass);
- Include code that implements the methods in listener interface. For example:
public void actionPerformed(ActionEvent e) { ...//code that reacts to the action... }
package Todos;
import java.awt.BorderLayout;
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
public class Main extends JFrame {
private JPanel contentPane;
protected JButton btnNewButton;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
Main frame = new Main();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the frame.
*/
public Main() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.getContentPane().setLayout(new BorderLayout());
//setDefaultLookAndFeelDecorated(false);
//setIconImage(Image imagen);
setTitle("");
setSize(java.awt.Toolkit.getDefaultToolkit().getScreenSize());
setPreferredSize(java.awt.Toolkit.getDefaultToolkit().getScreenSize());
setLocationRelativeTo(null);
this.btnNewButton = new JButton("New button");
this.btnNewButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
asd(arg0);
}
});
this.getContentPane().add(this.btnNewButton, BorderLayout.NORTH);
}
public void asd(ActionEvent arg0) {
this.getContentPane().add(new JButton("asd"));
}
}
The cuestion is, why this code doesnt work, the JButton im trying to add to the JFrame with the ActionPerformed event is not visible after i click.
This is an example code, may look silly, but i think it simplifies the cuestion, since my problem is in a few lines of code its not neccesary to show you the hole proyect.
Thank you in advance!