Salve galera…Estou tentando add um JPanel a uma área de um JFrame mas não estou conseguindo, esse JPanel eh para ser adicionado com um evento de click num JMenuItem.
to tentando assim
//classe extend JPanel
public class DesktopArea extends JPanel{
private static int deskCount;
public DesktopArea(){
JLabel label = new JLabel("" + deskCount);
label.setForeground(Color.WHITE);
this.add(label);
this.setBackground(Color.BLACK);
this.setCursor(Cursor.getPredefinedCursor(Cursor.CROSSHAIR_CURSOR));
deskCount++;
}
public Integer getDeskCount(){
return this.deskCount;
}
}
// JFrame, metodo de inicializacao no JFrame
public class FramePrincipal extends JFrame{
public FramePrincipal(){
begin();
}
}
public void begin(){
DesktopArea deskArea = new DesktopArea();
this.setExtendedState(MAXIMIZED_BOTH);
this.setLayout(new BorderLayout());
this.add(panelNorth, BorderLayout.NORTH);
this.getContentPane().add(deskArea, BorderLayout.CENTER); //aqui funciona a primeira vez, qdo coloco esse metodo no construtor do JFrame
this.add(panelSouth, BorderLayout.SOUTH);
this.setTitle("Project" + deskArea.getDeskCount());
}
//aqui onde eu tento adicionar novamente o JPanel ao JFrame FramePrincipal
private void fileNewActionPerformed(java.awt.event.ActionEvent evt) {
DesktopArea deskArea = new DesktopArea();
this.getContentPane().add(deskArea, BorderLayout.CENTER); //aqui onde tento add o JPanel
this.setTitle("Project" + deskArea.getDeskCount());
this.getContentPane().repaint();
}
Como resolver isso ???
obrigado
Eu nao entendi muito bem onde vc colocou esse teu FileNew event, ligado a que ele estah,
mas criei um menu, atrelei ele a um evento action.
Criei o evento action e beleza, o panel foi instanciado.
Vê se te ajuda em algo:
[code]
import java.awt.BorderLayout;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class FramePrincipal extends JFrame {
JPanel panelNorth = new JPanel();
JPanel panelSouth = new JPanel();
public FramePrincipal() {
begin();
}
public void begin() {
jMenuBar1 = new javax.swing.JMenuBar();
jMenu1 = new javax.swing.JMenu();
jMenuItem1 = new javax.swing.JMenuItem();
jMenu1.setText("File");
jMenuItem1.setText("jMenuItem1");
jMenuItem1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jMenuItem1ActionPerformed(evt);
}
});
jMenu1.add(jMenuItem1);
jMenuBar1.add(jMenu1);
setJMenuBar(jMenuBar1);
DesktopArea deskArea = new DesktopArea();
this.setExtendedState(MAXIMIZED_BOTH);
this.setLayout(new BorderLayout());
this.add(panelNorth, BorderLayout.NORTH);
this.getContentPane().add(deskArea, BorderLayout.CENTER); //aqui funciona a primeira vez, qdo coloco esse metodo no construtor do JFrame
this.add(panelSouth, BorderLayout.SOUTH);
this.setTitle("Project" + deskArea.getDeskCount());
}
//aqui onde eu tento adicionar novamente o JPanel ao JFrame FramePrincipal
private void fileNewActionPerformed(java.awt.event.ActionEvent evt) {
DesktopArea deskArea = new DesktopArea();
this.getContentPane().add(deskArea, BorderLayout.CENTER); //aqui onde tento add o JPanel
this.setTitle(“Project” + deskArea.getDeskCount());
this.getContentPane().repaint();
}
private void jMenuItem1ActionPerformed(java.awt.event.ActionEvent evt) {
DesktopArea deskArea = new DesktopArea();
this.getContentPane().add(deskArea, BorderLayout.CENTER); //aqui onde tento add o JPanel
this.setTitle("Project" + deskArea.getDeskCount());
this.getContentPane().repaint();
}
private javax.swing.JMenu jMenu1;
private javax.swing.JMenu jMenu2;
private javax.swing.JMenu jMenu3;
private javax.swing.JMenuBar jMenuBar1;
private javax.swing.JMenuItem jMenuItem1;
public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new FramePrincipal().setVisible(true);
}
});
}
}[/code]