A simple way to do this is by creating different panels for each screen, and switching as needed. Using CardLayout
, the switch becomes simpler than having to invalidate and redesign the screen every hour, or creating internal frames(what I find very expensive to control than with panels).
You initially need to create the panels, as if it were the "fragments" of screen, then name them so that the CardLayout
can identify them among themselves.
See the example below adapted from the documentation examples, to get a sense of how you can do:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class CardLayoutDemo extends JFrame {
private static final long serialVersionUID = 1L;
final static String BUTTONPANEL = "Card with JButtons";
final static String TEXTPANEL = "Card with JTextField";
JPanel cards;
public CardLayoutDemo() {
setTitle("CardLayoutDemo");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JMenuBar menubar = new JMenuBar();
JMenu menu = new JMenu("Menu");
JMenuItem submenu01 = new JMenuItem("Painel de botoes");
submenu01.setActionCommand(BUTTONPANEL);
submenu01.addActionListener(new ChangeCardlayoutListener());
JMenuItem submenu02 = new JMenuItem("Painel de texto");
submenu02.setActionCommand(TEXTPANEL);
submenu02.addActionListener(new ChangeCardlayoutListener());
menu.add(submenu01);
menu.add(submenu02);
menubar.add(menu);
setJMenuBar(menubar);
// cria o painel de botoes
JPanel card1 = new JPanel();
card1.add(new JButton("Button 1"));
card1.add(new JButton("Button 2"));
card1.add(new JButton("Button 3"));
// cria o painel de campos de texto
JPanel card2 = new JPanel(new GridLayout(2, 1, 5, 5));
card2.add(new JTextField("TextField", 20));
card2.add(new JTextField("TextField2", 20));
// este painel é quem será o container para o cardlayout
// organizar os outros dois paineis
cards = new JPanel(new CardLayout());
cards.add(card1, BUTTONPANEL);
cards.add(card2, TEXTPANEL);
getContentPane().add(cards, BorderLayout.CENTER);
pack();
setVisible(true);
}
class ChangeCardlayoutListener implements ActionListener {
@Override
public void actionPerformed(ActionEvent evt) {
CardLayout cl = (CardLayout) (cards.getLayout());
cl.show(cards, (String) evt.getActionCommand());
}
}
public static void main(String[] args) {
javax.swing.SwingUtilities.invokeLater(CardLayoutDemo::new);
}
}
Working:
Note that, obligatorily, one of the panels needs to start visible, but I believe that this is not a problem. And if the panels are too complex, the ideal is to create a class for each one that extends JPanel
, and instances them when adding in CardLayout
.
Reference:
Didn’t you ever try anything? There’s something already done from your screen?
– user28595
Yes, I did get to do something. But I could only change the entire screen contents or remove all fields from the panel and add one by one.
– Jônatas Trabuco Belotti