fork 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.Color;
  10. import java.awt.Container;
  11. import java.awt.Dimension;
  12. import java.awt.Font;
  13. import java.awt.HeadlessException;
  14. import javax.swing.JFrame;
  15. import javax.swing.JLabel;
  16. import javax.swing.JPanel;
  17.  
  18. /**
  19.  *
  20.  * @author hi
  21.  */
  22. public class SwingDemo extends JFrame {
  23.  
  24. /**
  25.   * @param args the command line arguments
  26.   */
  27.  
  28.  
  29. public SwingDemo(String title) throws HeadlessException {
  30. super(title);
  31. this.setSize(400,600);
  32. this.setLocationRelativeTo(null);
  33. this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  34. this.setVisible(true);
  35. creatAndShow();
  36. }
  37.  
  38.  
  39.  
  40. public static void main(String[] args) {
  41. // TODO code application logic here
  42. new SwingDemo("My Window");
  43. }
  44.  
  45. private void creatAndShow() {
  46.  
  47. //Tạo một layout chứa các layout con
  48. JPanel jpnBorder = new JPanel();
  49. jpnBorder.setLayout(new BorderLayout());
  50. Font ft = new Font("Arial",Font.BOLD|Font.ITALIC,25);
  51.  
  52.  
  53.  
  54. //Tạo layout phía Bắc
  55. JPanel jpnNorth = new JPanel();
  56. jpnNorth.setBackground(Color.red);
  57. jpnNorth.setPreferredSize(new Dimension(0,50));
  58. JLabel lbNorth = new JLabel("North");
  59. lbNorth.setForeground(Color.WHITE);
  60. lbNorth.setFont(ft);
  61. jpnNorth.add(lbNorth);
  62. jpnBorder.add(jpnNorth,BorderLayout.NORTH);
  63.  
  64.  
  65.  
  66. //Tạo layout phía Nam
  67. JPanel jpnSouth = new JPanel();
  68. jpnSouth.setBackground(Color.red);
  69. jpnSouth.setPreferredSize(jpnNorth.getPreferredSize());
  70. JLabel lbSouth = new JLabel("South");
  71. lbSouth.setForeground(Color.WHITE);
  72. lbSouth.setFont(ft);
  73. jpnSouth.add(lbSouth);
  74. jpnBorder.add(jpnSouth,BorderLayout.SOUTH);
  75.  
  76.  
  77.  
  78. //Tạo layout phía Tây
  79. JPanel jpnWest = new JPanel();
  80. jpnWest.setBackground(Color.BLUE);
  81. jpnWest.setLayout(new BorderLayout());
  82. JLabel lbWest = new JLabel("WEST",JLabel.CENTER);
  83. lbWest.setFont(ft);
  84. lbWest.setForeground(Color.DARK_GRAY);
  85. jpnWest.add(lbWest,BorderLayout.CENTER);
  86. jpnBorder.add(jpnWest,BorderLayout.WEST);
  87.  
  88.  
  89.  
  90.  
  91.  
  92.  
  93. //Tạo layout phía Đông
  94. JPanel jpnEast = new JPanel();
  95. jpnEast.setBackground(Color.BLUE);
  96. JLabel lbEast = new JLabel("EAST",JLabel.CENTER);
  97. lbEast.setFont(ft);
  98. lbEast.setForeground(Color.WHITE);
  99. jpnEast.setLayout(new BorderLayout());
  100. jpnEast.add(lbEast,BorderLayout.CENTER);
  101. jpnBorder.add(jpnEast,BorderLayout.EAST);
  102.  
  103.  
  104.  
  105.  
  106. //Tạo layout trung tâm
  107. JPanel jpnCenter = new JPanel();
  108. jpnCenter.setLayout(new BorderLayout());
  109. jpnCenter.setBackground(Color.LIGHT_GRAY);
  110. JLabel lbCenter = new JLabel("CENTER",JLabel.CENTER);
  111. lbCenter.setFont(ft);
  112. lbCenter.setForeground(Color.WHITE);
  113. jpnCenter.add(lbCenter,BorderLayout.CENTER);
  114. jpnBorder.add(jpnCenter,BorderLayout.CENTER);
  115.  
  116.  
  117.  
  118.  
  119.  
  120.  
  121.  
  122.  
  123.  
  124.  
  125.  
  126.  
  127.  
  128.  
  129.  
  130.  
  131.  
  132.  
  133.  
  134.  
  135. Container con = getContentPane();
  136. con.add(jpnBorder);
  137.  
  138. }
  139.  
  140. }
  141.  
Compilation error #stdin compilation error #stdout 0s 0KB
stdin
Standard input is empty
compilation info
Main.java:22: error: class SwingDemo is public, should be declared in a file named SwingDemo.java
public class SwingDemo extends JFrame {
       ^
1 error
stdout
Standard output is empty