Como eu faço para quando eu clicar em um botão, este botão criar outro botão em outro panel? Fiz o seguinte mas não funciona:
class AgasalhosListener implements ActionListener {
public void actionPerformed(ActionEvent event) {
JButton agasalho_1_button = new JButton("Agasalho 1");
panel3.add(agasalho_1_button);
}
}
public class Principal {
JFrame frame;
JPanel panel;
JPanel panel2;
JPanel panel3;
JPanel panel4;
JPanel panel5;
public static void main(String[] args) {
Principal gui = new Principal();
gui.go();
}
public void go() {
frame = new JFrame();
panel = new JPanel();
panel2 = new JPanel();
panel3 = new JPanel();
panel4 = new JPanel();
panel5 = new JPanel();
...
JButton agasalhos = new JButton("Agasalhos");
agasalhos.addActionListener(new AgasalhosListener());
panel4.add(agasalhos);
...
}
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class ActionL {
JLabel label;
JButton b1;
JPanel p1;
public ActionL() {
JFrame frame = new JFrame();
label = new JLabel( "\tPROGRAM BUTTON, PANEL ONE!" );
b1 = new JButton( "ButtonOne" );
b1.addActionListener(
new ActionListener() {
public void actionPerformed( ActionEvent e ) {
ActionL2 ac = new ActionL2();
ac.pop();
}
}
);
p1 = new JPanel();
p1.add( label );
p1.add( b1 );
frame.add( p1 );
frame.setSize( 400, 400 );
frame.setVisible( true );
frame.setLocationRelativeTo( null );
frame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
}
class ActionL2 {
public ActionL2() {}
public void pop() {
JFrame frame2 = new JFrame();
JLabel label2 = new JLabel( "\tPROGRAM FRAME TWO, AFTER OF THE BUTTON ONE!" );
JButton b2 = new JButton( "ButtonTwo" );
b2.addActionListener(
new ActionListener() {
public void actionPerformed( ActionEvent e ) {
System.exit( 0 );
}
}
);
JPanel panel2 = new JPanel();
panel2.add( label2 );
panel2.add( b2 );
frame2.add( panel2 );
frame2.setSize( 400, 400 );
frame2.setVisible( true );
frame2.setLocationRelativeTo( null );
frame2.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
}
}
}
public class MyButtonPanel {
public static void main(String[] args) {
ActionL app = new ActionL();
}
}