Switching / moving jpanels in java vibrations

  • > I have a jframe defined in the package 'abc'. this jframe is also the main class.
  • > the same package 'abc' also contains 4 jpanels (panel1, panel2, panel3, panel4) defined in different Java classes.

how should I call / show these different jpanels from the main jframe class when the user clicks on different buttons? what if i have 40-50 such jpanels. What would be the most efficient way to switch these panels from the main class?

+4
source share
2 answers

This is a very simple example that uses next() and one button to change panels.

 import java.awt.EventQueue; import javax.swing.JFrame; import javax.swing.JPanel; import java.awt.CardLayout; import java.awt.Color; import java.awt.GridBagLayout; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.BorderFactory; import javax.swing.BoxLayout; import javax.swing.JButton; import javax.swing.JLabel; import javax.swing.border.Border; public class TestCards extends JFrame { private final CardLayout cl = new CardLayout(); private final JPanel cards = new JPanel(cl); private final Border border = BorderFactory.createEmptyBorder(60, 60, 60, 60); public TestCards() { JPanel contentPane = new JPanel(); contentPane.setLayout(new BoxLayout(contentPane, BoxLayout.Y_AXIS)); contentPane.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5)); setContentPane(contentPane); JPanel panel1 = new JPanel(new GridBagLayout()); panel1.setBorder(border); panel1.setBackground(Color.RED); panel1.add(new JLabel("Card 1")); cards.add(panel1, "First Panel"); JPanel panel2 = new JPanel(new GridBagLayout()); panel2.setBorder(border); panel2.setBackground(Color.GREEN); panel2.add(new JLabel("Card 2")); cards.add(panel2, "Second Panel"); JPanel panel3 = new JPanel(new GridBagLayout()); panel3.setBorder(border); panel3.setBackground(Color.BLUE); panel3.add(new JLabel("Card 3")); cards.add(panel3, "Third Panel"); JButton controlButton = new JButton("Switch"); controlButton.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { cl.next(cards); } }); JPanel controlPanel = new JPanel(); controlPanel.setBackground(Color.LIGHT_GRAY); controlPanel.add(controlButton); contentPane.add(cards); contentPane.add(controlPanel); cl.show(cards, "First Panel"); } public static void main(String[] args) { EventQueue.invokeLater(new Runnable() { public void run() { TestCards frame = new TestCards(); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.pack(); frame.setLocationRelativeTo(null); frame.setVisible(true); } }); } } 

An alternative (among many) would be to use 3 buttons, each of which displays a specific panel (using show instead of next ).

For more information, see this .

+4
source

The CardLayout methods previous and next can be used to move forward and backward respectively through map components. To simplify component navigation not sequentially, you can put map names in a String array:

 private static final String[] CARD_NAMES = { "name_2996062106101", ... }; 

Then, to display a specific cardIndex , you could simply use:

 CardLayout layout = (CardLayout) contentPane.getLayout(); layout.show(contentPane, CARD_NAMES[cardIndex]); 

See How to use CardLayout for more details .

+3
source

Source: https://habr.com/ru/post/1438174/


All Articles