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.Color;
  9. import java.awt.Container;
  10. import java.awt.FlowLayout;
  11. import java.awt.GridLayout;
  12. import java.awt.HeadlessException;
  13. import java.awt.event.WindowAdapter;
  14. import java.awt.event.WindowEvent;
  15. import javax.swing.Icon;
  16. import javax.swing.ImageIcon;
  17. import javax.swing.JFrame;
  18. import javax.swing.JLabel;
  19. import javax.swing.JPanel;
  20.  
  21. /**
  22.  *
  23.  * @author ta
  24.  */
  25. public class JLabelDemo extends JFrame {
  26.  
  27.  
  28. public JLabelDemo(String title) throws HeadlessException {
  29.  
  30. this.setSize(600,800);
  31. this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  32. this.setLocationRelativeTo(null);
  33.  
  34. this.setVisible(true);
  35. creatAndShow();
  36. }
  37.  
  38. public static void main(String[] args) {
  39. new JLabelDemo("MY WINDOW");
  40.  
  41.  
  42. }
  43.  
  44. private void creatAndShow() {
  45.  
  46. JPanel jpn = new JPanel();
  47. jpn.setLayout(new GridLayout(1,3,5,5));
  48.  
  49. //Chèn ảnh
  50. Icon icon = new ImageIcon(getClass().getResource("java-heap-space-stack.jpg"));
  51.  
  52.  
  53. JLabel lb1 = new JLabel("label text only");
  54. JLabel lb2 = new JLabel(icon);
  55. JLabel lb3 = new JLabel("icon and text",icon,JLabel.CENTER);
  56.  
  57. lb3.setVerticalTextPosition(JLabel.BOTTOM);
  58. lb3.setHorizontalTextPosition(JLabel.CENTER);
  59.  
  60. jpn.add(lb1);
  61. jpn.add(lb2);
  62. jpn.add(lb3);
  63. Container con = getContentPane();
  64. con.add(jpn);
  65. pack();
  66.  
  67. }
  68.  
  69.  
  70.  
  71.  
  72.  
  73.  
  74.  
  75. }
  76.  
Compilation error #stdin compilation error #stdout 0s 0KB
stdin
Standard input is empty
compilation info
Main.java:25: error: class JLabelDemo is public, should be declared in a file named JLabelDemo.java
public class JLabelDemo extends JFrame {
       ^
1 error
stdout
Standard output is empty