Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.awt.BorderLayout;
- import java.awt.CardLayout;
- import java.awt.event.ActionEvent;
- import java.awt.event.ActionListener;
- import javax.swing.JButton;
- import javax.swing.JFrame;
- import javax.swing.JLabel;
- import javax.swing.JOptionPane;
- import javax.swing.JPanel;
- import javax.swing.SwingUtilities;
- public class TestCardLayout {
- protected void initUI() {
- JFrame frame = new JFrame("test");
- frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
- final CardLayout layout = new CardLayout();
- final JPanel panel = new JPanel(layout);
- for (int i = 0; i < 1000; i++) {
- panel.add(new JLabel("Label " + i), getLabelConstraint(i));
- }
- JButton next = new JButton("Next");
- next.addActionListener(new ActionListener() {
- @Override
- public void actionPerformed(ActionEvent e) {
- layout.next(panel);
- }
- });
- JButton previous = new JButton("Previous");
- previous.addActionListener(new ActionListener() {
- @Override
- public void actionPerformed(ActionEvent e) {
- layout.previous(panel);
- }
- });
- final JButton choose = new JButton("Choose");
- choose.addActionListener(new ActionListener() {
- @Override
- public void actionPerformed(ActionEvent e) {
- String value = JOptionPane.showInputDialog(choose, "Enter a number between 0 and 999");
- try {
- int i = Integer.valueOf(value);
- if (i > -1 && i < 1000) {
- layout.show(panel, getLabelConstraint(i));
- }
- } catch (NumberFormatException e1) {
- e1.printStackTrace();
- }
- }
- });
- JPanel buttonPanel = new JPanel();
- buttonPanel.add(previous);
- buttonPanel.add(next);
- buttonPanel.add(choose);
- frame.add(buttonPanel, BorderLayout.SOUTH);
- frame.add(panel);
- frame.pack();
- frame.setLocationRelativeTo(null);
- frame.setVisible(true);
- }
- private String getLabelConstraint(int i) {
- return "ComponentConstraint" + i;
- }
- public static void main(String[] args) {
- SwingUtilities.invokeLater(new Runnable() {
- @Override
- public void run() {
- new TestCardLayout().initUI();
- }
- });
- }
- }
Add Comment
Please, Sign In to add comment