Guest User

Untitled

a guest
Jan 19th, 2019
112
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.43 KB | None | 0 0
  1. import java.awt.BorderLayout;
  2. import java.awt.CardLayout;
  3. import java.awt.event.ActionEvent;
  4. import java.awt.event.ActionListener;
  5.  
  6. import javax.swing.JButton;
  7. import javax.swing.JFrame;
  8. import javax.swing.JLabel;
  9. import javax.swing.JOptionPane;
  10. import javax.swing.JPanel;
  11. import javax.swing.SwingUtilities;
  12.  
  13. public class TestCardLayout {
  14.  
  15. protected void initUI() {
  16. JFrame frame = new JFrame("test");
  17. frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  18. final CardLayout layout = new CardLayout();
  19. final JPanel panel = new JPanel(layout);
  20. for (int i = 0; i < 1000; i++) {
  21. panel.add(new JLabel("Label " + i), getLabelConstraint(i));
  22. }
  23. JButton next = new JButton("Next");
  24. next.addActionListener(new ActionListener() {
  25.  
  26. @Override
  27. public void actionPerformed(ActionEvent e) {
  28. layout.next(panel);
  29. }
  30. });
  31. JButton previous = new JButton("Previous");
  32. previous.addActionListener(new ActionListener() {
  33.  
  34. @Override
  35. public void actionPerformed(ActionEvent e) {
  36. layout.previous(panel);
  37. }
  38. });
  39. final JButton choose = new JButton("Choose");
  40. choose.addActionListener(new ActionListener() {
  41.  
  42. @Override
  43. public void actionPerformed(ActionEvent e) {
  44. String value = JOptionPane.showInputDialog(choose, "Enter a number between 0 and 999");
  45. try {
  46. int i = Integer.valueOf(value);
  47. if (i > -1 && i < 1000) {
  48. layout.show(panel, getLabelConstraint(i));
  49. }
  50. } catch (NumberFormatException e1) {
  51. e1.printStackTrace();
  52. }
  53. }
  54. });
  55. JPanel buttonPanel = new JPanel();
  56. buttonPanel.add(previous);
  57. buttonPanel.add(next);
  58. buttonPanel.add(choose);
  59.  
  60. frame.add(buttonPanel, BorderLayout.SOUTH);
  61. frame.add(panel);
  62. frame.pack();
  63. frame.setLocationRelativeTo(null);
  64. frame.setVisible(true);
  65. }
  66.  
  67. private String getLabelConstraint(int i) {
  68. return "ComponentConstraint" + i;
  69. }
  70.  
  71. public static void main(String[] args) {
  72. SwingUtilities.invokeLater(new Runnable() {
  73.  
  74. @Override
  75. public void run() {
  76. new TestCardLayout().initUI();
  77. }
  78. });
  79. }
  80. }
Add Comment
Please, Sign In to add comment