Java 98. Hướng dẫn kéo thả giao diện Java Swing



    Bản quyền thuộc về TITV.vn, 
    vui lòng không đăng tải lại nội dung từ trang này.

    Video giải thích chi tiết


     Code chi tiết 

    
    package view;
    
    import java.awt.BorderLayout;
    import java.awt.EventQueue;
    
    import javax.swing.JFrame;
    import javax.swing.JPanel;
    import javax.swing.border.EmptyBorder;
    import javax.swing.JButton;
    import java.awt.Font;
    import javax.swing.JTextField;
    import javax.swing.JLabel;
    import java.awt.Color;
    
    public class ViDu extends JFrame {
    
    	private JPanel contentPane;
    	private JTextField textField;
    
    	/**
    	 * Launch the application.
    	 */
    	public static void main(String[] args) {
    		EventQueue.invokeLater(new Runnable() {
    			public void run() {
    				try {
    					ViDu frame = new ViDu();
    					frame.setVisible(true);
    				} catch (Exception e) {
    					e.printStackTrace();
    				}
    			}
    		});
    	}
    
    	/**
    	 * Create the frame.
    	 */
    	public ViDu() {
    		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		setBounds(100, 100, 450, 300);
    		contentPane = new JPanel();
    		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
    		setContentPane(contentPane);
    		contentPane.setLayout(null);
    		
    		JButton btnNewButton = new JButton("New button");
    		btnNewButton.setFont(new Font("Tahoma", Font.PLAIN, 20));
    		btnNewButton.setBounds(82, 154, 280, 96);
    		contentPane.add(btnNewButton);
    		
    		textField = new JTextField();
    		textField.setBounds(34, 78, 390, 65);
    		contentPane.add(textField);
    		textField.setColumns(10);
    		
    		JLabel lblNewLabel = new JLabel("V\u00CD D\u1EE4");
    		lblNewLabel.setForeground(Color.RED);
    		lblNewLabel.setFont(new Font("Tahoma", Font.PLAIN, 20));
    		lblNewLabel.setBounds(184, 11, 63, 56);
    		contentPane.add(lblNewLabel);
    		
    		this.setVisible(true);
    	}
    }
    
    
    package view;
    
    import java.awt.BorderLayout;
    import java.awt.EventQueue;
    
    import javax.swing.JFrame;
    import javax.swing.JPanel;
    import javax.swing.border.EmptyBorder;
    import javax.swing.JButton;
    import java.awt.Font;
    import javax.swing.JTextField;
    import javax.swing.JLabel;
    import java.awt.Color;
    
    public class ViDu2 extends JFrame {
    
    	private JPanel contentPane;
    	private JTextField textField;
    
    	/**
    	 * Launch the application.
    	 */
    	public static void main(String[] args) {
    		EventQueue.invokeLater(new Runnable() {
    			public void run() {
    				try {
    					ViDu2 frame = new ViDu2();
    					frame.setVisible(true);
    				} catch (Exception e) {
    					e.printStackTrace();
    				}
    			}
    		});
    	}
    
    	/**
    	 * Create the frame.
    	 */
    	public ViDu2() {
    		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		setBounds(100, 100, 450, 300);
    		contentPane = new JPanel();
    		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
    		setContentPane(contentPane);
    		contentPane.setLayout(null);
    		
    		JButton btnNewButton = new JButton("New button");
    		btnNewButton.setFont(new Font("Tahoma", Font.PLAIN, 20));
    		btnNewButton.setBounds(82, 154, 280, 96);
    		contentPane.add(btnNewButton);
    		
    		textField = new JTextField();
    		textField.setBounds(34, 78, 390, 65);
    		contentPane.add(textField);
    		textField.setColumns(10);
    		
    		JLabel lblNewLabel = new JLabel("V\u00CD D\u1EE4");
    		lblNewLabel.setForeground(Color.RED);
    		lblNewLabel.setFont(new Font("Tahoma", Font.PLAIN, 20));
    		lblNewLabel.setBounds(184, 11, 63, 56);
    		contentPane.add(lblNewLabel);
    		
    		this.setVisible(true);
    	}
    }
    
    
    package view;
    
    import java.awt.BorderLayout;
    import java.awt.EventQueue;
    
    import javax.swing.JFrame;
    import javax.swing.JPanel;
    import javax.swing.border.EmptyBorder;
    import javax.swing.JButton;
    import java.awt.Font;
    import javax.swing.JTextField;
    import javax.swing.JLabel;
    import java.awt.Color;
    
    public class ViDu3 extends JFrame {
    
    	private JPanel contentPane;
    	private JTextField textField;
    
    	/**
    	 * Launch the application.
    	 */
    	public static void main(String[] args) {
    		EventQueue.invokeLater(new Runnable() {
    			public void run() {
    				try {
    					ViDu3 frame = new ViDu3();
    					frame.setVisible(true);
    				} catch (Exception e) {
    					e.printStackTrace();
    				}
    			}
    		});
    	}
    
    	/**
    	 * Create the frame.
    	 */
    	public ViDu3() {
    		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		setBounds(100, 100, 450, 300);
    		contentPane = new JPanel();
    		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
    		setContentPane(contentPane);
    		contentPane.setLayout(null);
    		
    		JButton btnNewButton = new JButton("New button");
    		btnNewButton.setFont(new Font("Tahoma", Font.PLAIN, 20));
    		btnNewButton.setBounds(82, 154, 280, 96);
    		contentPane.add(btnNewButton);
    		
    		textField = new JTextField();
    		textField.setBounds(34, 78, 390, 65);
    		contentPane.add(textField);
    		textField.setColumns(10);
    		
    		JLabel lblNewLabel = new JLabel("V\u00CD D\u1EE4");
    		lblNewLabel.setForeground(Color.RED);
    		lblNewLabel.setFont(new Font("Tahoma", Font.PLAIN, 20));
    		lblNewLabel.setBounds(184, 11, 63, 56);
    		contentPane.add(lblNewLabel);
    		
    		this.setVisible(true);
    	}
    }
    
    
    
    package test;
    
    import javax.swing.UIManager;
    
    import view.ViDu;
    
    
    public class Test {
    	public static void main(String[] args) {
    		try {
    			UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
    			new ViDu();
    		} catch (Exception e) {
    			e.printStackTrace();
    		}
    	}
    	
    }
    
    

    Không có nhận xét nào:

    Đăng nhận xét