fork(1) download
  1. /*
  2.  * To change this license header, choose License Headers in Project Properties.
  3.  * To change this template file, choose Tools | Templates
  4.  * and open the template in the editor.
  5.  */
  6. package swingdemo;
  7.  
  8. import java.awt.BorderLayout;
  9. import java.awt.CardLayout;
  10. import java.awt.Color;
  11. import java.awt.Container;
  12. import java.awt.HeadlessException;
  13. import java.awt.event.ActionEvent;
  14. import java.awt.event.ActionListener;
  15. import javax.swing.JButton;
  16. import javax.swing.JCheckBox;
  17. import javax.swing.JFrame;
  18. import javax.swing.JPanel;
  19.  
  20. /**
  21.  *
  22.  * @author MYPC
  23.  */
  24. public class CardLayoutDemo extends JFrame{
  25.  
  26. public CardLayoutDemo(String title) throws HeadlessException {
  27. super(title);
  28. this.setSize(400,600);
  29. this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  30. this.setVisible(true);
  31. this.setLocationRelativeTo(null);
  32. CreatAndShow();
  33.  
  34. }
  35.  
  36.  
  37.  
  38.  
  39. public void CreatAndShow() throws HeadlessException {
  40.  
  41. // Tạo Panel cha
  42. JPanel jpnBorder = new JPanel();
  43. jpnBorder.setLayout(new BorderLayout());
  44.  
  45.  
  46. //Tạo một panel phía bắc
  47. JPanel jpnNorth = new JPanel();
  48. JButton jbt1 = new JButton("Show Card1");
  49. JButton jbt2 = new JButton("Show Card2");
  50. jpnNorth.add(jbt1);
  51. jpnNorth.add(jbt2);
  52. jpnBorder.add(jpnNorth, BorderLayout.NORTH);
  53.  
  54.  
  55. //Tạo panel ở trung tâm
  56. final JPanel jpnCenter = new JPanel();
  57. jpnCenter.setLayout(new CardLayout());
  58. jpnCenter.setBackground(Color.LIGHT_GRAY);
  59.  
  60. //Tạo một panel bên trong panel trung tâm
  61. final JPanel card1 = new JPanel();
  62. card1.setBackground(Color.LIGHT_GRAY);
  63. card1.add(new JButton("Hello"));
  64. card1.add(new JButton("I'm Card1"));
  65.  
  66.  
  67. //Tạo panel bên trong panel trung tâm
  68. //panel này bị panel Card1 chồng lên
  69. final JPanel card2 = new JPanel();
  70. card2.setBackground(Color.PINK);
  71. card2.add(new JButton("Hi !"));
  72. card2.add(new JCheckBox("CardLayout"));
  73. card2.add(new JButton("I'm Card2"));
  74.  
  75. //Thêm 2 panel này vào panel trung tâm
  76. jpnCenter.add(card1,"my card1");
  77. jpnCenter.add(card2,"my card2");
  78.  
  79. jpnBorder.add(jpnCenter,BorderLayout.CENTER);
  80.  
  81.  
  82. Container con = getContentPane();
  83. con.add(jpnBorder);
  84.  
  85.  
  86. jbt1.addActionListener(new ActionListener() {
  87. @Override
  88. public void actionPerformed(ActionEvent e) {
  89. CardLayout c1 = (CardLayout) jpnCenter.getLayout();
  90.  
  91. c1.show(jpnCenter,"my card1");
  92.  
  93.  
  94. }
  95. });
  96.  
  97.  
  98. jbt2.addActionListener(new ActionListener() {
  99. @Override
  100. public void actionPerformed(ActionEvent e) {
  101. CardLayout c2 = (CardLayout) jpnCenter.getLayout();
  102. c2.show(jpnCenter,"my card2");
  103.  
  104.  
  105. }
  106. });
  107.  
  108. }
  109.  
  110. public static void main(String[] args) {
  111. new CardLayoutDemo("MY WINDOW");
  112. }
  113.  
  114.  
  115. }
  116.  
Compilation error #stdin compilation error #stdout 0s 0KB
stdin
Standard input is empty
compilation info
Main.java:24: error: class CardLayoutDemo is public, should be declared in a file named CardLayoutDemo.java
public class CardLayoutDemo extends JFrame{
       ^
1 error
stdout
Standard output is empty