this.btA = new JButton("A"); this.btB = new JButton("B"); btA.addActionListener(new ActionListener(){ @Override public void actionPerformed(ActionEvent e){ desktop.getInternalFrameA().toFront(); } }); btB.addActionListener(new ActionListener(){ @Override public void actionPerformed(ActionEvent e){ desktop.getInternalFrameB().toFront(); } });
JPanel panel1 = new JPanel(); JPanel panel2 = new JPanel(); frame.add(panel1); //Pour passer du panel 1 au panel 2 : frame.getContentPane().remove(panel1); frame.add(panel2); frame.validate(); frame.repaint();
Vous n’avez pas trouvé la réponse que vous recherchez ?
Posez votre questionimport java.awt.BorderLayout;Desktop.java
import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class MaFrame extends JFrame implements ActionListener {
/**
*
*/
private static final long serialVersionUID = -5449301750930787694L;
private Desktop desktop;
private JButton btA;
private JButton btB;
private JPanel panBoutons;
public MaFrame() {
this.setSize(800, 600);
this.setBackground(Color.blue);
this.setVisible(true);
this.setLocationRelativeTo(null);
this.setTitle("Ma Frame");
this.setLayout(new BorderLayout());
this.desktop = new Desktop();
this.btA = new JButton("A");
this.btB = new JButton("B");
this.btA.addActionListener(this);
this.btB.addActionListener(this);
this.panBoutons = new JPanel();
this.panBoutons.add(btA, BorderLayout.WEST);
this.panBoutons.add(btB, BorderLayout.EAST);
this.add(desktop, BorderLayout.CENTER);
this.add(this.panBoutons, BorderLayout.NORTH);
}
@Override
public void actionPerformed(ActionEvent arg0) {
if(arg0.getActionCommand().equals("A")) {
this.desktop.getInternalFrameA().moveToFront();
} else if(arg0.getActionCommand().equals("B")) {
this.desktop.getInternalFrameB().moveToFront();
}
}
}
import java.awt.BorderLayout;IntFrame.java
import java.awt.Color;
import java.beans.PropertyVetoException;
import javax.swing.JDesktopPane;
public class Desktop extends JDesktopPane {
/**
*
*/
private static final long serialVersionUID = -1565524468732451452L;
private IntFrame internalFrameA;
private IntFrame internalFrameB;
public Desktop() {
this.setBackground(Color.green);
this.internalFrameA = new IntFrame(Color.yellow, 1);
this.internalFrameB = new IntFrame(Color.red, 2);
this.add(this.internalFrameA, BorderLayout.CENTER);
this.add(this.internalFrameB, BorderLayout.CENTER);
try {
this.internalFrameA.setMaximum(true);
this.internalFrameB.setMaximum(true);
} catch (PropertyVetoException e1) {
e1.printStackTrace();
}
this.internalFrameA.toFront();
}
public IntFrame getInternalFrameA() {
return internalFrameA;
}
public void setInternalFrameA(IntFrame internalFrameA) {
this.internalFrameA = internalFrameA;
}
public IntFrame getInternalFrameB() {
return internalFrameB;
}
public void setInternalFrameB(IntFrame internalFrameB) {
this.internalFrameB = internalFrameB;
}
}
import java.awt.Color;
import javax.swing.JInternalFrame;
public class IntFrame extends JInternalFrame {
/**
*
*/
private static final long serialVersionUID = 4546649357556755344L;
public IntFrame(Color color, int n) {
//this.isMaximum();
this.setBackground(color);
this.setTitle("Internal Frame :"+n);
this.setVisible(true);
this.setResizable(true);
this.setLocation(0, 0);
this.setBorder(null);
}
}